#P8958. 「CGOI-3」残暴圣所
「CGOI-3」残暴圣所
Background
After finally clearing the Spring 2 Heart Gate, ac arrived at Spring 3 and decided to predict the difficulty of Ferocious Sanctuary.
Problem Description
To clear Ferocious Sanctuary, ac needs to perform operations during the next moments. The -th operation requires pressing a key at time , then holding this key until releasing it at time (). At each moment, ac either presses a key or releases a key, but can hold multiple keys at the same time.
The -th operation forms an operation interval , and is strictly increasing. Also, due to the level design of Ferocious Sanctuary, for any two operations, their operation intervals either do not intersect, or one contains the other.
ac designed difficulty coefficients . The difficulty of the -th operation can be evaluated as , and the difficulty of clearing Ferocious Sanctuary is the sum of the difficulties of all operations.
However, since ac is stuck on the first screen of Ferocious Sanctuary, he does not know the operation intervals of each operation. Given and , please compute, over all possible cases, the sum of the clearing difficulty, modulo .
Formal statement:
Given a sequence of length .
Define an "interval group" as a set of intervals, where the -th interval is . For all interval groups satisfying the conditions below, compute the sum of , modulo :
- is a permutation of .
- ,。
- , or or 。
Input Format
The first line contains an integer , representing the number of intervals.
The second line contains integers , with the meaning described above.
Output Format
Output one integer in one line, representing the answer modulo .
2
114 514 1919 810
2691692
3
1 1 4 5 1 4
98
8
275272885 418731188 289662326 114331587 192436268 885936831 877490593 508774565 633402863 149033362 995239139 494498006 168828873 138947653 983144753 844326228
349824160
Hint
Sample Explanation
For sample 1, there are only two possible interval groups:
- , the clearing difficulty is 。
- , the clearing difficulty is 。
The sum of difficulties is , and modulo it is still .
The following cases are invalid:
- , because is required to be strictly increasing, but 。
- , because is required, but 。
- , because at each moment, one must either press a key or release a key, but at the third moment two keys are released, and at the fourth moment no key is pressed or released.
- , because any two operation intervals must be disjoint or one contains the other, but these two intervals intersect and neither contains the other.
Constraints
For of the testdata, 。
For of the testdata, 。
For of the testdata, 。
For another of the testdata, 。
For of the testdata, ,。
Translated by ChatGPT 5
