#arc171b. B - Chmax
B - Chmax
Score: points
问题陈述
对于一个排列 ,它是 的一个排列,我们通过以下步骤定义 :
- 存在一个序列 。
只要存在一个整数 使得 ,就执行以下操作:
- 让 是满足 的最小整数 。然后,用 替换 。 定义 为这个过程结束时的 。(可以证明这个过程在有限步后终止。)
给定一个长度为 的序列 。有多少个排列 满足 ?求出模 的结果。
以上为大语言模型 kimi 翻译,仅供参考。
Problem Statement
For a permutation of , we define by the following procedure:
- There is a sequence .
As long as there is an integer such that , perform the following operation:- Let be the smallest integer that satisfies . Then, replace with .Define as the at the end of this process. (It can be proved that the process terminates after a finite number of steps.)
You are given a sequence of length . How many permutations of satisfy ? Find the count modulo .
Constraints
- All input values are integers.
Input
The input is given from Standard Input in the following format:
Output
Print the number, modulo , of permutations that satisfy .
Sample Input 1
4
3 3 3 4
Sample Output 1
1
For example, if , then is determined to be by the following steps:
- Initially, .
- The smallest integer such that is . Replace with , making .
- The smallest integer such that is . Replace with , making .
- The smallest integer such that is . Replace with , making .
- There are no more that satisfy , so the process ends. The current is defined as .
There is only one permutation such that , which is .
Sample Input 2
4
2 2 4 3
Sample Output 2
0
Sample Input 3
8
6 6 8 4 5 6 8 8
Sample Output 3
18