#P10792. 『SpOI - R1』笑起来最帅的小孩

『SpOI - R1』笑起来最帅的小孩

Problem Description

This problem contains multiple test cases.

There is a digit sequence aa of length nn. Each element in the sequence is a digit from 00 to 99.

There is also an empty digit sequence bb. A cursor will appear in bb (you can think of it as a thin line that can appear between digits, before the whole digit sequence, or after the whole digit sequence). At this time, there are no digits on either side of the cursor.

Now input the digit sequence aa into bb in order. Each time you input one digit, the digit immediately appears after the cursor.

Then the cursor immediately moves randomly to a position before any digit or to after all digits. The randomness is uniform. In other words, the probability that the cursor moves to any movable position is equal.

You are given the digit sequence aa. You need to output the expected value of the final bb interpreted directly as a decimal number (ignoring leading zeros), modulo the prime 20070720072007072007.

Since aa may be very long, this problem uses compressed input.

Specifically, initially aa is an empty digit sequence. The input will give you an array of kk pairs, where the ii-th item is (xi,li)(x_i,l_i), meaning the digit xix_i appears consecutively lil_i times appended to the end of the previous aa. You can use this method to decompress the real aa, and then solve the problem.


How to understand expectation in this problem: For a variable with possible outcomes XX, if the value of outcome XX is vXv_X and the probability of obtaining it is pXp_X, then for the outcome set SS, the expectation is E=XSpXvXE=\sum\limits_{X\in S}p_Xv_X.

If you do not know how to take a rational number modulo: please see this problem.

Input Format

The first line contains an integer TT, the number of test cases.

For each test case:

One line contains an integer kk, the number of items in the pair array after compressing aa.

Then there are kk lines, each containing two integers xi,lix_i,l_i, meaning that based on the aa sequence obtained from the previous item, you append lil_i digits xix_i at the end to get the new aa sequence. You can decompress the real aa sequence in this way.

Output Format

For each test case, output one integer per line, representing the expected value of the possible bb (interpreted as a decimal number, ignoring leading zeros) when the cursor moves randomly each time, modulo the prime 20070720072007072007.

1
2
4 1
2 1
33
1
3
1 2
3 1
7 2
1204285426

Hint

Constraints

This problem uses subtask bundling and subtask dependencies.

Let n=i=1klin=\sum\limits_{i=1}^k l_i.

For 100%100\% of the testdata, it is guaranteed that 1T151\leq T\leq 15, 1n2×1091\leq n\leq 2\times 10^9, 1k1051\leq k\leq 10^5, and for any ii, 0ai90\leq a_i\leq 9, 1li2×1091\leq l_i\leq 2\times 10^9.

Subtask TT\leq nn\leq Special Property Score Subtask Dependencies
1 1515 2×1092\times 10^9 AA 1010 None
2 100100 None 1515
3 55 20002000 2
4 10610^6 2,3
5 2×1092\times 10^9 4545 1,2,3,4

Special Property AA: it is guaranteed that in the decompressed aa, every digit appears at most once.

Translated by ChatGPT 5