#ABC478F. 最小值优先搜索 / Min-First Search
最小值优先搜索 / Min-First Search
Problem Statement
For a tree with vertices called vertex , vertex , , vertex , let be the permutation of obtained by the following procedure.
- Initially, let the sequence be the empty sequence , and let the set be .
- Repeat the following times.
- Let be the minimum value of . Remove from , and append to the end of .
- Then, let be the vertices adjacent to vertex in . For , if is not contained in , add to .
You are given a permutation of . Find the number, modulo , of trees such that . Here, two trees are distinguished when there exists a pair of vertices such that there is an edge between vertex and vertex in one tree and there is no edge in the other tree.
Constraints
- All input values are integers.
Input
The input is given from Standard Input in the following format:
Output
Output the number, modulo , of trees such that .
5
1 3 4 2 5
8
For example, if is the following tree, then .

The procedure to find proceeds as shown in the following figure.

Including this tree, the following eight trees satisfy the condition.

Thus, output 8.
20
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
868586527
Output modulo the number of trees satisfying the condition.
30
1 16 23 14 18 10 12 21 27 30 17 28 4 9 20 13 22 11 19 3 25 26 6 7 2 8 5 24 15 29
910753763
- Source: AtCoder ABC 478 F
相关
在下列比赛中: