#P16125. [USTCPC 2026] Melody
[USTCPC 2026] Melody
Background
Sakai Kanade wants to finish the song that she and her mother did not complete that year. She found an incomplete melody and wants to arrange it into a beautiful and harmonious piece of music.
Problem Description
A melody has length and consists of different notes. That is, each note is an integer from to . The notes form an equal temperament. Between two adjacent notes , a transition is formed with size .
Kanade has a harmony table for transitions , where the harmony value of a transition of size is .
She believes that when transitions of the same size appear repeatedly in a melody, their harmony values combine by exponentiation. That is, if a melody contains transitions of size , then their total harmony value is . In particular, she defines .
She believes that harmony values of transitions of different sizes combine by simple addition. That is, the harmony of the whole melody is .
Now she has found that incomplete melody, in which notes are already fixed as . Kanade wants you to compute: if she can choose the remaining notes arbitrarily, what is the sum of harmony values over all different complete melodies? Since the answer may be very large, you only need to output it modulo .
Input Format
This problem has multiple test cases.
The first line contains an integer (), the number of test cases.
For each test case, the first line contains three integers: the melody length (), the number of fixed notes (), and the number of note types ().
The next line contains an array of length , giving in order. It is guaranteed that .
Then follow lines, each containing two integers , meaning . It is guaranteed that , , and all are distinct.
It is guaranteed that , .
Output Format
Output lines. Each line contains one integer, the answer modulo .
3
7 0 3
0 1 2
13 3 7
1 2 2 2 0 1 0
2 1
10 7
7 5
1000000000 10 12
2 0 3 2 1 3 0 3 2 1 1 0
1 0
10 1
100 2
1000 3
10000 4
100000 5
1000000 6
10000000 7
100000000 8
1000000000 9
14667
3100924
13878903
Hint
For the second test case, one possible complete melody is: . In this melody, , so its harmony is .
Translated by ChatGPT 5