Automobile

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.

Medium4MathImplementationHash mapPrefix sumNo attempts yetTime limit1sMemory limit64 MB

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 (N1)M+1,(N1)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 (1N1061 \le N \le 10^6), MM (1M1061 \le M \le 10^6) and KK (1K10001 \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 1XN1 \le X \le N, and YY is an integer with 0Y1090 \le Y \le 10^9.
  • "S X Y" multiplies every value in column XX by YY. XX is an integer with 1XM1 \le X \le M, and YY is an integer with 0Y1090 \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.