#P17426. [ICPC 2018 Xuzhou R] Rikka with Sorting Networks

    ID: 19928 远端评测题 6000ms 512MiB 尝试: 0 已通过: 0 显示难度普及+/提高− 上传者: 标签>2018枚举其它技巧ICPC

[ICPC 2018 Xuzhou R] Rikka with Sorting Networks

Problem Description

Rikka knows that Bubble sort is a simple but beautiful algorithm, Quicksort is a complex but efficient algorithm, and Shellsort is a weird but practical algorithm. Rikka is interested in all sorting algorithms and she can assign as many new problems for ICPC contests as she wants.

Rikka hates those guys who create new problems with the same ideas over and over again, and she hopes not to become the person she hates to be. Though she has already assigned several problems for sorting algorithms such as Merge sort and Insertion sort, she decides to show you the last problem about sorting algorithms to end this series forever.

Here Rikka introduces the sorting network and she defines a comparator at first. For a permutation AA of the nn smallest positive integers denoted by a1,a2,⋯ ,ana_1, a_2, \cdots, a_n, a comparator [u,v][u, v] (u≠vu \ne v) sorts the uu-th and the vv-th element in AA into nondecreasing order. Formally, a comparator is a mapping [u,v][u, v] satisfying

  • [u,v](au)=min⁡(au,av)[u, v](a_u) = \min(a_u, a_v); and
  • [u,v](av)=max⁡(au,av)[u, v](a_v) = \max(a_u, a_v); and
  • [u,v](ak)=ak[u, v](a_k) = a_k for all kk with k≠uk \ne u and k≠vk \ne v.

Rikka defines a sorting network as a composition of comparators and provides for you a sorting network with kk ordered comparators. Now, Rikka wants you to count the number of permutations of 11 to nn which, through the given sorting network, would become an almost sorted permutation. She says a permutation of 11 to nn is almost sorted if the length of its longest increasing subsequence is at least (n−1)(n - 1).

Input Format

The input contains several test cases, and the first line contains a single integer TT (1≤T≤1001 \le T \le 100), the number of test cases.

For each test case, the first line contains three integers nn (2≤n≤502 \le n \le 50), the length of permutations, kk (0≤k≤100 \le k \le 10), the number of comparators, and qq (108≤q≤10910^8 \le q \le 10^9), a prime number for the output.

Then kk lines follow, the ii-th line of which contains two integers uu and vv (1≤u<v≤n)(1 \le u < v \le n), representing the ii-th comparator [u,v][u, v].

Output Format

For each test case, output a single line with a single integer, the remainder of the number of permutations which meet the requirement divided by qq.

4
4 0 998244353
4 1 998244353
1 2
4 3 998244353
1 2
2 3
1 2
4 6 998244353
1 2
2 3
1 2
3 4
2 3
1 2
10
14
24
24