atcoder#AGC054E. [AGC054E] ZigZag Break
[AGC054E] ZigZag Break
Score : points
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 .
- 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.
8
3 1
3 2
3 3
4 1
4 2
4 3
4 4
200000 10000
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 .