Problem: You are a student looking for a job. Today you had an employment examination for an IT company. They asked you to write an efficient program to perform several operations. First, they showed you an N \times N square matrix and a list of operations. All operations but one modify the matrix, and the last operation outputs the character in a specified cell. Please remember that you need to output the final matrix after you finish all the operations.

Followings are the detail of the operations:

WR r c v
(Write operation) write a integer v into the cell (r,c) (1 \leq v \leq 1,000,000)
CP r1 c1 r2 c2
(Copy operation) copy a character in the cell (r1,c1) into the cell (r2,c2)
SR r1 r2
(Swap Row operation) swap the r1-th row and r2-th row
SC c1 c2
(Swap Column operation) swap the c1-th column and c2-th column
RL
(Rotate Left operation) rotate the whole matrix in counter-clockwise direction by 90 degrees
RR
(Rotate Right operation) rotate the whole matrix in clockwise direction by 90 degrees
RH
(Reflect Horizontal operation) reverse the order of the rows
RV
(Reflect Vertical operation) reverse the order of the columns



Input

First line of each testcase contains nine integers. First two integers in the line, N and Q, indicate the size of matrix and the number of queries, respectively (1 \leq N,Q \leq 40,000). Next three integers, A B, and C, are coefficients to calculate values in initial matrix (1 \leq A,B,C \leq 1,000,000), and they are used as follows: A_{r,c} = (r * A + c * B) mod C where r and c are row and column indices, respectively (1\leq r,c\leq N). Last four integers, D, E, F, and G, are coefficients to compute the final hash value mentioned in the next section (1 \leq D \leq E \leq N, 1 \leq F \leq G \leq N, E - D \leq 1,000, G - F \leq 1,000). Each of next Q lines contains one operation in the format as described above.

Output

Output a hash value h computed from the final matrix B by using following pseudo source code.


h <- 314159265
for r = D...E
for c = F...G
h <- (31 * h + B_{r,c}) mod 1,000,000,007


where "<-" is a destructive assignment operator, "for i = S...T" indicates a loop for i from S to T (both inclusive), and "mod" is a remainder operation.

Examples

Input

2 1 3 6 12 1 2 1 2
WR 1 1 1


Output

676573821


Input

2 1 3 6 12 1 2 1 2
RL


Output

676636559


Input

2 1 3 6 12 1 2 1 2
RH


Output

676547189


Input

39989 6 999983 999979 999961 1 1000 1 1000
SR 1 39989
SC 1 39989
RL
RH
RR
RV


Output

458797120

Solution (in CPP):

Solution:
#include <bits/stdc++.h>
#define REP(i,n) for(int i=0; i<(int)(n); ++i)

using namespace std;
typedef pair<int, int> P;
int N, Q;
int A, B, C;
int D, E, F, G;
int rot;
int revy;
int revx;
int row[40404];
int col[40404];
void get(int& x, int& y) {
    x--; y--;
    if(revy) y = N - 1 - y;
    if(revx) x = N - 1 - x;
    if(rot) swap(y, x);

    y = row[y];
    x = col[x];
}

int init(int x, int y) {
    return ((long long)(y + 1) * A + (long long)(x + 1) * B) % C;
}

void rotate() {
    rot ^= 1;
    bool nry = revx ^ 1;
    bool nrx = revy;
    revy = nry;
    revx = nrx;
}

int main(){
    while(cin >> N >> Q >> A >> B >> C >> D >> E >> F >> G) {

        rot = revx = revy = false;
        REP(i, N) row[i] = i;
        REP(i, N) col[i] = i;

        map<P, int> mat;

        auto get_value = [&](int x, int y, bool insert=true) {
            if(!mat.count(P(x, y))) {
                if(insert) {
                    mat[P(x, y)] = init(x, y);
                } else {
                    return init(x, y);
                }
            }
            return mat[P(x, y)];
        };

        REP(_, Q) {
            string s;
            cin >> s;
            //cout << s << "!" << endl;
            if(s == "WR") {
                int y, x, val;
                cin >> y >> x >> val;
                get(x, y);
                mat[P(x, y)] = val;
            } else if(s == "CP") {
                int y1, x1, y2, x2;
                cin >> y1 >> x1 >> y2 >> x2;
                get(x1, y1);
                get(x2, y2);
                mat[P(x2, y2)] = get_value(x1, y1);
            } else if(s == "SR") {
                int y1, y2;
                cin >> y1 >> y2;
                y1--; y2--;
                if(!rot) {
                    if(revy) y1 = N - 1 - y1;
                    if(revy) y2 = N - 1 - y2;
                    swap(row[y1], row[y2]);
                } else {
                    int x1 = y1, x2 = y2;
                    if(revy) x1 = N - 1 - x1;
                    if(revy) x2 = N - 1 - x2;
                    swap(col[x1], col[x2]);
                }
            } else if(s == "SC") {
                int x1, x2;
                cin >> x1 >> x2;
                x1--; x2--;
                if(rot) {
                    int y1 = x1, y2 = x2;
                    if(revx) y1 = N - 1 - y1;
                    if(revx) y2 = N - 1 - y2;
                    swap(row[y1], row[y2]);
                } else {
                    if(revx) x1 = N - 1 - x1;
                    if(revx) x2 = N - 1 - x2;
                    swap(col[x1], col[x2]);
                }
            } else if(s == "RL") {
                rotate();
            } else if(s == "RR") {
                REP(i, 3) rotate();
            } else if(s == "RH") {
                revy ^= 1;
            } else if(s == "RV") {
                revx ^= 1;
            } 
            // cerr << "rot: " << rot << endl;;
            // cerr << "revx: " << revx << endl;;
            // cerr << "revy: " << revy << endl;;
        }

        long long h = 314159265;
        const int MOD = 1000000007;
        // if(N < 10) {
        //     for(int y = 0; y < N; y++) {
        //         for(int x = 0; x < N; x++) {
        //             int tx = x, ty = y;
        //             get(tx, ty);
        //             //cout << get_value(x, y) << "(" << init(x, y) << ") ";
        //         }
        //         //cout << endl;
        //     }
        // }
        for(int y = D; y <= E; y++) {
            for(int x = F; x <= G; x++) {
                int tx = x, ty = y;
                get(tx, ty);
                //if(N < 10) printf("(%d, %d) -> (%d, %d)\n", x, y, tx, ty);
                h = (h * 31 + get_value(tx, ty, false)) % MOD;
            }
        }
        cout << h << endl;
    }
    return 0;
}