#P1386. 座位安排

座位安排

Description

Arrange seats for nn people. First assign each person a number from 1n1\sim n. Let the number of the ii-th person be aia_i (different people may share the same number). Then, starting from the first person, everyone takes their seat in order.

When the ii-th person arrives, they try to sit at seat aia_i. If aia_i is occupied, they try ai+1a_{i+1}. If ai+1a_{i+1} is also occupied, they try ai+2a_{i+2}, … If they have tried up to the nn-th and still cannot sit, then the arrangement is invalid.

However, the numbers of mm 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 MM.

Input Format

The first line contains an integer TT, the number of test cases.

For each test case, the first line contains three integers, representing n,m,Mn,m,M.

If mm is not 00, the next line contains mm pairs of integers p1,q1,p2,q2,,pm,qmp_1,q_1,p_2,q_2,\ldots,p_m,q_m, where the ii-th pair pi,qip_i,q_i means that the number of the pip_i-th person must be qiq_i.

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 mod M\bmod\ M. 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 30%30\% of the testdata, 1n101 \le n \le 10, 1M327671 \le M \le 32767.
  • For 100%100\% of the testdata, 1T101 \le T \le 10, 1n3001 \le n \le 300, 0mn0 \le m \le n, 2M1092 \le M \le 10^9, 1pi,qin1 \le p_i,q_i \le n, and it is guaranteed that the pip_i are pairwise distinct.

Translated by ChatGPT 5