#P2523. [HAOI2011] Problem c

    ID: 1538 远端评测题 3000ms 125MiB 尝试: 0 已通过: 0 难度: 7 上传者: 标签>动态规划,dp2011河南各省省选组合数学

[HAOI2011] Problem c

Description

Arrange seats for nn people. First, assign each person a label in 1n1\thicksim n. Let the label of the ii-th person be aia_i (different people may share the same label).

Then, starting from the first person, they take seats in order. When the ii-th person arrives, they try to sit at 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, and so on. If all attempts up to seat nn fail, the arrangement is invalid.

However, the labels of mm people have already been fixed (they may have bribed your boss...), and you can only assign labels to the remaining people. Find the number of valid assignment schemes.

Since the answer may be large, output its remainder modulo MM.

Input Format

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

For each test case, the first line contains three integers, nn, mm, and MM.

If m0m \neq 0, then the next line contains mm pairs of integers, p1p_1, q1q_1, p2p_2, q2q_2, ..., pmp_m, qmq_m, where the ii-th pair pip_i, qiq_i means that the label 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 an integer representing the number of schemes $\bmod M. Note that there is exactly one space between YESand the number. Otherwise, outputNO`.

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 100%100\% of the testdata, it is guaranteed that

  • 1T101 \leq T \leq 10.
  • 1n3001 \leq n \leq 300, 0mn0 \leq m \leq n, 2M1092 \leq M \leq 10^9.
  • 1pi,qin1 \leq p_i, q_i \leq n.
  • All pip_i are distinct.

Translated by ChatGPT 5