#P9905. Make 2
Make 2
Make 2
Problem Description
For a sequence consisting of positive integers, you can perform the following operation several times:
- Choose an index which satisfies and , then decrease by , and add to and . A sequence consisting of positive integers is considered good if it is possible to make for each , by using several (possibly, zero) such operations. Now you need to calculate the number of good sequences that satisfy constraints, the -th constraint can be represented as a pair which requires . It can be proven that the answer is finite. Output the answer modulo .
Input
The first line contains a single integer (), denoting the number of test cases. For each test case, the first line contains two integers (, ). The next lines each contains two integers. The -th line contains (, ).
Output
For each test case, output one line with an integer denoting the answer modulo .
Sample Input
3
3 1
2 2
5 2
1 2
5 1
114514 0
Sample Output
1
2
158552999
Source
2023“钉耙编程”中国大学生算法设计超级联赛(10)