Chemical Code
题号:NC221784
时间限制:C/C++/Rust/Pascal 3秒,其他语言6秒
空间限制:C/C++/Rust/Pascal 256 M,其他语言512 M
64bit IO Format: %lld

题目描述

Chemical Code is an integrated development environment designed for chemists. Users can write the molecular formula in this editor, and if they move the mouse over the molecular formula, Chemical Code will display its relative molecular mass.
However, Moca, the developer of Chemical Code, did not implement this feature. Due to the tight schedule, she failed to meet the deadline, so that she just displayed the text "The relative molecular mass of CH3(CH2)2OH is 60.''.

The molecular formula that Chemical Code supports consists of three types of symbols -- chemical elements (), digits and parentheses. The relative molecular mass of is . The relative molecular mass of is . The relative molecular mass of is . The number written after a chemical element denotes that this element is repeated the corresponding number of times, and the number written after a pair of parentheses denotes where the subformula is also repeated the corresponding number of times. For example,  is expanded as , and  is expanded as  and . Notice that nesting parentheses are allowed and multiple consecutive digits are not allowed. The relative molecular mass of a molecular formula is the sum of the relative molecular mass of all the chemical elements after expanding this molecular formula. The following is the context-free grammar of the molecular formula, and the start symbol is E.

To simplify this feature, Chemical Code can be formed as a character array of length , and it will handle the user's input operation. In the beginning, the array is initialized with null characters. The user will perform operations to fill the entire array. There are two types of the input operation. The first one is inputting a chemical element () or a digit in the specified position. And the second one is inputting a pair of parentheses in two specified positions. After each operation, Chemical Code will erase the null characters and print the relative molecular mass of the current molecular formula, which can be derived in the above context-free grammar. It is guaranteed that each pair of parentheses input by the second type of operation is matching. For example, it is forbidden to input parentheses in the -nd and -th position in the molecular formula (␣C)␣.

Moca is asking for your help. Can you implement this feature for her?

输入描述:

The first line contains two integers   -- the length of the character array and the number of operations.

The next  lines describe the user's input operations, where the -th line contains one of two operations:

c_i pos_i ( is or a digit except , ) -- user inputs a chemical element or a digit in the position pos_i;
 L_i R_i  -- user inputs a pair of parentheses in the position L_i and R_i.

It is guaranteed that the user does not input characters in a position twice, and after all the operations, there is no null characters in the character array.

输出描述:

For each operation, print an integer in one line -- the relative molecular mass of the current molecular formula. Since the answer may be very large, you only need to print the answer modulo .
示例1

输入

复制
11 10
1 C 1
1 H 2
1 C 5
1 2 9
2 4 8
1 O 10
1 H 11
1 2 7
1 H 6
1 3 3

输出

复制
12
13
25
37
37
53
54
78
58
60

说明

The following table lists the answer of the above example.