Under construction

Still being built. Things may change

Compressed Formula

Time limit2sMemory limit512 MB

Summary
Evaluate an arithmetic formula with +, -, * over non-negative integers, given as N runs where each short string repeats r_i times, and print the result modulo 1,000,000,007.
Level

Medium7 of 10

Topics
Math, String, Implementation, Divide and conquer
Solved
No attempts yet

Problem

You are given a long but simple formula in a compressed format. A compressed formula is a sequence of NN pairs of an integer rir_i and a string sis_i, where each sis_i consists only of the digits 0 to 9 and the characters +, -, *. To restore the original formula, repeat sis_i exactly rir_i times for every ii, then concatenate the resulting strings in the order of the sequence.

A restored formula always satisfies the following BNF.

<expression> := <term> | <expression> '+' <term> | <expression> '-' <term>
<term> := <number> | <term> '*' <number>
<number> := <digit> | <non-zero-digit> <number>
<digit> := '0' | <non-zero-digit>
<non-zero-digit> := '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9'

Here + means addition, - means subtraction, and * means multiplication of integers.

Write a program that computes the value of the given formula modulo 1,000,000,007. Here xx modulo mm is the non-negative integer rr such that some integer kk satisfies x=km+rx = km + r and 0≤r<m0 \le r < m. For integers xx and mm, that rr is uniquely determined.

Input

The input consists of a single test case.

N
r1 s1
...
rN sN

The first line contains the length NN (1≤N≤1041 \le N \le 10^4) of the compressed formula. Each of the following NN lines holds one piece of the compressed formula. The ii-th line contains an integer rir_i (1≤ri≤1091 \le r_i \le 10^9) and a string sis_i (1≤∣si∣≤101 \le |s_i| \le 10), where rir_i is the number of repetitions of sis_i and sis_i is a piece of the original formula. The formula restored by repeating and concatenating the pieces satisfies the BNF above.

Output

Print the value of the given compressed formula modulo 1,000,000,007.

Examples4

  1. Example 1

    Input
    1
    5 1
    
    Expected output
    11111
    
  2. Example 2

    Input
    2
    19 2*
    1 2
    
    Expected output
    1048576
    
  3. Example 3

    Input
    2
    1 1-10
    10 01*2+1
    
    Expected output
    999999825
    
  4. Example 4

    Input
    4
    3 12+45-12
    4 12-3*2*1
    5 12345678
    3 11*23*45
    
    Expected output
    20008570