#P1386. 座位安排
座位安排
Description
Arrange seats for people. First assign each person a number from . Let the number of the -th person be (different people may share the same number). Then, starting from the first person, everyone takes their seat in order.
When the -th person arrives, they try to sit at seat . If is occupied, they try . If is also occupied, they try , … If they have tried up to the -th and still cannot sit, then the arrangement is invalid.
However, the numbers of people are already fixed (they may have bribed your boss), and you can only assign numbers for the remaining people. Find how many valid arrangements there are. Since the answer may be large, output it modulo .
Input Format
The first line contains an integer , the number of test cases.
For each test case, the first line contains three integers, representing .
If is not , the next line contains pairs of integers , where the -th pair means that the number of the -th person must be .
Output Format
For each test case, output one line. If there is a solution, output YES followed by one integer equal to the number of valid arrangements . Note that there is exactly one space between YES and the number. Otherwise, output NO.
2
4 3 10
1 2 2 1 3 1
10 3 8882
7 9 2 9 5 10
YES 4
NO
Hint
Constraints:
- For of the testdata, , .
- For of the testdata, , , , , , and it is guaranteed that the are pairwise distinct.
Translated by ChatGPT 5
京公网安备 11011102002149号