#arc138f. [arc138_f]KD Tree
[arc138_f]KD Tree
Problem Statement
There are points in a plane, the -th () of which is . Here, is a permutation of .
You can arrange a non-empty set of points, which is a recursive operation defined as follows.
- If contains exactly one point, arranging it creates a sequence composed of just that point.
- If contains two or more points, arranging it creates a sequence composed of those points by doing one of the following two operations.
- Choose any integer and divide into two: the set of points whose -coordinates are less than and the set of the other points. Here, neither nor should be empty. Create a sequence by concatenating the sequence obtained by arranging and the sequence obtained by arranging in this order.
- Choose any integer and divide into two: the set of points whose -coordinates are less than and the set of the other points. Here, neither nor should be empty. Create a sequence by concatenating the sequence obtained by arranging and the sequence obtained by arranging in this order.
Find the number, modulo , of sequences that can be obtained by arranging the set of points .
Constraints
- is a permutation of .
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
Output
Print the answer.
Sample Input 1
3
3 1 2
Sample Output 1
3
The following three sequences can be obtained.
For example, the sequence can be obtained as follows.
- Arrange the set by dividing it to the set of points whose -coordinates are less than (\=\\{(2,1)\\}) and the set of the other points (\=\\{(1,3),(3,2)\\}).
- Arrange the set to obtain the sequence .
- Arrange the set by dividing it to the set of points whose -coordinates are less than and the set of the other points.
- Arrange the set to obtain the sequence .
- Arrange the set to obtain the sequence .
- Concatenate the resulting two sequences to obtain the sequence .
- Concatenate the resulting two sequences to obtain the sequence .
Sample Input 2
5
1 2 3 4 5
Sample Output 2
1
Sample Input 3
10
3 6 4 8 7 2 10 5 9 1
Sample Output 3
1332
Sample Input 4
30
7 11 8 26 4 13 28 5 14 1 16 27 10 2 23 25 17 6 3 18 24 15 9 22 21 29 12 20 19 30
Sample Output 4
641915679