#arc149e. [arc149_e]Sliding Window Sort
[arc149_e]Sliding Window Sort
Problem Statement
You are given positive integers , , and . Consider the following operation on a sequence of positive integers .
- Do the following for in this order.
- Sort $A_{k\\bmod N}, A_{(k+1)\\bmod N}, \\ldots, A_{(k+M-1)\\bmod N}$ in ascending order. That is, replace with for each , where is the result of sorting $A_{k\\bmod N}, A_{(k+1)\\bmod N}, \\ldots, A_{(k+M-1)\\bmod N}$ in ascending order.
You are given a permutation of the integers from through . Find the number of sequences of positive integers that will equal after performing the operation above, modulo .
Constraints
- if .
Input
The input is given from Standard Input in the following format:
Print the number of sequences of positive integers that will equal after performing the operation, modulo .
Sample Input 1
6 3 5
6 4 2 3 1 5
Sample Output 1
18
For instance, satisfies the condition. On this , the operation will proceed as follows.
- The action for changes to .
- The action for changes to .
- The action for changes to .
- The action for changes to .
- The action for changes to , which equals .
Sample Input 2
6 3 5
6 5 4 3 2 1
Sample Output 2
0
No sequence satisfies the condition.
Sample Input 3
20 20 149
13 14 15 16 17 18 19 20 1 2 3 4 5 6 7 8 9 10 11 12
Sample Output 3
401576539
All permutations of the integers from through satisfy the condition.