Description
You are given scales with two sides of negligible mass. The scales are indexed with integers from to . On each side of the scale, there is either another scale or a single weight (of nonnegligible mass). The scale with index is set on the ground, while every other scale rests on some other scale. Notice that this implies that there are exactly weights. The weights are indexed with integers from to , and each has an integer mass: .
The following figure depicts a setup of three scales and four weights as given in the sample test case shown at the end of this problem statement. The numbers in the upright font represent the indices of the scales and the weights, while the numbers in italics represent the masses of the weights. For example, the scale with index rests on the left side of the scale with index , and the weight with index and mass rests on the right side of scale .

We say that a scale is balanced if the total mass of the left side is the same as the total mass of the right side. We say that a scale is super-balanced if it is balanced and if on both sides there is either a super-balanced scale or a weight.
For example, in the figure above, only scale is balanced (and also super-balanced), but if we increased the mass of weights and both to , all three scales would become super-balanced. However, if we instead increased the mass of weight to , scale would become balanced but not super-balanced, since scale still wouldn't be balanced.
We are now to process operations of two types:
- : Change the mass of weight to an integer mass of .
- : Say we want a scale to be super-balanced. We can take some of the weights and make them heavier using magic! Note that these new values for the mass do not have to be integers. What is the minimal total mass on scale necessary to make super-balanced? Since this number may be quite large, output it modulo . It can be shown that given the constraints, the result is always an integer.
Note that the operations of type change the tree whereas the operations of type do not.
Input format
In the first line of input, there are two integers: and .
The -th (for ) of the following lines contains two pairs of a character and a number, each pair describing one side of the -th scale: the character is either S (scale) or W (weight), denoting the type of the object on the given side of the scale, and the integer is the index of the appropriate item. It is guaranteed that a scale never rests on a scale with a greater index.
The following line contains integers, , representing the masses of the weights.
The final lines represent the operations. Each of them is either of the form or of the form , as described in the problem statement.
Output format
For each operation of the second type, output the appropriate minimum mass modulo in a separate line.
Input bounds
- .
- .
- .
- For each operation of type .
- For each operation of type .
- For each operation of type .
Subtasks
For subtasks 2--4, let the depth of a weight be defined as the number of scales on which it rests (directly or indirectly).
- (9 points) There is a weight on at least one side of every scale.
- (8 points) Every weight has the same depth.
- (24 points) Every weight has depth less than . In addition, we have .
- (14 points) Every weight has depth less than .
- (14 points) .
- (31 points) No additional constraints.
3 5
S 2 W 2
W 1 S 3
W 4 W 3
3 6 1 1
2 2
2 1
1 3 2
2 1
2 3
6
12
16
4
To make scale super-balanced, we increase the mass of weights and to each. As a result, scales and will both be balanced, and hence will be super-balanced. The total mass on scale is . When we do this, scale will also be balanced, so it will be super-balanced as well, with a total mass of . When we change the mass of weight to , this no longer works. Therefore, to make scale super-balanced, we can make weight have mass , weight have mass and weight have mass . The total mass would then be .