#agc054e. [agc054_e]ZigZag Break
[agc054_e]ZigZag Break
Problem Statement
Given are integers and . Find the number, modulo , of permutations of that satisfy the following conditions.
- .
- It is possible to repeat the following operation so that has just two elements.
- Choose three consecutive elements , , and . Here, or must hold. Then, erase from .
Solve test cases in an input file.
Constraints
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
Each case is in the following format:
Output
Print the answer for each test case.
Sample Input 1
8
3 1
3 2
3 3
4 1
4 2
4 3
4 4
200000 10000
Sample Output 1
1
2
1
3
5
5
3
621235018
When , one permutation that satisfies the condition is . One way to make it have just two elements is as follows.
- Choose to erase , resulting in .
- Choose to erase , resulting in .