Falling Multiset#12415

Falling Multiset

4 seconds
512 megabytes
Very hard

You are given an initially empty multiset of items. Each item has an integer weight and an integer value .

For each time unit i = 1,2,\dots,n, the following happens in order:

  1. Insert a new item (w_i, v_i) into the multiset.

  2. Update every item currently in the multiset (including the newly inserted one) independently:

    • if its weight w > 0, replace (w, v) with (w-1,\, v \cdot w);

    • if w = 0, the item is unchanged.

You must output the sum of values of all items currently in the multiset after each of the updates modulo .

Input

The first line contains one integer (1 \le n \le 10^{5}) — the number of time units.

Each of the next lines contains two integers w_i and v_i (1 \le w_i \le n;1 \le v_i \le 10^{9}) — the weight and value of the item inserted at time .

Output

For each time unit, print the sum of values of all items in the multiset modulo .

Examples

Example 1

Loading
Loading

Note

For the sample test case, the process will unfold as follows:

Time unit : add .

Update all items:

  • becomes (3, 3 \cdot 4) = (3,12).

Sum of values: .

Time unit : add .

Multiset before update: \{(3,12),(1,2)\}.

Update all items:

  • becomes (2, 12 \cdot 3) = (2,36).

  • becomes (0, 2 \cdot 1) = (0,2).

Sum of values: 36 + 2 = 38.

Time unit : add .

Multiset before update: \{(2,36),(0,2),(3,3)\}.

Update all items:

  • becomes (1, 36 \cdot 2) = (1,72).

  • stays .

  • becomes (2, 3 \cdot 3) = (2,9).

Sum of values: 72 + 2 + 9 = 83.

Time unit : add .

Multiset before update: \{(1,72),(0,2),(2,9),(2,1)\}.

Update all items:

  • becomes (0, 72 \cdot 1) = (0,72).

  • stays .

  • becomes (1, 9 \cdot 2) = (1,18).

  • becomes (1, 1 \cdot 2) = (1,2).

Sum of values: 72 + 2 + 18 + 2 = 94.

Scoring

1. Subtask ( points): (n \le 10^{4})

2. Subtask ( points): (w_i \le 10^4 for all 1 \le i \le n)

3. Subtask ( points): (w_1 = w_2 = \cdots = w_n)

4. Subtask ( points): (n \le 5 \cdot 10^{4})

5. Subtask ( points): No additional constraints