Median of Medians

시간 제한1.5초메모리 제한256 MB

요약
1부터 3N까지의 순열에서 세 블록의 중앙값들의 중앙값이 (3N+1)/2가 되면서 주어진 위치-값 쌍을 만족하는 순열의 개수를 10^9+7로 나눈 나머지로 구한다.
난이도

어려움10점 중 8점

유형
조합론, 수학, 정렬, 누적 합
정답자
아직 제출이 없습니다

문제

Busy Beaver has just learned about the Median of Medians algorithm! To better understand the algorithm, he has chosen an odd positive integer NN and wants to experiment with permutations of 1,2,…,3N\\{1,2,\dots,3N\\}.1

For any odd number kk and a sequence of distinct numbers A=(a_1,a_2,…,a_k)A = (a\_1,a\_2,\dots,a\_k), let B=(b_1,b_2,…,b_k)B = (b\_1,b\_2,\dots,b\_k) be AA sorted in increasing order. Then, median(a_1,a_2,…,a_k)\text{median}(a\_1,a\_2,\dots,a\_k) is b_k+12b\_{\frac{k+1}{2}}, the (k+12)\left(\frac{k+1}{2}\right)-th element of BB.

Busy Beaver thinks that a permutation of (p_1,p_2,…,p_3N)(p\_1,p\_2,\dots,p\_{3N}) of 1,2,…,3N\\{1,2,\dots,3N\\} is nice if and only if

median(median(p_1,p_2,…,p_N),median(p_N+1,p_N+2,…,p_2N),median(p_2N+1,…,p_3N))=3N+12.\text{median}\Big(\text{median}(p\_1, p\_2, \dots, p\_N),\text{median}(p\_{N+1}, p\_{N+2},\dots,p\_{2N}),\text{median}(p\_{2N+1},\dots,p\_{3N})\Big) = \frac{3N+1}{2}.

Busy Beaver is extra picky with his permutations; he likes having certain numbers in certain positions. He has MM pairs of numbers (a_i,b_i)(a\_i,b\_i). A nice permutation (p_1,p_2,…,p_3N)(p\_1,p\_2,\dots,p\_{3N}) is fitting if p_a_i=b_ip\_{a\_i} = b\_i for all 1≤i≤M1 \leq i \leq M.

Help Busy Beaver determine the number of fitting permutations! Since the number of such permutations may be very large, compute the number of such permutations modulo 109+710^9 + 7.


1A permutation of length NN is an array consisting of NN distinct integers from 11 to NN in arbitrary order. For example, \[2,3,1,5,4]\[2,3,1,5,4] is a permutation, but \[1,2,2]\[1,2,2] is not a permutation (22 appears twice in the array), and \[1,3,4]\[1,3,4] is also not a permutation (N=3N=3 but there is 44 in the array).

입력

Each test contains multiple test cases. The first line of input contains a single positive integer TT, the number of test cases (1≤T≤105)(1 \leq T \leq 10^5). The description of each test case follows.

The first line of each test case contains two spaced positive integers NN and MM (1≤N≤3⋅1051 \leq N \leq 3 \cdot 10^5, 0≤M≤3N0 \leq M \leq 3N, and NN is odd) --- the size of the permutation, and the number of pairs (a_i,b_i)(a\_i, b\_i), respectively.

The next MM lines contain two positive integers a_i,b_ia\_i, b\_i (1≤a_i,b_i≤3N1 \leq a\_i, b\_i \leq 3N) --- specifying that p_a_i=b_ip\_{a\_i} = b\_i. It is guaranteed that for all 1≤i<j≤M1 \leq i < j \leq M, a_i≠a_ja\_i \neq a\_j and b_i≠b_jb\_i \neq b\_j.

It is guaranteed that the sum of MM across all test cases is no more than 10610^6.

Note that there is no additional guarantee on the sum of NN across all test cases.

출력

For each test case, output one line with a single integer, indicating the number of fitting permutations modulo 109+710^9 + 7.

힌트

In the first test case, N=1N=1, so we are working with permutations of length 3N=33N = 3. Since M=0M=0, we have no additional constraints on the permutation. One can check that for all permutations of length 33, computing the median of the medians gives the correct median of 22, so there are 66 fitting permutations.

In the second test case, N=3N=3, so we are working with permutations of length 3N=93N = 9. Since M=9M=9, we have 99 constraints on the permutation, which have fixed all 99 elements of the permutation to (1,2,3,4,5,6,7,8,9)(1,2,3,4,5,6,7,8,9).

  • The median of the first 33 elements (1,2,3)(1,2,3) is 22.
  • The median of the middle 33 elements (4,5,6)(4,5,6) is 55.
  • The median of the last 33 elements (7,8,9)(7,8,9) is 88.

The median of (2,5,8)(2,5,8) is 55, which is the correct median. Thus, there is exactly 11 fitting permutation, which is (1,2,3,4,5,6,7,8,9)(1,2,3,4,5,6,7,8,9).

In the third test case, N=3N=3, so we are working with permutations of length 3N=93N = 9. Since M=6M=6, we have 66 constraints on the permutation, which have fixed the last 66 elements of the permutation to (4,5,6,7,8,9)(4,5,6,7,8,9). We are free to permute (1,2,3)(1,2,3) among the first 33 elements. Using a similar analysis as the second test case, we see that the following 66 permutations

  • (1,2,3,4,5,6,7,8,9)(1,2,3,4,5,6,7,8,9)
  • (1,3,2,4,5,6,7,8,9)(1,3,2,4,5,6,7,8,9)
  • (2,1,3,4,5,6,7,8,9)(2,1,3,4,5,6,7,8,9)
  • (2,3,1,4,5,6,7,8,9)(2,3,1,4,5,6,7,8,9)
  • (3,1,2,4,5,6,7,8,9)(3,1,2,4,5,6,7,8,9)
  • (3,2,1,4,5,6,7,8,9)(3,2,1,4,5,6,7,8,9)

are all fitting permutations.

In the fourth test case, N=3N=3, so we are working with permutations of length 3N=93N = 9. Since M=3M=3, we have 33 constraints on the permutation, which have fixed the first 33 elements of the permutation to (1,2,5)(1,2,5). It can be checked that there are no fitting permutations that satisfy these constraints.

예제1

  1. 예제 1

    입력
    4
    1 0
    3 9
    1 1
    2 2
    3 3
    4 4
    5 5
    6 6
    7 7
    8 8
    9 9
    3 6
    4 4
    5 5
    6 6
    7 7
    8 8
    9 9
    3 3
    1 1
    2 2
    3 5
    
    예상 출력
    6
    1
    6
    0