#P16923. [JLCPC 2026] 水晶城堡
[JLCPC 2026] 水晶城堡
Problem Description
is the guardian of the Crystal Castle. In the castle corridor, there is a row of magic crystals. The color ID of the -th crystal is . Adjacent crystals with the same color will resonate and form a color segment, which is a maximal contiguous segment of the same color. For example, the color sequence has color segments: , , and .
Every day, travelers come and ask questions. Each question specifies an interval : if we take out the crystals in this interval and randomly shuffle them (all different color sequences appear with equal probability), what is the expected number of color segments after shuffling?
Output the answer modulo . That is, if the answer is the reduced fraction , output . It can be proven that under the constraints of this problem, always exists.
Input Format
The first line contains an integer (), which is the number of test cases. Then there are blocks, each describing one test case:
- The first line contains two integers (), representing the number of crystals and the number of queries.
- The second line contains integers (), representing the color ID of each crystal.
- The next lines each contain two integers (), representing the endpoints of the query interval.
It is guaranteed that and .
Output Format
For each query in each test case, output one integer per line, representing the expected number of color segments modulo .
1
4 2
1 1 2 2
1 2
1 4
1
3
1
10 5
3 5 3 3 6 4 8 2 3 5
6 9
1 8
8 10
4 9
7 7
4
748683272
3
665496241
1
Hint
For the first sample:
For the first query, the taken crystal colors are . There is only one permutation, and the number of color segments is .
For the second query, the taken crystal colors are . The numbers of color segments for the permutations are , so the expected value is .
Translated by ChatGPT 5