#P7623. [AHOI2021初中组] 收衣服
[AHOI2021初中组] 收衣服
Description
Looking at so many clothes covered with dust, the two of them felt helpless. Also, some clothes cannot be washed together. To sort them into categories, Xiaokeke gave each piece of clothing a distinct label from , where is the number of clothes. It would be more convenient to wash them after arranging the clothes in the order .
Xiaokeke also thought that we can take out a continuous segment of the clothes rack, reverse its order by hand, and then put it back. As an OI contestant, you immediately abstracted Xiaokeke’s algorithm for sorting clothes: let the label of the -th piece of clothing from left to right initially be . Enumerate in the order . Let the smallest label among be . Then reverse from left to right, turning it into .
Xiaoxue soon discovered that although Xiaokeke’s algorithm looks impressive, it is actually quite silly—in the dim light, nobody can tell the labels on the clothes. So they could only return to the room for rational enjoyment: assume the cost of reversing the interval is . The cost of one sorting process is the sum of the operation costs of all reversals. Now Xiaokeke wants to know, when ranges over all permutations, the total sorting cost over all cases.
You only need to output the answer modulo (, a prime).
Input Format
The first line contains an integer .
The next lines: in line , there are space-separated integers, where the -th one denotes .
Output Format
One line containing an integer, which is the answer modulo .
5
1 2 3 4 5
1 2 3 4
1 2 3
1 2
1080
见附加文件的 sort2.in。
见附加文件的 sort2.ans。
Hint
[Sample 1 Explanation]
Let us give an example. When , the steps of the algorithm are as follows:
- When executing , the minimum among , i.e. , is . We reverse interval , and becomes , with cost .
- When executing , the minimum among , i.e. , is . We reverse interval , and becomes , with cost .
- When executing , the minimum among , i.e. , is . We reverse interval , and becomes , with cost .
- When executing , the minimum among , i.e. , is . We reverse interval , and becomes , with cost .
As you can see, after the -th step ends, the positions of the sequence are exactly the clothes labeled . After the algorithm finishes, is sorted. The total cost of this sorting is .
Note: The algorithm will always execute steps, and it will not terminate early even if it becomes sorted in the middle.
[Constraints and Hints]
Hint: The input size of this problem is large, so please avoid overly slow input methods.
- 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
京公网安备 11011102002149号