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>
using namespace std;

using ll = long long;
using pii = pair<int, int>;

constexpr int mod = 1e9 + 7;

constexpr int rev_tbl[2][4][2] = { // [mir][rot][row/col]
    {{0, 0}, {1, 0}, {1, 1}, {0, 1}},
    {{0, 1}, {1, 1}, {1, 0}, {0, 0}}
};

int main() {
    ll n, q, A, B, C, D, E, F, G;
    cin >> n >> q >> A >> B >> C >> D >> E >> F >> G;

    vector<string> op(q);
    vector<int> w(q), x(q), y(q), z(q);
    for(int i = 0; i < q; ++i) {
        cin >> op[i];
        if(op[i] == "WR") cin >> w[i] >> x[i] >> y[i];
        else if(op[i] == "CP") cin >> w[i] >> x[i] >> y[i] >> z[i];
        else if(op[i] == "SR") cin >> w[i] >> x[i];
        else if(op[i] == "SC") cin >> w[i] >> x[i];
    }

    int mir = 0, rot = 0;
    vector<int> ridx(n), cidx(n);
    iota(begin(ridx), end(ridx), 1);
    iota(begin(cidx), end(cidx), 1);
    auto get_r_idx = [&] (int r) -> int& {
        const int i = (rev_tbl[mir][rot][0] ? n - r : r - 1);
        return rot & 1 ? cidx[i] : ridx[i];
    };
    auto get_c_idx = [&] (int c) -> int& {
        const int i = (rev_tbl[mir][rot][1] ? n - c : c - 1);
        return rot & 1 ? ridx[i] : cidx[i];
    };
    auto get_orig_pos = [&] (int r, int c) {
        if(rot & 1) return make_pair(get_c_idx(c), get_r_idx(r));
        else        return make_pair(get_r_idx(r), get_c_idx(c));
    };
    map<pii, int> v;
    for(int i = 0; i < q; ++i) {
        if(op[i] == "WR") {
            int r, c; tie(r, c) = get_orig_pos(w[i], x[i]);
            v[{r, c}] = y[i];
        } else if(op[i] == "CP") {
            int r1, c1, r2, c2;
            tie(r1, c1) = get_orig_pos(w[i], x[i]);
            tie(r2, c2) = get_orig_pos(y[i], z[i]);
            if(v.count({r1, c1})) {
                v[{r2, c2}] = v[{r1, c1}];
            } else {
                v[{r2, c2}] = (r1 * A + c1 * B) % C;
            }
        } else if(op[i] == "SR") {
            swap(get_r_idx(w[i]), get_r_idx(x[i]));
        } else if(op[i] == "SC") {
            swap(get_c_idx(w[i]), get_c_idx(x[i]));
        } else if(op[i] == "RL") {
            if(mir) rot = (rot + 3) % 4;
            else    rot = (rot + 1) % 4;
        } else if(op[i] == "RR") {
            if(mir) rot = (rot + 1) % 4;
            else    rot = (rot + 3) % 4;
        } else if(op[i] == "RH") {
            mir = !mir;
            rot = (rot + 2) % 4;
        } else if(op[i] == "RV") {
            mir = !mir;
        }
    }

    ll h = 314159265;
    for(int r = D; r <= E; ++r) {
        for(int c = F; c <= G; ++c) {
            int rr, cc; tie(rr, cc) = get_orig_pos(r, c);
            const ll val = v.count({rr, cc}) ? v[{rr, cc}] : (rr * A + cc * B) % C;
            h = (h * 31 + val) % mod;
        }
    }
    cout << h << endl;
}