#P13011. 【MX-X13-T6】「KDOI-12」能做到的也只不过是静等缘分耗尽的那一天。

    ID: 12739 远端评测题 2000ms 512MiB 尝试: 0 已通过: 0 难度: 8 上传者: 标签>动态规划 DP数学O2优化组合数学容斥原理梦熊比赛

【MX-X13-T6】「KDOI-12」能做到的也只不过是静等缘分耗尽的那一天。

Description

For a permutation p1np_{1\sim n}, construct its max-heap Cartesian tree, then disconnect the edge between each node and its right child (if it exists). Denote the resulting forest as T(p)T(p).

For example, consider p15=[1,3,2,5,4]p_{1\sim 5} = [1, 3, 2, 5, 4]. The max-heap Cartesian tree and T(p)T(p) are shown below:

Given n,x,yn, x, y, you need to determine how many of the n!n! permutations of 1n1\sim n satisfy that nodes xx and yy belong to the same tree in T(p)T(p). Nodes are identified by their indices, not their values in pp.

Since the answer may be large, output it modulo a prime PP.

Input Format

This problem contains multiple test cases.

The first line contains two positive integers TT and PP, representing the number of test cases and the modulo. It is guaranteed that P\bm{P} is a prime. For each test case:

  • One line with three positive integers n,x,yn, x, y.

Output Format

For each test case, output a non-negative integer indicating the number of valid permutations modulo PP.

10 1000000007
4 1 4
4 2 2
4 3 2
5 4 2
7 3 5
8 2 7
10 3 8
100 99 6
1000 234 789
5000 1234 4321
6
24
8
25
882
3840
270000
220955222
251832899
768412458

Hint

Sample Explanation

For the first test case, the following 66 permutations satisfy the condition:
$[1, 2, 3, 4], [1, 3, 2, 4], [2, 1, 3, 4], [2, 3, 1, 4], [3, 1, 2, 4], [3, 2, 1, 4]$.

For the second test case, all 141\sim 4 permutations are valid.

Constraints

This problem uses bundling tests.

Subtask Points nn\leq TT\leq Special Constraints
11 55 88 10610^6 None
22 1515 20002000 20002000
33 10610^6
44 2525 5×1065\times10^6 2020
55 1515 10510^5 10610^6 A
66 2525 5×1065\times10^6 None
  • Special Constraint A: P=998244353P=998244353.

For all test cases:

  • 1T1061 \leq T \leq 10^6,
  • 1x,yn5×1061 \le x, y \le n \le 5 \times 10^6,
  • 108P109+710^8 \le P \le 10^9 + 7 and PP is a prime.

Hint

Please use a fast input method.


Translation by DeepSeek V3.