atcoder#ARC138F. [ARC138F] KD Tree
[ARC138F] KD Tree
Score : points
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.
- 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.
3
3 1 2
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 () and the set of the other points ().- 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 .
- 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 .
5
1 2 3 4 5
1
10
3 6 4 8 7 2 10 5 9 1
1332
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
641915679