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 <cstdio>
#include <iostream>
#include <sstream>
#include <iomanip>
#include <algorithm>
#include <cmath>
#include <string>
#include <vector>
#include <list>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <bitset>
#include <numeric>
#include <climits>
#include <cfloat>
using namespace std;

template <class T>
vector<vector<T> > matrixProduct(const vector<vector<T> >& x, const vector<vector<T> >& y)
{
    vector<vector<T> > z(2, vector<T>(2, 0));
    for(int i=0; i<2; ++i){
        for(int j=0; j<2; ++j){
            for(int k=0; k<2; ++k){
                z[i][j] += x[i][k] * y[k][j];
            }
        }
    }
    return z;
}

int n;

template <class T>
vector<T> transform(const vector<vector<T> >& x, const vector<T>& y)
{
    vector<T> z(2);
    z[0] = x[0][0] * y[0] + x[0][1] * y[1];
    z[1] = x[1][0] * y[0] + x[1][1] * y[1];
    if(z[0] < 0)
        z[0] = n + 1 + z[0];
    if(z[1] < 0)
        z[1] = n + 1 + z[1];
    return z;
}

int main()
{
    int q;
    long long a, b, c;
    int d, e, f, g;
    cin >> n >> q >> a >> b >> c >> d >> e >> f >> g;

    map<vector<int>, int> change;
    vector<vector<int> > p(2, vector<int>(2, 0));
    p[0][0] = p[1][1] = 1;
    vector<int> toY(n+1), toX(n+1);
    for(int i=1; i<=n; ++i)
        toY[i] = toX[i] = i;

    while(--q >= 0){
        string s;
        cin >> s;

        if(s == "WR"){
            vector<int> q(2);
            int v;
            cin >> q[0] >> q[1] >> v;
            q = transform(p, q);
            q[0] = toY[q[0]];
            q[1] = toX[q[1]];
            change[q] = v;
        }else if(s == "CP"){
            vector<int> q(2), r(2);
            cin >> q[0] >> q[1] >> r[0] >> r[1];
            q = transform(p, q);
            r = transform(p, r);
            q[0] = toY[q[0]];
            q[1] = toX[q[1]];
            r[0] = toY[r[0]];
            r[1] = toX[r[1]];
            if(change.find(q) == change.end())
                change[r] = (q[0] * a + q[1] * b) % c;
            else
                change[r] = change[q];
        }else if(s == "SR"){
            vector<int> q(2), r(2);
            cin >> q[0] >> r[0];
            q = transform(p, q);
            r = transform(p, r);
            swap(toY[q[0]], toY[r[0]]);
            swap(toX[q[1]], toX[r[1]]);
        }else if(s == "SC"){
            vector<int> q(2), r(2);
            cin >> q[1] >> r[1];
            q = transform(p, q);
            r = transform(p, r);
            swap(toY[q[0]], toY[r[0]]);
            swap(toX[q[1]], toX[r[1]]);
        }else if(s == "RL"){
            vector<vector<int> > q(2, vector<int>(2, 0));
            q[0][1] = 1;
            q[1][0] = -1;
            p = matrixProduct(p, q);
        }else if(s == "RR"){
            vector<vector<int> > q(2, vector<int>(2, 0));
            q[0][1] = -1;
            q[1][0] = 1;
            p = matrixProduct(p, q);
        }else if(s == "RH"){
            vector<vector<int> > q(2, vector<int>(2, 0));
            q[0][0] = -1;
            q[1][1] = 1;
            p = matrixProduct(p, q);
        }else if(s == "RV"){
            vector<vector<int> > q(2, vector<int>(2, 0));
            q[0][0] = 1;
            q[1][1] = -1;
            p = matrixProduct(p, q);

        }
    }

    long long h = 314159265;
    for(int y=d; y<=e; ++y){
        for(int x=f; x<=g; ++x){
            vector<int> q(2);
            q[0] = y;
            q[1] = x;
            q = transform(p, q);
            q[0] = toY[q[0]];
            q[1] = toX[q[1]];
            if(change.find(q) == change.end())
                h = (31 * h + ((q[0] * a + q[1] * b) % c)) % 1000000007;
            else
                h = (31 * h + change[q]) % 1000000007;
        }
    }
    cout << h << endl;

    return 0;
}