#agc008e. [agc008_e]Next or Nextnext
[agc008_e]Next or Nextnext
Problem Statement
You are given an integer sequence of length . How many permutations of the integers through satisfy the following condition?
- For each , at least one of the following holds: and .
Find the count modulo .
Constraints
- is an integer.
Input
The input is given from Standard Input in the following format:
Output
Print the number of the permutations that satisfy the condition, modulo .
Sample Input 1
3
1 2 3
Sample Output 1
4
The following four permutations satisfy the condition:
For example, satisfies the condition because , and .
Sample Input 2
2
1 1
Sample Output 2
1
The following one permutation satisfies the condition:
Sample Input 3
3
2 1 1
Sample Output 3
2
The following two permutations satisfy the condition:
Sample Input 4
3
1 1 1
Sample Output 4
0
Sample Input 5
13
2 1 4 3 6 7 5 9 10 8 8 9 11
Sample Output 5
6