#P7111. 青春有悔
青春有悔
Description
It was a competition lasting days. Each day, Gnar must take an exam. Due to many factors, on day Gnar’s theoretical maximum score is , and his actual score on that day is an uniformly random integer in (for example, not enough time, losing points on easy problems, etc.). After days, the organizers will settle the total score and set a cutoff line. Only those whose total score is at least the cutoff line can advance.
Countless “why” filled his mind, as if every day had flaws in performance. “Flaws... if only I could rewrite past regrets...”
Late at night, Gnar began rounds of daydreaming. In each daydream, Gnar returns to day of the competition and takes the exam in a different condition, making that day’s score an uniformly random integer in , while the other days are still random in . However, some subtle effects cause the cutoff line to become . Would his chance of advancing really be higher than in reality?
For each daydream, compute the probability of advancing modulo . It is easy to prove that the answer can be written as an irreducible fraction . You should output the smallest non-negative integer such that .
After all, it is only a daydream. Returning to day with a new maximum score will not change the real value of . The only thing that grows is regret for youth.
Input Format
The first line contains two positive integers and , representing the number of days and the number of daydreams.
The second line contains integers , representing the maximum score each day in reality.
The next lines each contain three integers , representing the day returned to in this daydream, the new maximum score, and the new cutoff line.
Output Format
Output lines. Each line contains one integer, the probability of advancing in the corresponding daydream modulo .
2 2
1 1
1 2 2
2 0 2
499122177
0
5 3
12 16 3 15 9
1 13 25
3 10 30
4 11 17
743774619
107297923
234909256
Hint
Sample Explanation #1
In the first daydream, Gnar returns to the first day. The scores over the two days are generated uniformly at random among , , , , , . Only the last three cases can advance, so the answer is .
In the second daydream, Gnar returns to the second day, and his condition becomes worse. Even if he gets the maximum score for both days, he still cannot advance.
Constraints and Notes
This problem uses bundled testdata. You must pass all test points in a Subtask to receive the score for that Subtask.
- Subtask #1 (10 points): .
- Subtask #2 (10 points): .
- Subtask #3 (10 points): .
- Subtask #4 (20 points): .
- Subtask #5 (25 points): .
- Subtask #6 (25 points): no special constraints.
For all testdata, it is guaranteed that , , .
Translated by ChatGPT 5
京公网安备 11011102002149号