atcoder#ARC132C. [ARC132C] Almost Sorted
[ARC132C] Almost Sorted
Score : points
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 .
4 2
3 -1 1 -1
2
The conditions are satisfied by and .
5 1
2 3 4 5 -1
0
The only permutation of that can be obtained by replacing the is , whose fifth term violates the condition, so the answer is .
16 5
-1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1
794673086
Print the count modulo .