Median of Medians
시간 제한1.5초메모리 제한256 MB
1부터 3N까지의 순열에서 세 블록의 중앙값들의 중앙값이 (3N+1)/2가 되면서 주어진 위치-값 쌍을 만족하는 순열의 개수를 10^9+7로 나눈 나머지로 구한다.
문제
Busy Beaver has just learned about the Median of Medians algorithm! To better understand the algorithm, he has chosen an odd positive integer and wants to experiment with permutations of .1
For any odd number and a sequence of distinct numbers , let be sorted in increasing order. Then, is , the -th element of .
Busy Beaver thinks that a permutation of of is nice if and only if
Busy Beaver is extra picky with his permutations; he likes having certain numbers in certain positions. He has pairs of numbers . A nice permutation is fitting if for all .
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 .
1A permutation of length is an array consisting of distinct integers from to in arbitrary order. For example, is a permutation, but is not a permutation ( appears twice in the array), and is also not a permutation ( but there is in the array).
입력
Each test contains multiple test cases. The first line of input contains a single positive integer , the number of test cases . The description of each test case follows.
The first line of each test case contains two spaced positive integers and (, , and is odd) --- the size of the permutation, and the number of pairs , respectively.
The next lines contain two positive integers () --- specifying that . It is guaranteed that for all , and .
It is guaranteed that the sum of across all test cases is no more than .
Note that there is no additional guarantee on the sum of across all test cases.
출력
For each test case, output one line with a single integer, indicating the number of fitting permutations modulo .
힌트
In the first test case, , so we are working with permutations of length . Since , we have no additional constraints on the permutation. One can check that for all permutations of length , computing the median of the medians gives the correct median of , so there are fitting permutations.
In the second test case, , so we are working with permutations of length . Since , we have constraints on the permutation, which have fixed all elements of the permutation to .
- The median of the first elements is .
- The median of the middle elements is .
- The median of the last elements is .
The median of is , which is the correct median. Thus, there is exactly fitting permutation, which is .
In the third test case, , so we are working with permutations of length . Since , we have constraints on the permutation, which have fixed the last elements of the permutation to . We are free to permute among the first elements. Using a similar analysis as the second test case, we see that the following permutations
are all fitting permutations.
In the fourth test case, , so we are working with permutations of length . Since , we have constraints on the permutation, which have fixed the first elements of the permutation to . It can be checked that there are no fitting permutations that satisfy these constraints.