atcoder#ARC112E. [ARC112E] Cigar Box
[ARC112E] Cigar Box
Score : points
Problem Statement
We applied the following operation times on the sequence , and it resulted in .
- Erase one element. Then, add the erased element to the beginning or end of the sequence.
Find the number of possible sequences of the operations, modulo .
Here, two sequences of operations are considered the same when, in every operation, the same element is chosen and added to the same position.
Constraints
- is a permutation of .
Input
Input is given from Standard Input in the following format:
Output
Print the number of possible sequences of the operations, modulo .
5 2
1 2 4 5 3
5
There are five possible sequences of operations, as follows:
- Erase and add it to the beginning, which results in . Then, erase and add it to the end, which results in .
- Erase and add it to the beginning, which results in . Then, erase and add it to the end, which results in .
- Erase and add it to the end, which results in . Then, erase and add it to the beginning, which results in .
- Erase and add it to the end, which results in . Then, erase and add it to the end, which results in .
- Erase and add it to the end, which results in . Then, erase and add it to the end, which results in .
2 16
1 2
150994942
Two of the four kinds of operations have no effect on the sequence, and the other two swap the two elements. From this fact, we can show that there are sequences of operations - half of all possible sequences - that we want to count. Thus, the answer is modulo , that is, .
10 3000
3 7 10 1 9 5 4 8 6 2
129989699