Under construction

Still being built. Things may change

Automobile

Time limit1sMemory limit64 MB

Summary
Starting from a matrix where each cell holds its row-major index, apply K row and column multiplications and report the total sum modulo 1e9+7.
Level

Medium4 of 10

Topics
Math, Implementation, Hash map, Prefix sum
Solved
No attempts yet

Problem

Mirko found a matrix with NN rows and MM columns on the back seat of his car. The first row holds 1,2,…,M1, 2, \dots, M, the second row holds M+1,M+2,…,2MM+1, M+2, \dots, 2M, and the same rule continues, so the NN-th row holds (N−1)M+1,(N−1)M+2,…,NM(N-1)M+1, (N-1)M+2, \dots, NM.

For N=3N = 3 and M=4M = 4 the matrix is this one.

1234
5678
9101112

The matrix was not interesting enough for Mirko, so KK times he picked a row or a column and multiplied every value in it by a non-negative integer. He wants the sum of all values in the final matrix. That sum can be very large, so print it modulo 109+710^9+7.

Input

The first line contains NN (1≤N≤1061 \le N \le 10^6), MM (1≤M≤1061 \le M \le 10^6) and KK (1≤K≤10001 \le K \le 1000), separated by spaces. Each of the next KK lines describes one operation, given in the order the operations were performed.

  • "R X Y" multiplies every value in row XX by YY. XX is an integer with 1≤X≤N1 \le X \le N, and YY is an integer with 0≤Y≤1090 \le Y \le 10^9.
  • "S X Y" multiplies every value in column XX by YY. XX is an integer with 1≤X≤M1 \le X \le M, and YY is an integer with 0≤Y≤1090 \le Y \le 10^9.

Output

Print the sum of all values in the final matrix modulo 109+710^9+7 on the first line.

Note

Look at the first example. After the second row is multiplied by 4, the fourth column by 1, the third row by 2, and the second row by 0 again, the matrix is this one.

1234
0000
18202224

The sum of all values is 1+2+3+4+0+0+0+0+18+20+22+24=941 + 2 + 3 + 4 + 0 + 0 + 0 + 0 + 18 + 20 + 22 + 24 = 94.

Examples3

  1. Example 1

    Input
    3 4 4
    R 2 4
    S 4 1
    R 3 2
    R 2 0
    
    Expected output
    94
    
  2. Example 2

    Input
    3 1 1
    S 1 4
    
    Expected output
    24
    
  3. Example 3

    Input
    2 4 4
    S 2 0
    S 2 3
    R 1 5
    S 1 3
    
    Expected output
    80