#P10103. [GDKOI2023 提高组] 错排
[GDKOI2023 提高组] 错排
Problem Description
X has recently been studying the derangement problem, so he started thinking about a variant: how many permutations of length satisfy that for positions , , and for all positions , ?
X came up with a total of such queries. Can you tell him the answer for each query?
Since the answer may be too large, you only need to output it modulo .
Input Format
The first line contains an integer , representing the number of queries.
The next lines each contain two integers .
Output Format
Output lines, each containing one integer: the answer modulo .
6
8 0
8 4
100 10
1000 100
10000 1000
100000 10000
14833
576
548326276
694205000
493811811
135068319
Hint
For 100% of the testdata, , .
This problem uses bundled subtasks.
- Subtask 1 (1pts): .
- Subtask 2 (9pts): , .
- Subtask 3 (10pts): .
- Subtask 4 (20pts): .
- Subtask 5 (20pts): .
- Subtask 6 (40pts): No special constraints.
Translated by ChatGPT 5