#arc132c. [arc132_c]Almost Sorted
[arc132_c]Almost Sorted
Problem Statement
Given is a sequence consisting of and , along with an integer . How many sequences satisfy the conditions below? Print the count modulo .
- is a permutation of .
- For each , if . (That is, can be obtained by replacing the s in in some way.)
- For each , .
Constraints
- or .
- if .
- , if and .
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
Output
Print the number of sequences that satisfy the conditions, modulo .
Sample Input 1
4 2
3 -1 1 -1
Sample Output 1
2
The conditions are satisfied by and .
Sample Input 2
5 1
2 3 4 5 -1
Sample Output 2
0
The only permutation of that can be obtained by replacing the is , whose fifth term violates the condition, so the answer is .
Sample Input 3
16 5
-1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1
Sample Output 3
794673086
Print the count modulo .