luogu#P7950. [✗✓OI R1] 后方之水
[✗✓OI R1] 后方之水
Background
This problem has no background, because I am a saint and also the right seat of god.
Problem Description
Define a process of merging stones as follows: there is a row of piles of stones, and the -th pile has stones. Each time, you may choose two adjacent piles to merge. Suppose their sizes are , then you will get one pile of stones, and you need to pay a cost of . In the end, all piles must be merged into one pile. Let be the minimum cost to merge these piles of stones.
Given the total number of stones , compute
and output the answer modulo .
Input Format
This problem contains multiple test cases.
The first line contains an integer , the number of test cases.
The next lines each contain two integers .
Output Format
Output lines, each containing one integer, the answer modulo .
3
2 6
3 5
4 7
35
45
336
5
182565 710825096
429580 541341177
741770 757408347
461909 941427258
114514 1919810
487324711
256967112
352532743
962265551
926494516
Hint
[Sample Explanation]
Explanation for the first test case in the sample:
The partitions are , for a total of cases.
The answer is $1\times 5 + 2 \times 4 + 3 \times 3 + 4 \times 2 + 5 \times 1 = 35$.
[Constraints]
| Test Point ID | ||
|---|---|---|
| 1,2 | ||
| 3,4 | ||
| 5,6,7 | ||
| 8,9 | ||
| 10,11 | ||
| 12,13,14 | ||
| 15 | ||
| 16,17,18 | ||
| 19~25 | ||
For of the testdata, , , .

Translated by ChatGPT 5