Problem:
Matrix Operation
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.
Sample Input 1
2 1 3 6 12 1 2 1 2
WR 1 1 1
Output for the Sample Input 1
676573821
Sample Input 2
2 1 3 6 12 1 2 1 2
RL
Output for the Sample Input 2
676636559
Sample Input 3
2 1 3 6 12 1 2 1 2
RH
Output for the Sample Input 3
676547189
Sample Input 4
39989 6 999983 999979 999961 1 1000 1 1000
SR 1 39989
SC 1 39989
RL
RH
RR
RV
Output for the Sample Input 4
458797120

Submitted solutions:

code_snippet: #include <cstdio>
#include <cstdlib>
#include <cstring>
#include <map>

using namespace std;

const int maxn=4e4+5;

bool RH,RV,R;
int row[maxn],colum[maxn];
int A,B,C,D,E,F,G,n,q;

struct HashMap
{
    int mod;
    int begin[100007],next[maxn],val[maxn],key[maxn];
    int s,i,num;
    void ini()
    {
        mod=100007;
        memset(begin,-1,sizeof(begin));
        num=0;
    }
    int back(int x,int y)
    {
        s=x*n+y;
        for(i=begin[s%mod];i!=-1;i=next[i])
            if(key[i]==s)return val[i];
        return (A*(long long)x+B*(long long)y)%C;
    }
    void change(int x,int y,int k)
    {
        s=x*n+y;
        for(i=begin[s%mod];i!=-1;i=next[i])
            if(key[i]==s)
            {
                val[i]=k;
                return;
            }
        key[num]=s;
        val[num]=k;
        s=s%mod;
        next[num]=begin[s];
        begin[s]=num++;
    }
}S;

void change(int &r,int &c)
{
    if(R)swap(r,c);
    if(RH)r=row[n+1-r];
    else r=row[r];
    if(RV)c=colum[n+1-c];
    else c=colum[c];
}

const int mod=1e9+7;

int r1,r2,c1,c2,r,c,v;
char s[10];

int main()
{
    scanf("%d%d",&n,&q);
    scanf("%d%d%d%d%d%d%d",&A,&B,&C,&D,&E,&F,&G);
    S.ini();
    for(int i=1;i<=n;i++)
        row[i]=colum[i]=i;
    RH=RV=R=false;
    for(int i=0;i<q;i++)
    {
        scanf("%s",s);
        if(s[0]=='W')
        {
            scanf("%d%d%d",&r,&c,&v);
            change(r,c);
            S.change(r,c,v);
        }
        else if(s[0]=='C')
        {
            scanf("%d%d%d%d",&r1,&c1,&r2,&c2);
            change(r1,c1);
            change(r2,c2);
            S.change(r2,c2,S.back(r1,c1));
        }
        else if(s[0]=='S')
        {
            if(s[1]=='R')
            {
                scanf("%d%d",&r1,&r2);
                c1=c2=0;
                if(R)
                {
                    swap(r1,c1);
                    swap(r2,c2);
                }
                if(r1&&RH)r1=n+1-r1,r2=n+1-r2;
                if(c1&&RV)c1=n+1-c1,c2=n+1-c2;
                if(r1)swap(row[r1],row[r2]);
                if(c1)swap(colum[c1],colum[c2]);
            }
            else if(s[1]=='C')
            {
                scanf("%d%d",&c1,&c2);
                r1=r2=0;
                if(R)
                {
                    swap(r1,c1);
                    swap(r2,c2);
                }
                if(r1&&RH)r1=n+1-r1,r2=n+1-r2;
                if(c1&&RV)c1=n+1-c1,c2=n+1-c2;
                if(r1)swap(row[r1],row[r2]);
                if(c1)swap(colum[c1],colum[c2]);
            }
        }
        else if(s[0]=='R')
        {
            if(s[1]=='L')
            {
                if(R)RH=!RH;
                else RV=!RV;
                R=!R;
            }
            else if(s[1]=='R')
            {
                if(R)RV=!RV;
                else RH=!RH;
                R=!R;
            }
            else if(s[1]=='H')
            {
                if(!R)RH=!RH;
                else RV=!RV;
            }
            else if(s[1]=='V')
            {
                if(!R)RV=!RV;
                else RH=!RH;
            }
        }
    }
    long long ans=314159265LL;
    for(int i=D;i<=E;i++)
        for(int j=F;j<=G;j++)
        {
            r=i,c=j;
            change(r,c);
            ans=(ans*31+S.back(r,c))%mod;
        }
    printf("%d\n",(int)ans);
    return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 3816
final_rank: 1

code_snippet: // AOJ #2343 Matrix Operation
// 2025.3.1

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int MOD = 1000000007;

#define gc() getchar_unlocked()
#define pc(c) putchar_unlocked(c)

int Cin() {  // 整数の入力
	int n = 0, c = gc();
	if (c == '-') {	c = gc();
		do n = 10*n + (c & 0xf), c = gc(); while (c >= '0');
		return -n;
	}
	do n = 10*n + (c & 0xf), c = gc(); while (c >= '0');
	return n;
}

char temp[100];
string Cins() { // 文字列の入力　スペース以下の文字で入力終了
    char *s = temp;
	do *s = gc();
	while (*s++ > ' ');
	*(s-1) = 0;
    string input(temp); // 文字配列からstringを作成
    return input;
}

void Cout(int n) { // 整数の表示（出力）
	char b[30];
	if (!n) pc('0');
	else {
		if (n < 0) pc('-'), n = -n;
		int i = 0; while (n) b[i++] = n % 10 + '0', n /= 10;
		while (i--) pc(b[i]);
	}
	pc('\n');
}

inline ll encode(int r, int c, int N) {
    return (ll)r * (N + 1LL) + c;
}

int main(){
    int N = Cin(), Q = Cin();
    int A = Cin(), B = Cin(), C = Cin(), D = Cin(), E = Cin(), F = Cin(), G = Cin();

    vector<int> rowMapping(N+1), colMapping(N+1);
    for (int i = 1; i <= N; i++) rowMapping[i] = colMapping[i] = i;

    bool swapped = false;
    bool rowRev = false;
    bool colRev = false;

    unordered_map<ll, int> cellOverride;
    cellOverride.reserve(Q * 2);

    auto getPhysical = [&](int r, int c) -> pair<int,int> {
        int pr, pc;
        if (!swapped) {
            int idxR = (rowRev ? (N - r + 1) : r);
            int idxC = (colRev ? (N - c + 1) : c);
            pr = rowMapping[idxR];
            pc = colMapping[idxC];
        } else {
            int idxR = (rowRev ? (N - c + 1) : c);
            int idxC = (colRev ? (N - r + 1) : r);
            pr = rowMapping[idxR];
            pc = colMapping[idxC];
        }
        return {pr, pc};
    };

    for (int i = 0; i < Q; i++){
        string op = Cins();
        if (op == "WR") {
            int r = Cin(), c = Cin(), v = Cin();
            auto pr_pc = getPhysical(r, c);
            ll key = encode(pr_pc.first, pr_pc.second, N);
            cellOverride[key] = v;
        }
        else if (op == "CP") {
            int r1 = Cin(), c1 = Cin(), r2 = Cin(), c2 = Cin();
            auto src = getPhysical(r1, c1);
            auto dst = getPhysical(r2, c2);
            ll keySrc = encode(src.first, src.second, N);
            int t;
            if (cellOverride.count(keySrc)) t = cellOverride[keySrc];
            else t = ( (ll)src.first * A + (ll)src.second * B ) % C;
            ll keyDst = encode(dst.first, dst.second, N);
            cellOverride[keyDst] = t;
        }
        else if (op == "SR") {
            int r1 = Cin(), r2 = Cin();
            if (!swapped) {
                int idx1 = (rowRev ? (N - r1 + 1) : r1);
                int idx2 = (rowRev ? (N - r2 + 1) : r2);
                swap(rowMapping[idx1], rowMapping[idx2]);
            } else {
                int idx1 = (colRev ? (N - r1 + 1) : r1);
                int idx2 = (colRev ? (N - r2 + 1) : r2);
                swap(colMapping[idx1], colMapping[idx2]);
            }
        }
        else if (op == "SC") {
            int c1 = Cin(), c2 = Cin();
            if (!swapped) {
                int idx1 = (colRev ? (N - c1 + 1) : c1);
                int idx2 = (colRev ? (N - c2 + 1) : c2);
                swap(colMapping[idx1], colMapping[idx2]);
            } else {
                int idx1 = (rowRev ? (N - c1 + 1) : c1);
                int idx2 = (rowRev ? (N - c2 + 1) : c2);
                swap(rowMapping[idx1], rowMapping[idx2]);
            }
        }
        else if (op == "RH") {
            if (!swapped) rowRev = !rowRev;
            else colRev = !colRev;
        }
        else if (op == "RV") {
            if (!swapped) colRev = !colRev;
            else rowRev = !rowRev;
        }
        else if (op == "RL") {
            if (!swapped) {
                swapped = true;
                colRev = !colRev;
            } else {
                swapped = false;
                rowRev = !rowRev;
            }
        }
        else if (op == "RR") {
            if (!swapped) {
                swapped = true;
                rowRev = !rowRev;
            } else {
                swapped = false;
                colRev = !colRev;
            }
        }
    }

    ll h = 314159265;
    for (int r = D; r <= E; r++){
        for (int c = F; c <= G; c++){
            auto pr_pc = getPhysical(r, c);
            int t;
            ll key = encode(pr_pc.first, pr_pc.second, N);
            if (cellOverride.count(key)) t = cellOverride[key];
            else t = ( (ll)pr_pc.first * A + (ll)pr_pc.second * B ) % C;
            h = ( (ll)31 * h + t ) % MOD;
        }
    }
    Cout(h);
    return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 4344
final_rank: 2

code_snippet: // AOJ #2343 Matrix Operation
// 2025.3.1

// AOJ #2343 Matrix Operation
// 2025.3.1

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int MOD = 1000000007;

inline ll encode(int r, int c, int N) {
    return (ll)r * (N + 1LL) + c;
}

int main(){
    ios::sync_with_stdio(0);
    cin.tie(0);

    int N, Q;
    int A, B, C;
    int D, E, F, G;
    cin >> N >> Q >> A >> B >> C >> D >> E >> F >> G;

    vector<int> rowMapping(N+1), colMapping(N+1);
    for (int i = 1; i <= N; i++){
        rowMapping[i] = i;
        colMapping[i] = i;
    }

    bool swapped = false;
    bool rowRev = false;
    bool colRev = false;

    unordered_map<ll, int> cellOverride;
    cellOverride.reserve(Q * 2);

    auto getPhysical = [&](int r, int c) -> pair<int,int> {
        int pr, pc;
        if (!swapped) {
            int idxR = (rowRev ? (N - r + 1) : r);
            int idxC = (colRev ? (N - c + 1) : c);
            pr = rowMapping[idxR];
            pc = colMapping[idxC];
        } else {
            int idxR = (rowRev ? (N - c + 1) : c);
            int idxC = (colRev ? (N - r + 1) : r);
            pr = rowMapping[idxR];
            pc = colMapping[idxC];
        }
        return {pr, pc};
    };

    for (int i = 0; i < Q; i++){
        string op;
        cin >> op;
        if (op == "WR") {
            int r, c, v;
            cin >> r >> c >> v;
            auto pr_pc = getPhysical(r, c);
            ll key = encode(pr_pc.first, pr_pc.second, N);
            cellOverride[key] = v;
        }
        else if (op == "CP") {
            int r1, c1, r2, c2;
            cin >> r1 >> c1 >> r2 >> c2;
            auto src = getPhysical(r1, c1);
            auto dst = getPhysical(r2, c2);
            ll keySrc = encode(src.first, src.second, N);
            int value;
            if (cellOverride.count(keySrc)) value = cellOverride[keySrc];
            else value = ( (ll)src.first * A + (ll)src.second * B ) % C;
            ll keyDst = encode(dst.first, dst.second, N);
            cellOverride[keyDst] = value;
        }
        else if (op == "SR") {
            int r1, r2;
            cin >> r1 >> r2;
            if (!swapped) {
                int idx1 = (rowRev ? (N - r1 + 1) : r1);
                int idx2 = (rowRev ? (N - r2 + 1) : r2);
                swap(rowMapping[idx1], rowMapping[idx2]);
            } else {
                int idx1 = (colRev ? (N - r1 + 1) : r1);
                int idx2 = (colRev ? (N - r2 + 1) : r2);
                swap(colMapping[idx1], colMapping[idx2]);
            }
        }
        else if (op == "SC") {
            int c1, c2;
            cin >> c1 >> c2;
            if (!swapped) {
                int idx1 = (colRev ? (N - c1 + 1) : c1);
                int idx2 = (colRev ? (N - c2 + 1) : c2);
                swap(colMapping[idx1], colMapping[idx2]);
            } else {
                int idx1 = (rowRev ? (N - c1 + 1) : c1);
                int idx2 = (rowRev ? (N - c2 + 1) : c2);
                swap(rowMapping[idx1], rowMapping[idx2]);
            }
        }
        else if (op == "RH") {
            if (!swapped) rowRev = !rowRev;
            else colRev = !colRev;
        }
        else if (op == "RV") {
            if (!swapped) colRev = !colRev;
            else rowRev = !rowRev;
        }
        else if (op == "RL") {
            if (!swapped) {
                swapped = true;
                colRev = !colRev;
            } else {
                swapped = false;
                rowRev = !rowRev;
            }
        }
        else if (op == "RR") {
            if (!swapped) {
                swapped = true;
                rowRev = !rowRev;
            } else {
                swapped = false;
                colRev = !colRev;
            }
        }
    }

    ll h = 314159265;
    for (int r = D; r <= E; r++){
        for (int c = F; c <= G; c++){
            auto pr_pc = getPhysical(r, c);
            int t;
            ll key = encode(pr_pc.first, pr_pc.second, N);
            if (cellOverride.count(key)) t = cellOverride[key];
            else t = ( (ll)pr_pc.first * A + (ll)pr_pc.second * B ) % C;
            h = ( (ll)31 * h + t ) % MOD;
        }
    }
    cout << h << endl;
    return 0;
}
accuracy: 1
time_ms: 20
memory_kb: 4408
final_rank: 3

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, s, t) for (int i = (int)(s); i < (int)(t); ++i)
#define revrep(i, t, s) for (int i = (int)(t) - 1; i >= (int)(s); --i)
#define all(x) begin(x), end(x)
template <typename T>
bool chmax(T& a, const T& b) {
    return a < b ? (a = b, 1) : 0;
}
template <typename T>
bool chmin(T& a, const T& b) {
    return a > b ? (a = b, 1) : 0;
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    cout << fixed << setprecision(15);

    int n, q;
    ll a, b, c;
    int d, e, f, g;
    cin >> n >> q >> a >> b >> c >> d >> e >> f >> g;
    --d, --f;

    vector<int> row(n), col(n);
    iota(all(row), 0);
    iota(all(col), 0);

    using T = pair<int, bool>;  // rotate, then horizontal flip
    auto concat = [&](T a, T b) {
        auto [dira, flipa] = a;
        auto [dirb, flipb] = b;
        int dir = dira + (flipa ? 4 - dirb : dirb);
        return make_pair(dir % 4, flipa ^ flipb);
    };
    auto rev = [&](T a) {
        auto [dir, flip] = a;
        return make_pair(flip ? dir : (4 - dir) % 4, flip);
    };
    auto apply = [&](T a, int r, int c) {
        auto [dir, flip] = a;
        rep(_, 0, dir) {
            int tmp = r;
            r = n - 1 - c;
            c = tmp;
        }
        if (flip) c = n - 1 - c;
        return make_pair(r, c);
    };
    auto apply_row = [&](T a, int rr) {
        auto [r, c] = apply(a, rr, -1);
        if (0 <= r && r < n)
            return make_pair(r, 0);
        else
            return make_pair(c, 1);
    };
    auto apply_col = [&](T a, int cc) {
        auto [r, c] = apply(a, -1, cc);
        if (0 <= r && r < n)
            return make_pair(r, 0);
        else
            return make_pair(c, 1);
    };

    map<pair<int, int>, ll> val;
    T transform = {0, 0};

    auto get_val = [&](int i, int j) {
        i = row[i];
        j = col[j];
        if (val.count({i, j})) return val[{i, j}];
        return ((i + 1) * a + (j + 1) * b) % c;
    };

    // rep(i, 0, n) {
    //     rep(j, 0, n) cout << get_val(i, j) << " ";
    //     cout << "\n";
    // }
    // cout << "\n";
    // cout << flush;

    while (q--) {
        string op;
        cin >> op;
        if (op == "WR") {
            int r, c, v;
            cin >> r >> c >> v;
            --r, --c;
            auto [rr, cc] = apply(rev(transform), r, c);
            rr = row[rr];
            cc = col[cc];
            val[{rr, cc}] = v;
        } else if (op == "CP") {
            int r1, c1, r2, c2;
            cin >> r1 >> c1 >> r2 >> c2;
            --r1, --c1, --r2, --c2;
            auto [rr1, cc1] = apply(rev(transform), r1, c1);
            auto [rr2, cc2] = apply(rev(transform), r2, c2);

            rr2 = row[rr2];
            cc2 = col[cc2];
            val[{rr2, cc2}] = get_val(rr1, cc1);
        } else if (op == "SR") {
            int r1, r2;
            cin >> r1 >> r2;
            --r1, --r2;
            auto [k1, f1] = apply_row(rev(transform), r1);
            auto [k2, f2] = apply_row(rev(transform), r2);
            if (f1 == 0) {
                swap(row[k1], row[k2]);
            } else {
                swap(col[k1], col[k2]);
            }
        } else if (op == "SC") {
            int c1, c2;
            cin >> c1 >> c2;
            --c1, --c2;
            auto [k1, f1] = apply_col(rev(transform), c1);
            auto [k2, f2] = apply_col(rev(transform), c2);
            if (f1 == 0) {
                swap(row[k1], row[k2]);
            } else {
                swap(col[k1], col[k2]);
            }
        } else if (op == "RL") {
            transform = concat(transform, {1, 0});
        } else if (op == "RR") {
            transform = concat(transform, {3, 0});
        } else if (op == "RH") {
            transform = concat(transform, {2, 1});
        } else if (op == "RV") {
            transform = concat(transform, {0, 1});
        }
        // rep(i, 0, n) {
        //     rep(j, 0, n) {
        //         auto [r, c] = apply(rev(transform), i, j);
        //         cout << get_val(r, c) << " ";
        //     }
        //     cout << "\n";
        // }
        // cout << "\n";
        // cout << flush;
    }
    ll h = 314159265;
    ll mod = 1e9 + 7;
    rep(r, d, e) rep(c, f, g) {
        auto [rr, cc] = apply(rev(transform), r, c);
        h = (31 * h + get_val(rr, cc)) % mod;
    }
    cout << h << endl;
}
accuracy: 1
time_ms: 30
memory_kb: 4144
final_rank: 5

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, s, t) for (int i = (int)(s); i < (int)(t); ++i)
#define revrep(i, t, s) for (int i = (int)(t) - 1; i >= (int)(s); --i)
#define all(x) begin(x), end(x)
template <typename T>
bool chmax(T& a, const T& b) {
    return a < b ? (a = b, 1) : 0;
}
template <typename T>
bool chmin(T& a, const T& b) {
    return a > b ? (a = b, 1) : 0;
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    cout << fixed << setprecision(15);

    int n, q;
    ll a, b, c;
    int d, e, f, g;
    cin >> n >> q >> a >> b >> c >> d >> e >> f >> g;
    --d, --f;

    vector<int> row(n), col(n);
    iota(all(row), 0);
    iota(all(col), 0);

    using T = pair<int, bool>;  // rotate, then horizontal flip
    auto concat = [&](T a, T b) {
        auto [dira, flipa] = a;
        auto [dirb, flipb] = b;
        int dir = dira + (flipa ? 4 - dirb : dirb);
        return make_pair(dir % 4, flipa ^ flipb);
    };
    auto rev = [&](T a) {
        auto [dir, flip] = a;
        return make_pair(flip ? dir : (4 - dir) % 4, flip);
    };
    auto apply = [&](T a, int r, int c) {
        auto [dir, flip] = a;
        rep(_, 0, dir) {
            int tmp = r;
            r = n - 1 - c;
            c = tmp;
        }
        if (flip) c = n - 1 - c;
        return make_pair(r, c);
    };
    auto apply_row = [&](T a, int rr) {
        auto [r, c] = apply(a, rr, -1);
        if (0 <= r && r < n)
            return make_pair(r, 0);
        else
            return make_pair(c, 1);
    };
    auto apply_col = [&](T a, int cc) {
        auto [r, c] = apply(a, -1, cc);
        if (0 <= r && r < n)
            return make_pair(r, 0);
        else
            return make_pair(c, 1);
    };

    map<pair<int, int>, ll> val;
    T transform = {0, 0};

    auto get_val = [&](int i, int j) {
        i = row[i];
        j = col[j];
        if (val.count({i, j})) return val[{i, j}];
        return ((i + 1) * a + (j + 1) * b) % c;
    };

    while (q--) {
        string op;
        cin >> op;
        if (op == "WR") {
            int r, c, v;
            cin >> r >> c >> v;
            --r, --c;
            auto [rr, cc] = apply(rev(transform), r, c);
            rr = row[rr];
            cc = col[cc];
            val[{rr, cc}] = v;
        } else if (op == "CP") {
            int r1, c1, r2, c2;
            cin >> r1 >> c1 >> r2 >> c2;
            --r1, --c1, --r2, --c2;
            auto [rr1, cc1] = apply(rev(transform), r1, c1);
            auto [rr2, cc2] = apply(rev(transform), r2, c2);

            rr2 = row[rr2];
            cc2 = col[cc2];
            val[{rr2, cc2}] = get_val(rr1, cc2);
        } else if (op == "SR") {
            int r1, r2;
            cin >> r1 >> r2;
            --r1, --r2;
            auto [k1, f1] = apply_row(rev(transform), r1);
            auto [k2, f2] = apply_row(rev(transform), r2);
            if (f1 == 0) {
                swap(row[k1], row[k2]);
            } else {
                swap(col[k1], col[k2]);
            }
        } else if (op == "SC") {
            int c1, c2;
            cin >> c1 >> c2;
            --c1, --c2;
            auto [k1, f1] = apply_col(rev(transform), c1);
            auto [k2, f2] = apply_col(rev(transform), c2);
            if (f1 == 0) {
                swap(row[k1], row[k2]);
            } else {
                swap(col[k1], col[k2]);
            }
        } else if (op == "RL") {
            transform = concat(transform, {1, 0});
        } else if (op == "RR") {
            transform = concat(transform, {3, 0});
        } else if (op == "RH") {
            transform = concat(transform, {2, 1});
        } else if (op == "RV") {
            transform = concat(transform, {0, 1});
        }
    }
    ll h = 314159265;
    ll mod = 1e9 + 7;
    rep(r, d, e) rep(c, f, g) {
        auto [rr, cc] = apply(rev(transform), r, c);
        h = (31 * h + get_val(rr, cc)) % mod;
    }
    cout << h << endl;
}
accuracy: 0.0364963503649635
time_ms: 30
memory_kb: 4000
final_rank: 11

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, s, t) for (int i = (int)(s); i < (int)(t); ++i)
#define revrep(i, t, s) for (int i = (int)(t) - 1; i >= (int)(s); --i)
#define all(x) begin(x), end(x)
template <typename T>
bool chmax(T& a, const T& b) {
    return a < b ? (a = b, 1) : 0;
}
template <typename T>
bool chmin(T& a, const T& b) {
    return a > b ? (a = b, 1) : 0;
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    cout << fixed << setprecision(15);

    int n, q;
    ll a, b, c;
    int d, e, f, g;
    cin >> n >> q >> a >> b >> c >> d >> e >> f >> g;
    --d, --f;

    vector<int> row(n), col(n);
    iota(all(row), 0);
    iota(all(col), 0);

    using T = pair<int, bool>;  // rotate, then horizontal flip
    auto concat = [&](T a, T b) {
        auto [dira, flipa] = a;
        auto [dirb, flipb] = b;
        int dir = dira + (flipa ? 4 - dirb : dirb);
        return make_pair(dir % 4, flipa ^ flipb);
    };
    auto rev = [&](T a) {
        auto [dir, flip] = a;
        return make_pair(flip ? dir : (4 - dir) % 4, flip);
    };
    auto apply = [&](T a, int r, int c) {
        auto [dir, flip] = a;
        rep(_, 0, dir) {
            int tmp = r;
            r = n - 1 - c;
            c = tmp;
        }
        if (flip) c = n - 1 - c;
        return make_pair(r, c);
    };
    auto apply_row = [&](T a, int rr) {
        auto [r, c] = apply(a, rr, -1);
        if (0 <= r && r < n)
            return make_pair(r, 0);
        else
            return make_pair(c, 1);
    };
    auto apply_col = [&](T a, int cc) {
        auto [r, c] = apply(a, -1, cc);
        if (0 <= r && r < n)
            return make_pair(r, 0);
        else
            return make_pair(c, 1);
    };

    map<pair<int, int>, ll> val;
    T transform = {0, 0};

    auto get_val = [&](int i, int j) {
        i = row[i];
        j = col[j];
        if (val.count({i, j})) return val[{i, j}];
        return ((i + 1) * a + (j + 1) * b) % c;
    };

    while (q--) {
        string op;
        cin >> op;
        if (op == "WR") {
            int r, c, v;
            cin >> r >> c >> v;
            --r, --c;
            auto [rr, cc] = apply(rev(transform), r, c);
            val[{rr, cc}] = v;
        } else if (op == "CP") {
            int r1, c1, r2, c2;
            cin >> r1 >> c1 >> r2 >> c2;
            --r1, --c1, --r2, --c2;
            auto [rr1, cc1] = apply(rev(transform), r1, c1);
            auto [rr2, cc2] = apply(rev(transform), r2, c2);

            rr2 = row[rr2];
            cc2 = col[cc2];
            val[{rr2, cc2}] = get_val(rr1, cc2);
        } else if (op == "SR") {
            int r1, r2;
            cin >> r1 >> r2;
            --r1, --r2;
            auto [k1, f1] = apply_row(rev(transform), r1);
            auto [k2, f2] = apply_row(rev(transform), r2);
            if (f1 == 0) {
                swap(row[k1], row[k2]);
            } else {
                swap(col[k1], col[k2]);
            }
        } else if (op == "SC") {
            int c1, c2;
            cin >> c1 >> c2;
            --c1, --c2;
            auto [k1, f1] = apply_col(rev(transform), c1);
            auto [k2, f2] = apply_col(rev(transform), c2);
            if (f1 == 0) {
                swap(row[k1], row[k2]);
            } else {
                swap(col[k1], col[k2]);
            }
        } else if (op == "RL") {
            transform = concat(transform, {1, 0});
        } else if (op == "RR") {
            transform = concat(transform, {3, 0});
        } else if (op == "RH") {
            transform = concat(transform, {1, 1});
            transform = concat(transform, {3, 0});
        } else if (op == "RV") {
            transform = concat(transform, {0, 1});
        }
    }
    ll h = 314159265;
    ll mod = 1e9 + 7;
    rep(r, d, e) rep(c, f, g) {
        auto [rr, cc] = apply(rev(transform), r, c);
        h = (31 * h + get_val(rr, cc)) % mod;
    }
    cout << h << endl;
}
accuracy: 0.0364963503649635
time_ms: 30
memory_kb: 3992
final_rank: 10

code_snippet: #include <bits/stdc++.h>
using namespace std;

struct cell {
	int r, c;
	bool operator<(const cell& p) const {
		return r != p.r ? r < p.r : c < p.c;
	}
};

int main() {
	cin.tie(0);
	ios::sync_with_stdio(false);
	int N, Q, A, B, C, D, E, F, G;
	cin >> N >> Q >> A >> B >> C >> D >> E >> F >> G;
	D -= 1;
	F -= 1;
	vector<int> pr(N), pc(N);
	for (int i = 0; i < N; i++) {
		pr[i] = i;
		pc[i] = i;
	}
	vector<int> ori = { 0, 1, 2, 3 };
	auto orient_cell = [&](const cell& p) -> cell {
		vector<int> inv(4);
		for (int i = 0; i < 4; i++) {
			inv[ori[i]] = i;
		}
		cell res = p;
		if (inv[0] & 1) {
			res.c = N - res.c - 1;
		}
		if (inv[0] & 2) {
			res.r = N - res.r - 1;
		}
		if ((inv[0] ^ inv[1]) == 2) {
			swap(res.r, res.c);
		}
		return res;
	};
	auto inside = [&](int x) {
		return 0 <= x && x < N;
	};
	auto initial_cell = [&](const cell& p) -> cell {
		cell res = orient_cell(p);
		return cell{pr[res.r], pc[res.c]};
	};
	map<cell, int> dat;
	auto value_cell = [&](const cell& p) -> int {
		cell pt = initial_cell(p);
		if (dat.find(pt) != dat.end()) {
			return dat[pt];
		}
		return (1LL * (pt.r + 1) * A + 1LL * (pt.c + 1) * B) % C;
	};
	for (int i = 0; i < Q; i++) {
		string com;
		cin >> com;
		if (com == "WR") {
			cell p; int v;
			cin >> p.r >> p.c >> v;
			p.r -= 1;
			p.c -= 1;
			p = initial_cell(p);
			dat[p] = v;
		}
		if (com == "CP") {
			cell p1, p2;
			cin >> p1.r >> p1.c >> p2.r >> p2.c;
			p1.r -= 1;
			p1.c -= 1;
			p2.r -= 1;
			p2.c -= 1;
			int v = value_cell(p1);
			p2 = initial_cell(p2);
			dat[p2] = v;
		}
		if (com == "SR") {
			int r1, r2;
			cin >> r1 >> r2;
			r1 -= 1;
			r2 -= 1;
			cell p1 = orient_cell(cell{r1, -1});
			cell p2 = orient_cell(cell{r2, -1});
			if (inside(p1.r)) {
				swap(pr[p1.r], pr[p2.r]);
			}
			else {
				swap(pc[p1.c], pc[p2.c]);
			}
		}
		if (com == "SC") {
			int c1, c2;
			cin >> c1 >> c2;
			c1 -= 1;
			c2 -= 1;
			cell p1 = orient_cell(cell{-1, c1});
			cell p2 = orient_cell(cell{-1, c2});
			if (inside(p1.r)) {
				swap(pr[p1.r], pr[p2.r]);
			}
			else {
				swap(pc[p1.c], pc[p2.c]);
			}
		}
		if (com == "RL") {
			ori = { ori[1], ori[3], ori[0], ori[2] };
		}
		if (com == "RR") {
			ori = { ori[2], ori[0], ori[3], ori[1] };
		}
		if (com == "RH") {
			ori = { ori[2], ori[3], ori[0], ori[1] };
		}
		if (com == "RV") {
			ori = { ori[1], ori[0], ori[3], ori[2] };
		}
	}
	int ans = 314159265;
	for (int i = D; i < E; i++) {
		for (int j = F; j < G; j++) {
			int v = value_cell(cell{i, j});
			ans = (ans * 31LL + v) % 1000000007;
		}
	}
	cout << ans << endl;
	return 0;
}
accuracy: 1
time_ms: 40
memory_kb: 3932
final_rank: 9

code_snippet: #include <bits/stdc++.h>

#ifdef _MSC_VER
#  include <intrin.h>
#else
#  include <x86intrin.h>
#endif

#include <limits>
#include <type_traits>

namespace suisen {
// ! utility
template <typename ...Types>
using constraints_t = std::enable_if_t<std::conjunction_v<Types...>, std::nullptr_t>;
template <bool cond_v, typename Then, typename OrElse>
constexpr decltype(auto) constexpr_if(Then&& then, OrElse&& or_else) {
    if constexpr (cond_v) {
        return std::forward<Then>(then);
    } else {
        return std::forward<OrElse>(or_else);
    }
}

// ! function
template <typename ReturnType, typename Callable, typename ...Args>
using is_same_as_invoke_result = std::is_same<std::invoke_result_t<Callable, Args...>, ReturnType>;
template <typename F, typename T>
using is_uni_op = is_same_as_invoke_result<T, F, T>;
template <typename F, typename T>
using is_bin_op = is_same_as_invoke_result<T, F, T, T>;

template <typename Comparator, typename T>
using is_comparator = std::is_same<std::invoke_result_t<Comparator, T, T>, bool>;

// ! integral
template <typename T, typename = constraints_t<std::is_integral<T>>>
constexpr int bit_num = std::numeric_limits<std::make_unsigned_t<T>>::digits;
template <typename T, unsigned int n>
struct is_nbit { static constexpr bool value = bit_num<T> == n; };
template <typename T, unsigned int n>
static constexpr bool is_nbit_v = is_nbit<T, n>::value;

// ?
template <typename T>
struct safely_multipliable {};
template <>
struct safely_multipliable<int> { using type = long long; };
template <>
struct safely_multipliable<long long> { using type = __int128_t; };
template <>
struct safely_multipliable<unsigned int> { using type = unsigned long long; };
template <>
struct safely_multipliable<unsigned long int> { using type = __uint128_t; };
template <>
struct safely_multipliable<unsigned long long> { using type = __uint128_t; };
template <>
struct safely_multipliable<float> { using type = float; };
template <>
struct safely_multipliable<double> { using type = double; };
template <>
struct safely_multipliable<long double> { using type = long double; };
template <typename T>
using safely_multipliable_t = typename safely_multipliable<T>::type;

template <typename T, typename = void>
struct rec_value_type {
    using type = T;
};
template <typename T>
struct rec_value_type<T, std::void_t<typename T::value_type>> {
    using type = typename rec_value_type<typename T::value_type>::type;
};
template <typename T>
using rec_value_type_t = typename rec_value_type<T>::type;

} // namespace suisen

// ! type aliases
using i128 = __int128_t;
using u128 = __uint128_t;

template <typename T>
using pq_greater = std::priority_queue<T, std::vector<T>, std::greater<T>>;
template <typename T, typename U>
using umap = std::unordered_map<T, U>;

// ! macros (capital: internal macro)
#define OVERLOAD2(_1,_2,name,...) name
#define OVERLOAD3(_1,_2,_3,name,...) name
#define OVERLOAD4(_1,_2,_3,_4,name,...) name

#define REP4(i,l,r,s)  for(std::remove_reference_t<std::remove_const_t<decltype(r)>>i=(l);i<(r);i+=(s))
#define REP3(i,l,r)    REP4(i,l,r,1)
#define REP2(i,n)      REP3(i,0,n)
#define REPINF3(i,l,s) for(std::remove_reference_t<std::remove_const_t<decltype(l)>>i=(l);;i+=(s))
#define REPINF2(i,l)   REPINF3(i,l,1)
#define REPINF1(i)     REPINF2(i,0)
#define RREP4(i,l,r,s) for(std::remove_reference_t<std::remove_const_t<decltype(r)>>i=(l)+fld((r)-(l)-1,s)*(s);i>=(l);i-=(s))
#define RREP3(i,l,r)   RREP4(i,l,r,1)
#define RREP2(i,n)     RREP3(i,0,n)

#define rep(...)    OVERLOAD4(__VA_ARGS__, REP4   , REP3   , REP2   )(__VA_ARGS__)
#define rrep(...)   OVERLOAD4(__VA_ARGS__, RREP4  , RREP3  , RREP2  )(__VA_ARGS__)
#define repinf(...) OVERLOAD3(__VA_ARGS__, REPINF3, REPINF2, REPINF1)(__VA_ARGS__)

#define CAT_I(a, b) a##b
#define CAT(a, b) CAT_I(a, b)
#define UNIQVAR(tag) CAT(tag, __LINE__)
#define loop(n) for (std::remove_reference_t<std::remove_const_t<decltype(n)>> UNIQVAR(loop_variable) = n; UNIQVAR(loop_variable) --> 0;)

#define all(iterable) std::begin(iterable), std::end(iterable)
#define input(type, ...) type __VA_ARGS__; read(__VA_ARGS__)

#ifdef LOCAL
#  define debug(...) debug_internal(#__VA_ARGS__, __VA_ARGS__)

template <class T, class... Args>
void debug_internal(const char* s, T&& first, Args&&... args) {
    constexpr const char* prefix = "[\033[32mDEBUG\033[m] ";
    constexpr const char* open_brakets = sizeof...(args) == 0 ? "" : "(";
    constexpr const char* close_brakets = sizeof...(args) == 0 ? "" : ")";
    std::cerr << prefix << open_brakets << s << close_brakets << ": " << open_brakets << std::forward<T>(first);
    ((std::cerr << ", " << std::forward<Args>(args)), ...);
    std::cerr << close_brakets << "\n";
}

#else
#  define debug(...) void(0)
#endif

// ! I/O utilities

// __int128_t
std::ostream& operator<<(std::ostream& dest, __int128_t value) {
    std::ostream::sentry s(dest);
    if (s) {
        __uint128_t tmp = value < 0 ? -value : value;
        char buffer[128];
        char* d = std::end(buffer);
        do {
            --d;
            *d = "0123456789"[tmp % 10];
            tmp /= 10;
        } while (tmp != 0);
        if (value < 0) {
            --d;
            *d = '-';
        }
        int len = std::end(buffer) - d;
        if (dest.rdbuf()->sputn(d, len) != len) {
            dest.setstate(std::ios_base::badbit);
        }
    }
    return dest;
}
// __uint128_t
std::ostream& operator<<(std::ostream& dest, __uint128_t value) {
    std::ostream::sentry s(dest);
    if (s) {
        char buffer[128];
        char* d = std::end(buffer);
        do {
            --d;
            *d = "0123456789"[value % 10];
            value /= 10;
        } while (value != 0);
        int len = std::end(buffer) - d;
        if (dest.rdbuf()->sputn(d, len) != len) {
            dest.setstate(std::ios_base::badbit);
        }
    }
    return dest;
}

// pair
template <typename T, typename U>
std::ostream& operator<<(std::ostream& out, const std::pair<T, U>& a) {
    return out << a.first << ' ' << a.second;
}
// tuple
template <unsigned int N = 0, typename ...Args>
std::ostream& operator<<(std::ostream& out, const std::tuple<Args...>& a) {
    if constexpr (N >= std::tuple_size_v<std::tuple<Args...>>) {
        return out;
    } else {
        out << std::get<N>(a);
        if constexpr (N + 1 < std::tuple_size_v<std::tuple<Args...>>) {
            out << ' ';
        }
        return operator<<<N + 1>(out, a);
    }
}
// vector
template <typename T>
std::ostream& operator<<(std::ostream& out, const std::vector<T>& a) {
    for (auto it = a.begin(); it != a.end();) {
        out << *it;
        if (++it != a.end()) out << ' ';
    }
    return out;
}
// array
template <typename T, size_t N>
std::ostream& operator<<(std::ostream& out, const std::array<T, N>& a) {
    for (auto it = a.begin(); it != a.end();) {
        out << *it;
        if (++it != a.end()) out << ' ';
    }
    return out;
}
inline void print() { std::cout << '\n'; }
template <typename Head, typename... Tail>
inline void print(const Head& head, const Tail &...tails) {
    std::cout << head;
    if (sizeof...(tails)) std::cout << ' ';
    print(tails...);
}
template <typename Iterable>
auto print_all(const Iterable& v, std::string sep = " ", std::string end = "\n") -> decltype(std::cout << *v.begin(), void()) {
    for (auto it = v.begin(); it != v.end();) {
        std::cout << *it;
        if (++it != v.end()) std::cout << sep;
    }
    std::cout << end;
}

__int128_t parse_i128(std::string& s) {
    __int128_t ret = 0;
    for (int i = 0; i < int(s.size()); i++) if ('0' <= s[i] and s[i] <= '9') ret = 10 * ret + s[i] - '0';
    if (s[0] == '-') ret = -ret;
    return ret;
}
__uint128_t parse_u128(std::string& s) {
    __uint128_t ret = 0;
    for (int i = 0; i < int(s.size()); i++) if ('0' <= s[i] and s[i] <= '9') ret = 10 * ret + s[i] - '0';
    return ret;
}
// __int128_t
std::istream& operator>>(std::istream& in, __int128_t& v) {
    std::string s;
    in >> s;
    v = parse_i128(s);
    return in;
}
// __uint128_t
std::istream& operator>>(std::istream& in, __uint128_t& v) {
    std::string s;
    in >> s;
    v = parse_u128(s);
    return in;
}
// pair
template <typename T, typename U>
std::istream& operator>>(std::istream& in, std::pair<T, U>& a) {
    return in >> a.first >> a.second;
}
// tuple
template <unsigned int N = 0, typename ...Args>
std::istream& operator>>(std::istream& in, std::tuple<Args...>& a) {
    if constexpr (N >= std::tuple_size_v<std::tuple<Args...>>) {
        return in;
    } else {
        return operator>><N + 1>(in >> std::get<N>(a), a);
    }
}
// vector
template <typename T>
std::istream& operator>>(std::istream& in, std::vector<T>& a) {
    for (auto it = a.begin(); it != a.end(); ++it) in >> *it;
    return in;
}
// array
template <typename T, size_t N>
std::istream& operator>>(std::istream& in, std::array<T, N>& a) {
    for (auto it = a.begin(); it != a.end(); ++it) in >> *it;
    return in;
}
template <typename ...Args>
void read(Args &...args) {
    (std::cin >> ... >> args);
}

// ! integral utilities

// Returns pow(-1, n)
template <typename T>
constexpr inline int pow_m1(T n) {
    return -(n & 1) | 1;
}
// Returns pow(-1, n)
template <>
constexpr inline int pow_m1<bool>(bool n) {
    return -int(n) | 1;
}

// Returns floor(x / y)
template <typename T>
constexpr inline T fld(const T x, const T y) {
    return (x ^ y) >= 0 ? x / y : (x - (y + pow_m1(y >= 0))) / y;
}
template <typename T>
constexpr inline T cld(const T x, const T y) {
    return (x ^ y) <= 0 ? x / y : (x + (y + pow_m1(y >= 0))) / y;
}

template <typename T, suisen::constraints_t<suisen::is_nbit<T, 16>> = nullptr>
__attribute__((target("popcnt"))) constexpr inline int popcount(const T x) { return _mm_popcnt_u32(x); }
template <typename T, suisen::constraints_t<suisen::is_nbit<T, 32>> = nullptr>
__attribute__((target("popcnt"))) constexpr inline int popcount(const T x) { return _mm_popcnt_u32(x); }
template <typename T, suisen::constraints_t<suisen::is_nbit<T, 64>> = nullptr>
__attribute__((target("popcnt"))) constexpr inline int popcount(const T x) { return _mm_popcnt_u64(x); }
template <typename T, suisen::constraints_t<suisen::is_nbit<T, 16>> = nullptr>
constexpr inline int count_lz(const T x) { return x ? __builtin_clz(x) : suisen::bit_num<T>; }
template <typename T, suisen::constraints_t<suisen::is_nbit<T, 32>> = nullptr>
constexpr inline int count_lz(const T x) { return x ? __builtin_clz(x) : suisen::bit_num<T>; }
template <typename T, suisen::constraints_t<suisen::is_nbit<T, 64>> = nullptr>
constexpr inline int count_lz(const T x) { return x ? __builtin_clzll(x) : suisen::bit_num<T>; }
template <typename T, suisen::constraints_t<suisen::is_nbit<T, 16>> = nullptr>
constexpr inline int count_tz(const T x) { return x ? __builtin_ctz(x) : suisen::bit_num<T>; }
template <typename T, suisen::constraints_t<suisen::is_nbit<T, 32>> = nullptr>
constexpr inline int count_tz(const T x) { return x ? __builtin_ctz(x) : suisen::bit_num<T>; }
template <typename T, suisen::constraints_t<suisen::is_nbit<T, 64>> = nullptr>
constexpr inline int count_tz(const T x) { return x ? __builtin_ctzll(x) : suisen::bit_num<T>; }
template <typename T>
constexpr inline int floor_log2(const T x) { return suisen::bit_num<T> -1 - count_lz(x); }
template <typename T>
constexpr inline int ceil_log2(const T x) { return floor_log2(x) + ((x & -x) != x); }
template <typename T>
constexpr inline int kth_bit(const T x, const unsigned int k) { return (x >> k) & 1; }
template <typename T>
constexpr inline int parity(const T x) { return popcount(x) & 1; }

// ! container

template <typename T, typename Comparator, suisen::constraints_t<suisen::is_comparator<Comparator, T>> = nullptr>
auto priqueue_comp(const Comparator comparator) {
    return std::priority_queue<T, std::vector<T>, Comparator>(comparator);
}

template <typename Iterable>
auto isize(const Iterable& iterable) -> decltype(int(iterable.size())) {
    return iterable.size();
}

template <typename T, typename Gen, suisen::constraints_t<suisen::is_same_as_invoke_result<T, Gen, int>> = nullptr>
auto generate_vector(int n, Gen generator) {
    std::vector<T> v(n);
    for (int i = 0; i < n; ++i) v[i] = generator(i);
    return v;
}
template <typename T>
auto generate_range_vector(T l, T r) {
    return generate_vector(r - l, [l](int i) { return l + i; });
}
template <typename T>
auto generate_range_vector(T n) {
    return generate_range_vector(0, n);
}

template <typename T>
void sort_unique_erase(std::vector<T>& a) {
    std::sort(a.begin(), a.end());
    a.erase(std::unique(a.begin(), a.end()), a.end());
}

template <typename InputIterator, typename BiConsumer>
auto foreach_adjacent_values(InputIterator first, InputIterator last, BiConsumer f) -> decltype(f(*first++, *last), void()) {
    if (first != last) for (auto itr = first, itl = itr++; itr != last; itl = itr++) f(*itl, *itr);
}
template <typename Container, typename BiConsumer>
auto foreach_adjacent_values(Container c, BiConsumer f) -> decltype(c.begin(), c.end(), void()) {
    foreach_adjacent_values(c.begin(), c.end(), f);
}

// ! other utilities

// x <- min(x, y). returns true iff `x` has chenged.
template <typename T>
inline bool chmin(T& x, const T& y) {
    if (y >= x) return false;
    x = y;
    return true;
}
// x <- max(x, y). returns true iff `x` has chenged.
template <typename T>
inline bool chmax(T& x, const T& y) {
    if (y <= x) return false;
    x = y;
    return true;
}

template <typename T, std::enable_if_t<std::is_integral_v<T>, std::nullptr_t> = nullptr>
std::string bin(T val, int bit_num = -1) {
    std::string res;
    if (bit_num >= 0) {
        for (int bit = bit_num; bit-- > 0;) res += '0' + ((val >> bit) & 1);
    } else {
        for (; val; val >>= 1) res += '0' + (val & 1);
        std::reverse(res.begin(), res.end());
    }
    return res;
}

template <typename T, std::enable_if_t<std::is_integral_v<T>, std::nullptr_t> = nullptr>
std::vector<T> digits_low_to_high(T val, T base = 10) {
    std::vector<T> res;
    for (; val; val /= base) res.push_back(val % base);
    if (res.empty()) res.push_back(T{ 0 });
    return res;
}
template <typename T, std::enable_if_t<std::is_integral_v<T>, std::nullptr_t> = nullptr>
std::vector<T> digits_high_to_low(T val, T base = 10) {
    auto res = digits_low_to_high(val, base);
    std::reverse(res.begin(), res.end());
    return res;
}

template <typename T>
std::string join(const std::vector<T>& v, const std::string& sep, const std::string& end) {
    std::ostringstream ss;
    for (auto it = v.begin(); it != v.end();) {
        ss << *it;
        if (++it != v.end()) ss << sep;
    }
    ss << end;
    return ss.str();
}

namespace suisen {}
using namespace suisen;
using namespace std;

struct io_setup {
    io_setup(int precision = 20) {
        std::ios::sync_with_stdio(false);
        std::cin.tie(nullptr);
        std::cout << std::fixed << std::setprecision(precision);
    }
} io_setup_ {};

// ! code from here

int main() {
    input(int, n, q);
    input(int, a, b, c);

    auto default_value = [&](long long i, long long j) -> int {
        ++i, ++j;
        return (i * a + j * b) % c;
    };
    map<pair<int, int>, int> updated;

    vector<int> row(n), col(n);
    iota(all(row), 0);
    iota(all(col), 0);

    input(int, row_l, row_r, col_l, col_r);
    --row_l, --col_l;

    /**
     * 0   3
     * 
     * 1   2
     */

    int row_fr = 0, row_to = 1;
    int col_fr = 0, col_to = 3;

    auto is_row_h = [&]{
        auto [rl, rr] = minmax(row_fr, row_to);
        return (rl == 0 and rr == 3) or (rl == 1 and rr == 2);
    };
    auto is_row_v = [&] {
        return not is_row_h();
    };

    auto get_ids = [&](int i, int j) {
        if (is_row_h()) {
            swap(i, j);
            if (row_fr == 2 or row_fr == 3) i = n - i - 1;
            if (col_fr == 1 or col_fr == 2) j = n - j - 1;
            return make_pair(row[i], col[j]);
        } else {
            if (row_fr == 1 or row_fr == 2) i = n - i - 1;
            if (col_fr == 2 or col_fr == 3) j = n - j - 1;
            return make_pair(row[i], col[j]);
        }
    };
    auto get_value = [&](int i, int j) {
        tie(i, j) = get_ids(i, j);
        if (auto it = updated.find({ i, j }); it != updated.end()) {
            return it->second;
        } else {
            return default_value(i, j);
        }
    };

    array<int, 4> RL { 1, 2, 3, 0 };
    array<int, 4> RR { 3, 0, 1, 2 };

    auto dbg = [&]{
        // cerr << "DEBUG" << endl;
        // for (int i = 0; i < n; ++i) {
        //     for (int j = 0; j < n; ++j) {
        //         cerr << get_value(i, j) << " ";
        //     }
        //     cerr << endl;
        // }
    };

    dbg();
    loop(q) {
        input(string, op);
        if (op == "WR") {
            input(int, i, j, v);
            --i, --j;
            tie(i, j) = get_ids(i, j);
            updated[{ i, j }] = v;
        } else if (op == "CP") {
            input(int, i1, j1, i2, j2);
            --i1, --j1, --i2, --j2;
            tie(i2, j2) = get_ids(i2, j2);
            updated[{ i2, j2 }] = get_value(i1, j1);
        } else if (op == "SR") {
            input(int, i1, i2);
            --i1, --i2;
            if (is_row_v()) {
                if (row_fr == 1 or row_fr == 2) {
                    i1 = n - 1 - i1;
                    i2 = n - 1 - i2;
                }
                swap(row[i1], row[i2]);
            } else {
                if (col_fr == 1 or col_fr == 2) {
                    i1 = n - 1 - i1;
                    i2 = n - 1 - i2;
                }
                swap(col[i1], col[i2]);
            }
        } else if (op == "SC") {
            input(int, i1, i2);
            --i1, --i2;
            if (is_row_h()) {
                if (row_fr == 2 or row_fr == 3) {
                    i1 = n - 1 - i1;
                    i2 = n - 1 - i2;
                }
                swap(row[i1], row[i2]);
            } else {
                if (col_fr == 2 or col_fr == 3) {
                    i1 = n - 1 - i1;
                    i2 = n - 1 - i2;
                }
                swap(col[i1], col[i2]);
            }
        } else if (op == "RL") {
            row_fr = RL[row_fr], row_to = RL[row_to], col_fr = RL[col_fr], col_to = RL[col_to];
        } else if (op == "RR") {
            row_fr = RR[row_fr], row_to = RR[row_to], col_fr = RR[col_fr], col_to = RR[col_to];
        } else if (op == "RV") {
            if (is_row_h()) {
                swap(row_fr, row_to);
            } else {
                swap(col_fr, col_to);
            }
        } else if (op == "RH") {
            if (is_row_v()) {
                swap(row_fr, row_to);
            } else {
                swap(col_fr, col_to);
            }
        }
        dbg();
    }

    long long h = 314159265;
    for (int i = row_l; i < row_r; ++i) for (int j = col_l; j < col_r; ++j) {
        h = (31 * h + get_value(i, j)) % 1000000007;
    }
    print(h);
    return 0;
}
accuracy: 1
time_ms: 30
memory_kb: 4136
final_rank: 4

code_snippet: #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return true; } return false; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return true; } return false; }
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
#define mp make_pair
#define si(x) int(x.size())
const int mod=1000000007,MAX=300005,INF=1<<30;

int main(){
    
    std::ifstream in("text.txt");
    std::cin.rdbuf(in.rdbuf());
    cin.tie(0);
    ios::sync_with_stdio(false);
    
    int N,Q;cin>>N>>Q;
    ll A,B,C,D,E,F,G;cin>>A>>B>>C>>D>>E>>F>>G;D--;E--;F--;G--;
    vector<int> H(N),W(N),invH(N),invW(N);
    iota(all(H),0);
    iota(all(W),0);
    iota(all(invH),0);
    iota(all(invW),0);
    bool f=false,g=false,z=false;
    map<pair<int,int>,ll> MA;
    
    auto inv=[&](int h,int w){
        //cout<<h<<" "<<w<<" ";
        if(z) swap(h,w);
        
        if(f) h=N-1-h;
        h=invH[h];
        if(g) w=N-1-w;
        w=invW[w];
        //cout<<h<<" "<<w<<endl;
        //if(z) swap(h,w);
        return mp(h,w);
    };
    
    while(Q--){
        string S;cin>>S;
        if(S=="WR"){
            int h,w,v;cin>>h>>w>>v;h--;w--;
            tie(h,w)=inv(h,w);
            MA[mp(h,w)]=v;
        }
        if(S=="CP"){
            int h1,w1,h2,w2;cin>>h1>>w1>>h2>>w2;
            h1--;w1--;h2--;w2--;
            tie(h1,w1)=inv(h1,w1);
            tie(h2,w2)=inv(h2,w2);
            ll v;
            if(MA.count(mp(h1,w1))) v=MA[mp(h1,w1)];
            else v=(A*(h1+1)+B*(w1+1))%C;
            MA[mp(h2,w2)]=v;
        }
        if(S=="SR"){
            int h1,h2;cin>>h1>>h2;h1--;h2--;
            if(z){
                if(g){
                    h1=N-1-h1;
                    h2=N-1-h2;
                }
                swap(invW[h1],invW[h2]);
                swap(W[invW[h1]],W[invW[h2]]);
            }else{
                if(f){
                    h1=N-1-h1;
                    h2=N-1-h2;
                }
                swap(invH[h1],invH[h2]);
                swap(H[invH[h1]],H[invH[h2]]);
            }
        }
        if(S=="SC"){
            int w1,w2;cin>>w1>>w2;w1--;w2--;
            if(z){
                if(f){
                    w1=N-1-w1;
                    w2=N-1-w2;
                }
                swap(invH[w1],invH[w2]);
                swap(H[invH[w1]],H[invH[w2]]);
            }else{
                if(g){
                    w1=N-1-w1;
                    w2=N-1-w2;
                }
                swap(invW[w1],invW[w2]);
                swap(W[invW[w1]],W[invW[w2]]);
            }
        }
        if(S=="RL"){
            if(!z) g=!g;
            else f=!f;
            //swap(H,W);
            //swap(invH,invW);
            z=!z;
        }
        if(S=="RR"){
            if(!z) f=!f;
            else g=!g;
            //swap(H,W);
            //swap(invH,invW);
            z=!z;
        }
        if(S=="RH"){
            if(z) g=!g;
            else f=!f;
        }
        if(S=="RV"){
            if(z) f=!f;
            else g=!g;
        }
        
        //cout<<f<<" "<<g<<" "<<z<<endl;
    }
    
    //cout<<f<<" "<<g<<" "<<z<<endl;
    
    ll ans=314159265;
    
    for(int r=D;r<=E;r++){
        for(int c=F;c<=G;c++){
            int h=r,w=c;
            tie(h,w)=inv(h,w);
            //cout<<h<<" "<<w<<endl;
            ll v;
            if(MA.count(mp(h,w))) v=MA[mp(h,w)];
            else v=(A*(h+1)+B*(w+1))%C;
            ans=(ans*31+v)%mod;
        }
    }
    
    cout<<ans<<"\n";
}
accuracy: 1
time_ms: 30
memory_kb: 4316
final_rank: 6

code_snippet: #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return true; } return false; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return true; } return false; }
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
#define mp make_pair
#define si(x) int(x.size())
const int mod=1000000007,MAX=300005,INF=1<<30;

int main(){
    
    std::ifstream in("text.txt");
    std::cin.rdbuf(in.rdbuf());
    cin.tie(0);
    ios::sync_with_stdio(false);
    
    int N,Q;cin>>N>>Q;
    ll A,B,C,D,E,F,G;cin>>A>>B>>C>>D>>E>>F>>G;D--;E--;F--;G--;
    vector<int> H(N),W(N),invH(N),invW(N);
    iota(all(H),0);
    iota(all(W),0);
    iota(all(invH),0);
    iota(all(invW),0);
    bool f=false,g=false,z=false;
    map<pair<int,int>,ll> MA;
    
    auto inv=[&](int h,int w){
        //cout<<h<<" "<<w<<" ";
        if(z) swap(h,w);
        
        if(f) h=N-1-h;
        h=invH[h];
        if(g) w=N-1-w;
        w=invW[w];
        //cout<<h<<" "<<w<<endl;
        //if(z) swap(h,w);
        return mp(h,w);
    };
    
    while(Q--){
        string S;cin>>S;
        if(S=="WR"){
            int h,w,v;cin>>h>>w>>v;h--;w--;
            tie(h,w)=inv(h,w);
            MA[mp(h,w)]=v;
        }
        if(S=="CP"){
            int h1,w1,h2,w2;cin>>h1>>w1>>h2>>w2;
            h1--;w1--;h2--;w2--;
            tie(h1,w1)=inv(h1,w1);
            tie(h2,w2)=inv(h2,w2);
            ll v;
            if(MA.count(mp(h1,w1))) v=MA[mp(h1,w1)];
            else v=(A*(h1+1)+B*(w1+1))%C;
            MA[mp(h2,w2)]=v;
        }
        if(S=="SR"){
            int h1,h2;cin>>h1>>h2;h1--;h2--;
            if(f){
                h1=N-1-h1;
                h2=N-1-h2;
            }
            if(z){
                if(g){
                    h1=N-1-h1;
                    h2=N-1-h2;
                }
                swap(invW[h1],invW[h2]);
                swap(W[invW[h1]],W[invW[h2]]);
            }else{
                if(f){
                    h1=N-1-h1;
                    h2=N-1-h2;
                }
                swap(invH[h1],invH[h2]);
                swap(H[invH[h1]],H[invH[h2]]);
            }
        }
        if(S=="SC"){
            int w1,w2;cin>>w1>>w2;w1--;w2--;
            if(z){
                if(f){
                    w1=N-1-w1;
                    w2=N-1-w2;
                }
                swap(invH[w1],invH[w2]);
                swap(H[invH[w1]],H[invH[w2]]);
            }else{
                if(g){
                    w1=N-1-w1;
                    w2=N-1-w2;
                }
                swap(invW[w1],invW[w2]);
                swap(W[invW[w1]],W[invW[w2]]);
            }
        }
        if(S=="RL"){
            if(!z) g=!g;
            else f=!f;
            //swap(H,W);
            //swap(invH,invW);
            z=!z;
        }
        if(S=="RR"){
            if(!z) f=!f;
            else g=!g;
            //swap(H,W);
            //swap(invH,invW);
            z=!z;
        }
        if(S=="RH"){
            if(z) g=!g;
            else f=!f;
        }
        if(S=="RV"){
            if(z) f=!f;
            else g=!g;
        }
        
        //cout<<f<<" "<<g<<" "<<z<<endl;
    }
    
    //cout<<f<<" "<<g<<" "<<z<<endl;
    
    ll ans=314159265;
    
    for(int r=D;r<=E;r++){
        for(int c=F;c<=G;c++){
            int h=r,w=c;
            tie(h,w)=inv(h,w);
            //cout<<h<<" "<<w<<endl;
            ll v;
            if(MA.count(mp(h,w))) v=MA[mp(h,w)];
            else v=(A*(h+1)+B*(w+1))%C;
            ans=(ans*31+v)%mod;
        }
    }
    
    cout<<ans<<"\n";
}
accuracy: 0.029197080291970802
time_ms: 10
memory_kb: 4280
final_rank: 13

code_snippet: #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return true; } return false; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return true; } return false; }
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
#define mp make_pair
#define si(x) int(x.size())
const int mod=1000000007,MAX=300005,INF=1<<30;

int main(){
    
    std::ifstream in("text.txt");
    std::cin.rdbuf(in.rdbuf());
    cin.tie(0);
    ios::sync_with_stdio(false);
    
    int N,Q;cin>>N>>Q;
    ll A,B,C,D,E,F,G;cin>>A>>B>>C>>D>>E>>F>>G;D--;E--;F--;G--;
    vector<int> H(N),W(N),invH(N),invW(N);
    iota(all(H),0);
    iota(all(W),0);
    iota(all(invH),0);
    iota(all(invW),0);
    bool f=false,g=false,z=false;
    map<pair<int,int>,int> MA;
    
    auto inv=[&](int h,int w){
        //cout<<h<<" "<<w<<" ";
        if(z) swap(h,w);
        
        if(f) h=N-1-h;
        h=invH[h];
        if(g) w=N-1-w;
        w=invW[w];
        //cout<<h<<" "<<w<<endl;
        //if(z) swap(h,w);
        return mp(h,w);
    };
    
    while(Q--){
        string S;cin>>S;
        if(S=="WR"){
            int h,w,v;cin>>h>>w>>v;h--;w--;
            tie(h,w)=inv(h,w);
            MA[mp(h,w)]=v;
        }
        if(S=="CP"){
            int h1,w1,h2,w2;cin>>h1>>w1>>h2>>w2;
            h1--;w1--;h2--;w2--;
            tie(h1,w1)=inv(h1,w1);
            tie(h2,w2)=inv(h2,w2);
            int v;
            if(MA.count(mp(h1,w1))) v=MA[mp(h1,w1)];
            else v=(A*(h1+1)+B*(w1+1))%C;
            MA[mp(h2,w2)]=v;
        }
        if(S=="SR"){
            int h1,h2;cin>>h1>>h2;h1--;h2--;
            if(f){
                h1=N-1-h1;
                h2=N-1-h2;
            }
            if(z){
                swap(invW[h1],invW[h2]);
                swap(W[invW[h1]],W[invW[h2]]);
            }else{
                swap(invH[h1],invH[h2]);
                swap(H[invH[h1]],H[invH[h2]]);
            }
        }
        if(S=="SC"){
            int w1,w2;cin>>w1>>w2;w1--;w2--;
            if(g){
                w1=N-1-w1;
                w2=N-1-w2;
            }
            if(z){
                swap(invH[w1],invH[w2]);
                swap(H[invH[w1]],H[invH[w2]]);
            }else{
                swap(invW[w1],invW[w2]);
                swap(W[invW[w1]],W[invW[w2]]);
            }
        }
        if(S=="RL"){
            swap(f,g);
            if(!z) g=!g;
            else f=!f;
            //swap(H,W);
            //swap(invH,invW);
            z=!z;
        }
        if(S=="RR"){
            swap(f,g);
            if(!z) f=!f;
            else g=!g;
            //swap(H,W);
            //swap(invH,invW);
            z=!z;
        }
        if(S=="RH"){
            if(z) g=!g;
            else f=!f;
        }
        if(S=="RV"){
            if(z) f=!f;
            else g=!g;
        }
        
        //cout<<f<<" "<<g<<" "<<z<<endl;
    }
    
    //cout<<f<<" "<<g<<" "<<z<<endl;
    
    ll ans=314159265;
    
    for(int r=D;r<=E;r++){
        for(int c=F;c<=G;c++){
            int h=r,w=c;
            tie(h,w)=inv(h,w);
            //cout<<h<<" "<<w<<endl;
            int v;
            if(MA.count(mp(h,w))) v=MA[mp(h,w)];
            else v=(A*(h+1)+B*(w+1))%C;
            ans=(ans*31+v)%mod;
        }
    }
    
    cout<<ans<<"\n";
}
accuracy: 0.029197080291970802
time_ms: 10
memory_kb: 4316
final_rank: 15

code_snippet: #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return true; } return false; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return true; } return false; }
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
#define mp make_pair
#define si(x) int(x.size())
const int mod=1000000007,MAX=300005,INF=1<<30;

int main(){
    
    std::ifstream in("text.txt");
    std::cin.rdbuf(in.rdbuf());
    cin.tie(0);
    ios::sync_with_stdio(false);
    
    int N,Q;cin>>N>>Q;
    ll A,B,C,D,E,F,G;cin>>A>>B>>C>>D>>E>>F>>G;D--;E--;F--;G--;
    vector<int> H(N),W(N),invH(N),invW(N);
    iota(all(H),0);
    iota(all(W),0);
    iota(all(invH),0);
    iota(all(invW),0);
    bool f=false,g=false,z=false;
    map<pair<int,int>,int> MA;
    
    auto inv=[&](int h,int w){
        //cout<<h<<" "<<w<<" ";
        if(f) h=N-1-h;
        h=invH[h];
        if(g) w=N-1-w;
        w=invW[w];
        //cout<<h<<" "<<w<<endl;
        if(z) swap(h,w);
        return mp(h,w);
    };
    
    while(Q--){
        string S;cin>>S;
        if(S=="WR"){
            int h,w,v;cin>>h>>w>>v;h--;w--;
            tie(h,w)=inv(h,w);
            MA[mp(h,w)]=v;
        }
        if(S=="CP"){
            int h1,w1,h2,w2;cin>>h1>>w1>>h2>>w2;
            h1--;w1--;h2--;w2--;
            tie(h1,w1)=inv(h1,w1);
            tie(h2,w2)=inv(h2,w2);
            int v;
            if(MA.count(mp(h1,w1))) v=MA[mp(h1,w1)];
            else v=(A*(h1+1)+B*(w1+1))%C;
            MA[mp(h2,w2)]=v;
        }
        if(S=="SR"){
            int h1,h2;cin>>h1>>h2;h1--;h2--;
            if(f){
                h1=N-1-h1;
                h2=N-1-h2;
            }
            if(z){
                swap(invW[h1],invW[h2]);
                swap(W[invW[h1]],W[invW[h2]]);
            }else{
                swap(invH[h1],invH[h2]);
                swap(H[invH[h1]],H[invH[h2]]);
            }
        }
        if(S=="SC"){
            int w1,w2;cin>>w1>>w2;w1--;w2--;
            if(g){
                w1=N-1-w1;
                w2=N-1-w2;
            }
            if(z){
                swap(invH[w1],invH[w2]);
                swap(H[invH[w1]],H[invH[w2]]);
            }else{
                swap(invW[w1],invW[w2]);
                swap(W[invW[w1]],W[invW[w2]]);
            }
        }
        if(S=="RL"){
            swap(f,g);
            if(z) g=!g;
            else f=!f;
            //swap(H,W);
            //swap(invH,invW);
            z=!z;
        }
        if(S=="RR"){
            swap(f,g);
            if(z) f=!f;
            else g=!g;
            //swap(H,W);
            //swap(invH,invW);
            z=!z;
        }
        if(S=="RH"){
            if(z) g=!g;
            else f=!f;
        }
        if(S=="RV"){
            if(z) f=!f;
            else g=!g;
        }
        
        //cout<<f<<" "<<g<<" "<<z<<endl;
    }
    
    //cout<<f<<" "<<g<<" "<<z<<endl;
    
    ll ans=314159265;
    
    for(int r=D;r<=E;r++){
        for(int c=F;c<=G;c++){
            int h=r,w=c;
            tie(h,w)=inv(h,w);
            //cout<<h<<" "<<w<<endl;
            int v;
            if(MA.count(mp(h,w))) v=MA[mp(h,w)];
            else v=(A*(h+1)+B*(w+1))%C;
            ans=(ans*31+v)%mod;
        }
    }
    
    cout<<ans<<"\n";
}
accuracy: 0.029197080291970802
time_ms: 10
memory_kb: 4248
final_rank: 12

code_snippet: #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return true; } return false; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return true; } return false; }
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
#define mp make_pair
#define si(x) int(x.size())
const int mod=1000000007,MAX=300005,INF=1<<30;

int main(){
    
    std::ifstream in("text.txt");
    std::cin.rdbuf(in.rdbuf());
    cin.tie(0);
    ios::sync_with_stdio(false);
    
    int N,Q;cin>>N>>Q;
    ll A,B,C,D,E,F,G;cin>>A>>B>>C>>D>>E>>F>>G;D--;E--;F--;G--;
    vector<int> H(N),W(N),invH(N),invW(N);
    iota(all(H),0);
    iota(all(W),0);
    iota(all(invH),0);
    iota(all(invW),0);
    bool f=false,g=false,z=false;
    map<pair<int,int>,int> MA;
    
    auto inv=[&](int h,int w){
        //cout<<h<<" "<<w<<" ";
        if(f) h=N-1-h;
        h=invH[h];
        if(g) w=N-1-w;
        w=invW[w];
        //cout<<h<<" "<<w<<endl;
        if(z) swap(h,w);
        return mp(h,w);
    };
    
    while(Q--){
        string S;cin>>S;
        if(S=="WR"){
            int h,w,v;cin>>h>>w>>v;h--;w--;
            tie(h,w)=inv(h,w);
            MA[mp(h,w)]=v;
        }
        if(S=="CP"){
            int h1,w1,h2,w2;cin>>h1>>w1>>h2>>w2;
            h1--;w1--;h2--;w2--;
            tie(h1,w1)=inv(h1,w1);
            tie(h2,w2)=inv(h2,w2);
            int v;
            if(MA.count(mp(h1,w1))) v=MA[mp(h1,w1)];
            else v=(A*(h1+1)+B*(w1+1))%C;
            MA[mp(h2,w2)]=v;
        }
        if(S=="SR"){
            int h1,h2;cin>>h1>>h2;h1--;h2--;
            if(f){
                h1=N-1-h1;
                h2=N-1-h2;
            }
            if(z){
                swap(invW[h1],invW[h2]);
                swap(W[invW[h1]],W[invW[h2]]);
            }else{
                swap(invH[h1],invH[h2]);
                swap(H[invH[h1]],H[invH[h2]]);
            }
        }
        if(S=="SC"){
            int w1,w2;cin>>w1>>w2;w1--;w2--;
            if(g){
                w1=N-1-w1;
                w2=N-1-w2;
            }
            if(z){
                swap(invH[w1],invH[w2]);
                swap(H[invH[w1]],H[invH[w2]]);
            }else{
                swap(invW[w1],invW[w2]);
                swap(W[invW[w1]],W[invW[w2]]);
            }
        }
        if(S=="RL"){
            swap(f,g);
            if(z) g=!g;
            else f=!f;
            swap(H,W);
            swap(invH,invW);
            z=!z;
        }
        if(S=="RR"){
            swap(f,g);
            if(z) f=!f;
            else g=!g;
            swap(H,W);
            swap(invH,invW);
            z=!z;
        }
        if(S=="RH"){
            if(z) g=!g;
            else f=!f;
        }
        if(S=="RV"){
            if(z) f=!f;
            else g=!g;
        }
        
        //cout<<f<<" "<<g<<" "<<z<<endl;
    }
    
    //cout<<f<<" "<<g<<" "<<z<<endl;
    
    ll ans=314159265;
    
    for(int r=D;r<=E;r++){
        for(int c=F;c<=G;c++){
            int h=r,w=c;
            tie(h,w)=inv(h,w);
            //cout<<h<<" "<<w<<endl;
            int v;
            if(MA.count(mp(h,w))) v=MA[mp(h,w)];
            else v=(A*(h+1)+B*(w+1))%C;
            ans=(ans*31+v)%mod;
        }
    }
    
    cout<<ans<<"\n";
}
accuracy: 0.029197080291970802
time_ms: 10
memory_kb: 4328
final_rank: 16

code_snippet: #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return true; } return false; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return true; } return false; }
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
#define mp make_pair
#define si(x) int(x.size())
const int mod=1000000007,MAX=300005,INF=1<<30;

int main(){
    
    std::ifstream in("text.txt");
    std::cin.rdbuf(in.rdbuf());
    cin.tie(0);
    ios::sync_with_stdio(false);
    
    int N,Q;cin>>N>>Q;
    ll A,B,C,D,E,F,G;cin>>A>>B>>C>>D>>E>>F>>G;D--;E--;F--;G--;
    vector<int> H(N),W(N),invH(N),invW(N);
    iota(all(H),0);
    iota(all(W),0);
    iota(all(invH),0);
    iota(all(invW),0);
    bool f=false,g=false,z=false;
    map<pair<int,int>,int> MA;
    
    auto inv=[&](int h,int w){
        //cout<<h<<" "<<w<<" ";
        if(f) h=N-1-h;
        h=invH[h];
        if(g) w=N-1-w;
        w=invW[w];
        //cout<<h<<" "<<w<<endl;
        if(z) swap(h,w);
        return mp(h,w);
    };
    
    while(Q--){
        string S;cin>>S;
        if(S=="WR"){
            int h,w,v;cin>>h>>w>>v;h--;w--;
            tie(h,w)=inv(h,w);
            MA[mp(h,w)]=v;
        }
        if(S=="CP"){
            int h1,w1,h2,w2;cin>>h1>>w1>>h2>>w2;
            h1--;w1--;h2--;w2--;
            tie(h1,w1)=inv(h1,w1);
            tie(h2,w2)=inv(h2,w2);
            int v;
            if(MA.count(mp(h1,w1))) v=MA[mp(h1,w1)];
            else v=(A*(h1+1)+B*(w1+1))%C;
            MA[mp(h2,w2)]=v;
        }
        if(S=="SR"){
            int h1,h2;cin>>h1>>h2;h1--;h2--;
            if(f){
                h1=N-1-h1;
                h2=N-1-h2;
            }
            if(z){
                swap(invW[h1],invW[h2]);
                swap(W[invW[h1]],W[invW[h2]]);
            }else{
                swap(invH[h1],invH[h2]);
                swap(H[invH[h1]],H[invH[h2]]);
            }
        }
        if(S=="SC"){
            int w1,w2;cin>>w1>>w2;w1--;w2--;
            if(g){
                w1=N-1-w1;
                w2=N-1-w2;
            }
            if(z){
                swap(invH[w1],invH[w2]);
                swap(H[invH[w1]],H[invH[w2]]);
            }else{
                swap(invW[w1],invW[w2]);
                swap(W[invW[w1]],W[invW[w2]]);
            }
        }
        if(S=="RL"){
            swap(f,g);
            f=!f;
            swap(H,W);
            swap(invH,invW);
            z=!z;
        }
        if(S=="RR"){
            swap(f,g);
            g=!g;
            swap(H,W);
            swap(invH,invW);
            z=!z;
        }
        if(S=="RH"){
            f=!f;
        }
        if(S=="RV"){
            g=!g;
        }
        
        //cout<<f<<" "<<g<<" "<<z<<endl;
    }
    
    //cout<<f<<" "<<g<<" "<<z<<endl;
    
    ll ans=314159265;
    
    for(int r=D;r<=E;r++){
        for(int c=F;c<=G;c++){
            int h=r,w=c;
            tie(h,w)=inv(h,w);
            //cout<<h<<" "<<w<<endl;
            int v;
            if(MA.count(mp(h,w))) v=MA[mp(h,w)];
            else v=(A*(h+1)+B*(w+1))%C;
            ans=(ans*31+v)%mod;
        }
    }
    
    cout<<ans<<"\n";
}
accuracy: 0.029197080291970802
time_ms: 10
memory_kb: 4292
final_rank: 14

code_snippet: #include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<vector>
#include<cmath>
#include<algorithm>
#include<map>
#include<queue>
#include<deque>
#include<iomanip>
#include<tuple>
#include<cassert>
#include<set>
#include<complex>
#include<numeric>
#include<functional>
#include<unordered_map>
#include<unordered_set>
using namespace std;
typedef long long int LL;
typedef pair<int,int> P;
typedef pair<LL,LL> LP;
const int INF=1<<30;
const LL MAX=1e9+7;

void array_show(int *array,int array_n,char middle=' '){
    for(int i=0;i<array_n;i++)printf("%d%c",array[i],(i!=array_n-1?middle:'\n'));
}
void array_show(LL *array,int array_n,char middle=' '){
    for(int i=0;i<array_n;i++)printf("%lld%c",array[i],(i!=array_n-1?middle:'\n'));
}
void array_show(vector<int> &vec_s,int vec_n=-1,char middle=' '){
    if(vec_n==-1)vec_n=vec_s.size();
    for(int i=0;i<vec_n;i++)printf("%d%c",vec_s[i],(i!=vec_n-1?middle:'\n'));
}
void array_show(vector<LL> &vec_s,int vec_n=-1,char middle=' '){
    if(vec_n==-1)vec_n=vec_s.size();
    for(int i=0;i<vec_n;i++)printf("%lld%c",vec_s[i],(i!=vec_n-1?middle:'\n'));
}
template<typename T> ostream& operator<<(ostream& os,const vector<T>& v1){
    int n=v1.size();
    for(int i=0;i<n;i++){
        if(i)os<<" ";
        os<<v1[i];
    }
    return os;
}
template<typename T1,typename T2> ostream& operator<<(ostream& os,const pair<T1,T2>& p){
    os<<p.first<<" "<<p.second;
    return os;
}
template<typename T> istream& operator>>(istream& is,vector<T>& v1){
    int n=v1.size();
    for(int i=0;i<n;i++)is>>v1[i];
    return is;
}
template<typename T1,typename T2> istream& operator>>(istream& is,pair<T1,T2>& p){
    is>>p.first>>p.second;
    return is;
}

namespace sol{
    const LL mod=1e9+7;
    LL A,B,C;
    LL N;
    map<P,LL> m1;
    int trans,xt,yt;
    vector<int> vx,vy;

    P pos(int x,int y){
        x--,y--;
        if(trans)swap(x,y);
        if(xt)x=N-x-1;
        if(yt)y=N-y-1;
        x=vx[x],y=vy[y];
        if(m1.find({x,y})==m1.end()){
            m1[{x,y}]=(A*(x+1)+B*(y+1))%C;
        }
        return {x,y};
    }

    void solve(){
        int m;
        int i,j,k;
        LL a,b,c,d;
        LL D,E,F,G;
        string sa;
        cin>>N>>m>>A>>B>>C>>D>>E>>F>>G;
        vx.resize(N),vy.resize(N);
        iota(vx.begin(),vx.end(),0);
        iota(vy.begin(),vy.end(),0);
        for(i=0;i<m;i++){

            // for(j=0;j<N;j++){
            //     for(k=0;k<N;k++){
            //         cout<<m1[pos(j+1,k+1)]<<" ";
            //     }
            //     cout<<endl;
            // }

            cin>>sa;
            if(sa=="WR"){
                cin>>a>>b>>c;
                m1[pos(a,b)]=c;
            }
            if(sa=="CP"){
                cin>>a>>b>>c>>d;
                m1[pos(c,d)]=m1[pos(a,b)];
            }
            if(sa=="SR" || sa=="SC"){
                cin>>a>>b;
                a--,b--,c=0;
                if(sa=="SC")c=1;
                c^=trans;
                if(c==0){
                    if(xt)a=N-a-1,b=N-b-1;
                    swap(vx[a],vx[b]);
                }else{
                    if(yt)a=N-a-1,b=N-b-1;
                    swap(vy[a],vy[b]);
                }
            }
            if(sa=="RH")xt^=1;
            if(sa=="RV")yt^=1;
            if(sa=="RL"){
                trans^=1;
                swap(xt,yt);
                yt^=1;
            }
            if(sa=="RR"){
                trans^=1;
                swap(xt,yt);
                xt^=1;
            }
        }
        LL s=314159265;
        for(i=D;i<=E;i++){
            for(j=F;j<=G;j++){
                s=(31*s+m1[pos(i,j)])%mod;
            }
        }
        cout<<s<<endl;
    }
}

int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);
    sol::solve();
}
accuracy: 0.029197080291970802
time_ms: 310
memory_kb: 65692
final_rank: 18

code_snippet: #include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<vector>
#include<cmath>
#include<algorithm>
#include<map>
#include<queue>
#include<deque>
#include<iomanip>
#include<tuple>
#include<cassert>
#include<set>
#include<complex>
#include<numeric>
#include<functional>
#include<unordered_map>
#include<unordered_set>
using namespace std;
typedef long long int LL;
typedef pair<int,int> P;
typedef pair<LL,LL> LP;
const int INF=1<<30;
const LL MAX=1e9+7;

void array_show(int *array,int array_n,char middle=' '){
    for(int i=0;i<array_n;i++)printf("%d%c",array[i],(i!=array_n-1?middle:'\n'));
}
void array_show(LL *array,int array_n,char middle=' '){
    for(int i=0;i<array_n;i++)printf("%lld%c",array[i],(i!=array_n-1?middle:'\n'));
}
void array_show(vector<int> &vec_s,int vec_n=-1,char middle=' '){
    if(vec_n==-1)vec_n=vec_s.size();
    for(int i=0;i<vec_n;i++)printf("%d%c",vec_s[i],(i!=vec_n-1?middle:'\n'));
}
void array_show(vector<LL> &vec_s,int vec_n=-1,char middle=' '){
    if(vec_n==-1)vec_n=vec_s.size();
    for(int i=0;i<vec_n;i++)printf("%lld%c",vec_s[i],(i!=vec_n-1?middle:'\n'));
}
template<typename T> ostream& operator<<(ostream& os,const vector<T>& v1){
    int n=v1.size();
    for(int i=0;i<n;i++){
        if(i)os<<" ";
        os<<v1[i];
    }
    return os;
}
template<typename T1,typename T2> ostream& operator<<(ostream& os,const pair<T1,T2>& p){
    os<<p.first<<" "<<p.second;
    return os;
}
template<typename T> istream& operator>>(istream& is,vector<T>& v1){
    int n=v1.size();
    for(int i=0;i<n;i++)is>>v1[i];
    return is;
}
template<typename T1,typename T2> istream& operator>>(istream& is,pair<T1,T2>& p){
    is>>p.first>>p.second;
    return is;
}

namespace sol{
    const LL mod=1e9+7;
    LL A,B,C;
    LL N;
    map<P,LL> m1;
    int trans,xt,yt;
    vector<int> vx,vy;

    P pos(int x,int y){
        x--,y--;
        if(trans)swap(x,y);
        if(xt)x=N-x-1;
        if(yt)y=N-y-1;
        x=vx[x],y=vy[y];
        if(m1.find({x,y})==m1.end()){
            m1[{x,y}]=(A*(x+1)+B*(y+1))%C;
        }
        return {x,y};
    }

    void solve(){
        int m;
        int i,j,k;
        LL a,b,c,d;
        LL D,E,F,G;
        string sa;
        cin>>N>>m>>A>>B>>C>>D>>E>>F>>G;
        vx.resize(N),vy.resize(N);
        iota(vx.begin(),vx.end(),0);
        iota(vy.begin(),vy.end(),0);
        for(i=0;i<m;i++){

            // for(j=0;j<N;j++){
            //     for(k=0;k<N;k++){
            //         cout<<m1[pos(j+1,k+1)]<<" ";
            //     }
            //     cout<<endl;
            // }

            cin>>sa;
            if(sa=="WR"){
                cin>>a>>b>>c;
                m1[pos(a,b)]=c;
            }
            if(sa=="CP"){
                cin>>a>>b>>c>>d;
                m1[pos(c,d)]=m1[pos(a,b)];
            }
            if(sa=="SR" || sa=="SC"){
                cin>>a>>b;
                a--,b--,c=0;
                if(sa=="SC")c=1;
                c^=trans;
                if(c==0){
                    if(xt)a=N-a-1,b=N-b-1;
                    swap(vx[a],vx[b]);
                }else{
                    if(yt)a=N-a-1,b=N-b-1;
                    swap(vy[a],vy[b]);
                }
            }
            if(sa=="RH"||sa=="RV"){
                c=trans;
                if(sa=="RV")c^=1;
                if(c==0)xt^=1;
                else yt^=1;
            }
            if(sa=="RL"){
                trans^=1;
                swap(xt,yt);
                if(trans)yt^=1;
                else xt^=1;
            }
            if(sa=="RR"){
                trans^=1;
                swap(xt,yt);
                if(trans)xt^=1;
                else yt^=1;
            }
        }
        LL s=314159265;
        for(i=D;i<=E;i++){
            for(j=F;j<=G;j++){
                s=(31*s+m1[pos(i,j)])%mod;
            }
        }
        cout<<s<<endl;
    }
}

int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);
    sol::solve();
}
accuracy: 0.029197080291970802
time_ms: 300
memory_kb: 65908
final_rank: 17

code_snippet: #include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<vector>
#include<cmath>
#include<algorithm>
#include<map>
#include<queue>
#include<deque>
#include<iomanip>
#include<tuple>
#include<cassert>
#include<set>
#include<complex>
#include<numeric>
#include<functional>
#include<unordered_map>
#include<unordered_set>
using namespace std;
typedef long long int LL;
typedef pair<int,int> P;
typedef pair<LL,LL> LP;
const int INF=1<<30;
const LL MAX=1e9+7;

void array_show(int *array,int array_n,char middle=' '){
    for(int i=0;i<array_n;i++)printf("%d%c",array[i],(i!=array_n-1?middle:'\n'));
}
void array_show(LL *array,int array_n,char middle=' '){
    for(int i=0;i<array_n;i++)printf("%lld%c",array[i],(i!=array_n-1?middle:'\n'));
}
void array_show(vector<int> &vec_s,int vec_n=-1,char middle=' '){
    if(vec_n==-1)vec_n=vec_s.size();
    for(int i=0;i<vec_n;i++)printf("%d%c",vec_s[i],(i!=vec_n-1?middle:'\n'));
}
void array_show(vector<LL> &vec_s,int vec_n=-1,char middle=' '){
    if(vec_n==-1)vec_n=vec_s.size();
    for(int i=0;i<vec_n;i++)printf("%lld%c",vec_s[i],(i!=vec_n-1?middle:'\n'));
}
template<typename T> ostream& operator<<(ostream& os,const vector<T>& v1){
    int n=v1.size();
    for(int i=0;i<n;i++){
        if(i)os<<" ";
        os<<v1[i];
    }
    return os;
}
template<typename T1,typename T2> ostream& operator<<(ostream& os,const pair<T1,T2>& p){
    os<<p.first<<" "<<p.second;
    return os;
}
template<typename T> istream& operator>>(istream& is,vector<T>& v1){
    int n=v1.size();
    for(int i=0;i<n;i++)is>>v1[i];
    return is;
}
template<typename T1,typename T2> istream& operator>>(istream& is,pair<T1,T2>& p){
    is>>p.first>>p.second;
    return is;
}

namespace sol{
    const LL mod=1e9+7;
    LL A,B,C;
    LL N;
    map<P,LL> m1;
    int trans,xt,yt;
    vector<int> vx,vy;

    P pos(int x,int y){
        x--,y--;
        if(trans)swap(x,y);
        if(xt)x=N-x-1;
        if(yt)y=N-y-1;
        x=vx[x],y=vy[y];
        if(m1.find({x,y})==m1.end()){
            m1[{x,y}]=(A*(x+1)+B*(y+1))%C;
        }
        return {x,y};
    }

    void solve(){
        int m;
        int i,j,k;
        LL a,b,c,d;
        LL D,E,F,G;
        string sa;
        cin>>N>>m>>A>>B>>C>>D>>E>>F>>G;
        vx.resize(N),vy.resize(N);
        iota(vx.begin(),vx.end(),0);
        iota(vy.begin(),vy.end(),0);
        for(i=0;i<m;i++){
            cin>>sa;
            if(sa=="WR"){
                cin>>a>>b>>c;
                m1[pos(a,b)]=c;
            }
            if(sa=="CR"){
                cin>>a>>b>>c>>d;
                m1[pos(c,d)]=m1[pos(a,b)];
            }
            if(sa=="SR" || sa=="SC"){
                cin>>a>>b;
                a--,b--,c=0;
                if(sa=="SC")c=1;
                c^=trans;
                if(c==0){
                    if(xt)a=N-a-1,b=N-b-1;
                    swap(vx[a],vx[b]);
                }else{
                    if(yt)a=N-a-1,b=N-b-1;
                    swap(vy[a],vy[b]);
                }
            }
            if(sa=="RH"||sa=="RV"){
                c=trans;
                if(sa=="RV")c^=1;
                if(c==0)xt^=1;
                else yt^=1;
            }
            if(sa=="RL"){
                trans^=1;
                swap(xt,yt);
                yt^=1;
            }
            if(sa=="RR"){
                trans^=1;
                swap(xt,yt);
                xt^=1;
            }
        }
        LL s=314159265;
        for(i=D;i<=E;i++){
            for(j=F;j<=G;j++){
                s=(31*s+m1[pos(i,j)])%mod;
            }
        }
        cout<<s<<endl;
    }
}

int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);
    sol::solve();
}
accuracy: 0.021897810218978103
time_ms: 300
memory_kb: 65884
final_rank: 20

code_snippet: #include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<vector>
#include<cmath>
#include<algorithm>
#include<map>
#include<queue>
#include<deque>
#include<iomanip>
#include<tuple>
#include<cassert>
#include<set>
#include<complex>
#include<numeric>
#include<functional>
#include<unordered_map>
#include<unordered_set>
using namespace std;
typedef long long int LL;
typedef pair<int,int> P;
typedef pair<LL,LL> LP;
const int INF=1<<30;
const LL MAX=1e9+7;

void array_show(int *array,int array_n,char middle=' '){
    for(int i=0;i<array_n;i++)printf("%d%c",array[i],(i!=array_n-1?middle:'\n'));
}
void array_show(LL *array,int array_n,char middle=' '){
    for(int i=0;i<array_n;i++)printf("%lld%c",array[i],(i!=array_n-1?middle:'\n'));
}
void array_show(vector<int> &vec_s,int vec_n=-1,char middle=' '){
    if(vec_n==-1)vec_n=vec_s.size();
    for(int i=0;i<vec_n;i++)printf("%d%c",vec_s[i],(i!=vec_n-1?middle:'\n'));
}
void array_show(vector<LL> &vec_s,int vec_n=-1,char middle=' '){
    if(vec_n==-1)vec_n=vec_s.size();
    for(int i=0;i<vec_n;i++)printf("%lld%c",vec_s[i],(i!=vec_n-1?middle:'\n'));
}
template<typename T> ostream& operator<<(ostream& os,const vector<T>& v1){
    int n=v1.size();
    for(int i=0;i<n;i++){
        if(i)os<<" ";
        os<<v1[i];
    }
    return os;
}
template<typename T1,typename T2> ostream& operator<<(ostream& os,const pair<T1,T2>& p){
    os<<p.first<<" "<<p.second;
    return os;
}
template<typename T> istream& operator>>(istream& is,vector<T>& v1){
    int n=v1.size();
    for(int i=0;i<n;i++)is>>v1[i];
    return is;
}
template<typename T1,typename T2> istream& operator>>(istream& is,pair<T1,T2>& p){
    is>>p.first>>p.second;
    return is;
}

namespace sol{
    const LL mod=1e9+7;
    LL A,B,C;
    LL N;
    map<P,LL> m1;
    int trans,xt,yt;
    vector<int> vx,vy;

    P pos(int x,int y){
        x--,y--;
        if(trans)swap(x,y);
        if(xt)x=N-x-1;
        if(yt)y=N-y-1;
        x=vx[x],y=vy[y];
        if(m1.find({x,y})==m1.end()){
            m1[{x,y}]=(A*(x+1)+B*(y+1))%C;
        }
        return {x,y};
    }

    void solve(){
        int m;
        int i,j,k;
        LL a,b,c,d;
        LL D,E,F,G;
        string sa;
        cin>>N>>m>>A>>B>>C>>D>>E>>F>>G;
        vx.resize(N),vy.resize(N);
        iota(vx.begin(),vx.end(),0);
        iota(vy.begin(),vy.end(),0);
        for(i=0;i<m;i++){
            cin>>sa;
            if(sa=="WR"){
                cin>>a>>b>>c;
                m1[pos(a,b)]=c;
            }
            if(sa=="CR"){
                cin>>a>>b>>c>>d;
                m1[pos(c,d)]=m1[pos(a,b)];
            }
            if(sa=="SR" || sa=="SC"){
                cin>>a>>b;
                a--,b--,c=0;
                if(sa=="SC")c=1;
                c^=trans;
                if(c==0){
                    if(xt)a=N-a-1,b=N-b-1;
                    swap(vx[a],vx[b]);
                }else{
                    if(yt)a=N-a-1,b=N-b-1;
                    swap(vy[a],vy[b]);
                }
            }
            if(sa=="RH")xt^=1;
            if(sa=="RV")yt^=1;
            if(sa=="RL"){
                trans^=1;
                swap(xt,yt);
                yt^=1;
            }
            if(sa=="RR"){
                yt^=1;
                trans^=1;
                swap(xt,yt);
            }
        }
        LL s=314159265;
        for(i=D;i<=E;i++){
            for(j=F;j<=G;j++){
                s=(31*s+m1[pos(i,j)])%mod;
            }
        }
        cout<<s<<endl;
    }
}

int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);
    sol::solve();
}
accuracy: 0.029197080291970802
time_ms: 310
memory_kb: 65808
final_rank: 19

code_snippet: /*	author:  Kite_kuma
	created: 2021.10.30 17:38:41 */

// #ifdef LOCAL
// #define _GLIBCXX_DEBUG
// #endif
#include <bits/stdc++.h>
using namespace std;

#pragma region macros

#define foa(s, v) for(auto &s : v)
#define all(v) (v).begin(), (v).end()
#define rall(v) (v).rbegin(), (v).rend()
#define popcnt(n) __builtin_popcountll((long long)n)

#define REPname(a, b, c, d, e, ...) e
#define rep(...) REPname(__VA_ARGS__, REP3, REP2, REP1, REP0)(__VA_ARGS__)
#define REP0(x) for(int Counter_in_rep_macro = 0; Counter_in_rep_macro < (x); ++Counter_in_rep_macro)
#define REP1(i, x) for(int i = 0; i < (x); ++i)
#define REP2(i, l, r) for(int i = (l); i < (r); ++i)
#define REP3(i, l, r, c) for(int i = (l); i < (r); i += (c))

#define DREPname(a, b, c, d, e, ...) e
#define drep(...) DREPname(__VA_ARGS__, DREP3, DREP2, DREP1)(__VA_ARGS__)
#define DREP1(i, x) for(int i = (x)-1; i >= 0; --i)
#define DREP2(i, l, r) for(int i = (r)-1; i >= (l); --i)
#define DREP3(i, l, r, c) for(int i = (r)-1; i >= (l); i -= (c))

#pragma endregion

#pragma region aliases

using ll = long long;
using ld = long double;
using ull = unsigned long long;
using vi = std::vector<int>;
using vvi = std::vector<std::vector<int>>;
using vvvi = std::vector<std::vector<std::vector<int>>>;
using vll = std::vector<ll>;
using vvll = std::vector<vll>;
using vvvll = std::vector<vvll>;
using pii = std::pair<int, int>;
using pll = std::pair<long long, long long>;
template <class T = ll>
using V = std::vector<T>;
template <class T = ll>
using VV = V<V<T>>;
template <class T = ll>
using VVV = V<V<V<T>>>;
template <class T = ll>
using pqup = std::priority_queue<T, std::vector<T>, std::greater<T>>;
template <class T = ll>
using pqdn = std::priority_queue<T>;

#pragma endregion

#pragma region constants

const int inf = 1e9;
const long long INF = 1e18;
const long double pi = acos(-1);
const char dl = '\n';
const char sp = ' ';
int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};
int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};

const int mod_1000000007 = 1000000007;
const int mod_998244353 = 998244353;

#pragma endregion

#pragma region basic_operation

template <class T0, class T1, class T2>
inline bool in_range(T0 x, T1 lef, T2 rig) {
	return ((lef <= x) && (x < rig));
}

template <class T>
inline bool chmin(T &a, T b) {
	if(a > b) {
		a = b;
		return true;
	}
	return false;
}
template <class T>
inline bool chmax(T &a, T b) {
	if(a < b) {
		a = b;
		return true;
	}
	return false;
}

void Yes(bool f = 1) { std::cout << (f ? "Yes" : "No") << '\n'; }
void No() { std::cout << "No\n"; }
void YES(bool f = 1) { std::cout << (f ? "YES" : "NO") << '\n'; }
void NO() { std::cout << "NO\n"; }

template <class T>
void drop(T answer) {
	std::cout << answer << '\n';
	exit(0);
}

void err(bool flag = true) {
	if(!flag) return;
	std::cout << -1 << '\n';
	exit(0);
}

template <class T>
void vout(std::vector<T> const &v, bool vertically = 0) {
	if(vertically) {
		for(auto const &a : v) {
			std::cout << a << '\n';
		}
		return;
	}
	for(auto it = v.begin(); it != v.end(); it++) {
		std::cout << (*it);
		if(it != v.end() - 1) {
			std::cout << ' ';
		}
	}
	std::cout << '\n';
	return;
}

inline void print() { std::cout << '\n'; }
template <class T>
inline void print(T x) {
	std::cout << x << '\n';
	return;
}
template <typename Head, typename... Tail>
void print(Head H, Tail... T) {
	std::cout << H << " ";
	print(T...);
}

template <class T>
void add(std::vector<T> &v, T val) {
	for(auto &a : v) a += val;
	return;
}

template <class T>
T dup(T a, T b) {
	assert(b != 0);
	return (a + b - 1) / b;
}

template <class T>
T greatest_lower_multiple(T x, T d) {
	if(d == 0) return 0;
	if(d < 0) d *= -1;
	T y = x % d;
	if(y < 0) y += d;
	return x - y;
}

template <class T>
T least_upper_multiple(T x, T d) {
	return -greatest_lower_multiple(-x, d);
}

long long POW(long long a, long long n) {
	long long res = 1;
	while(n > 0) {
		if(n & 1) res = res * a;
		a = a * a;
		n >>= 1;
	}
	return res;
}

long long modpow(long long a, long long n, long long mod) {	 // a^n mod
	assert(n >= 0 && mod);
	if(mod == 1) return 0LL;
	long long res = 1;
	a %= mod;
	while(n > 0) {
		if(n & 1) res = res * a % mod;
		a = a * a % mod;
		n >>= 1;
	}
	return res < 0 ? res + mod : res;
}

// return x which satisfies a * x % mod == gcd(a, mod)
// not (mod | a)
long long modinv(long long a, long long mod) {
	long long b = mod, u = 1, v = 0;
	while(b) {
		long long t = a / b;
		a -= t * b;
		std::swap(a, b);
		u -= t * v;
		std::swap(u, v);
	}
	u %= mod;
	if(u < 0) u += mod;
	return u;
}

template <class T>
int lb(const std::vector<T> &a, const T x) {
	return std::distance(a.begin(), std::lower_bound(a.begin(), a.end(), x));
}
template <class T>
int ub(const std::vector<T> &a, const T x) {
	return std::distance(a.begin(), std::upper_bound(a.begin(), a.end(), x));
}
template <class T>
void unq_sort(std::vector<T> &a) {
	std::sort(a.begin(), a.end());
	a.erase(std::unique(a.begin(), a.end()), a.end());
}
template <class T>
std::vector<int> press(std::vector<T> &a) {
	auto vec = a;
	unq_sort(vec);
	std::vector<int> ret;
	for(auto &v : a) ret.push_back(lb(vec, v));
	return ret;
}

#pragma endregion

#pragma region input
#define VEC(type, name, size) \
	vector<type> name(size);  \
	scanner::INPUT(name)
#define VVEC(type, name, h, w)                     \
	vector<vector<type>> name(h, vector<type>(w)); \
	scanner::INPUT(name)
#define INT(...)     \
	int __VA_ARGS__; \
	scanner::INPUT(__VA_ARGS__)
#define LL(...)            \
	long long __VA_ARGS__; \
	scanner::INPUT(__VA_ARGS__)
#define STR(...)        \
	string __VA_ARGS__; \
	scanner::INPUT(__VA_ARGS__)
#define CHR(...)      \
	char __VA_ARGS__; \
	scanner::INPUT(__VA_ARGS__)
#define DBL(...)        \
	double __VA_ARGS__; \
	scanner::INPUT(__VA_ARGS__)
#define LD(...)              \
	long double __VA_ARGS__; \
	scanner::INPUT(__VA_ARGS__)
#define TPL3(type0, type1, type2, name)   \
	std::tuple<type0, type1, type2> name; \
	scanner::INPUT(name);
#define TPL4(type0, type1, type2, type3, name)   \
	std::tuple<type0, type1, type2, type3> name; \
	scanner::INPUT(name);

namespace scanner {
template <class T>
void scan(T &a) {
	std::cin >> a;
}

template <class T, class L>
void scan(std::pair<T, L> &p) {
	scan(p.first);
	scan(p.second);
}

template <class T0, class T1, class T2>
void scan(std::tuple<T0, T1, T2> &p) {
	T0 t0;
	T1 t1;
	T2 t2;
	scan(t0);
	scan(t1);
	scan(t2);
	p = std::make_tuple(t0, t1, t2);
}

template <class T0, class T1, class T2, class T3>
void scan(std::tuple<T0, T1, T2, T3> &p) {
	T0 t0;
	T1 t1;
	T2 t2;
	T3 t3;
	scan(t0);
	scan(t1);
	scan(t2);
	scan(t3);
	p = std::make_tuple(t0, t1, t2, t3);
}

template <class T>
void scan(std::vector<T> &a) {
	for(auto &i : a) scan(i);
}

void INPUT() {}
template <class Head, class... Tail>
void INPUT(Head &head, Tail &... tail) {
	scan(head);
	INPUT(tail...);
}
}  // namespace scanner

template <typename T1, typename T2>
std::istream &operator>>(std::istream &is, std::pair<T1, T2> &p) {
	is >> p.first >> p.second;
	return is;
}
#pragma endregion

#pragma region debug

#pragma region output
template <typename T1, typename T2>
std::ostream &std::operator<<(std::ostream &os, const std::pair<T1, T2> &p) {
	os << p.first << " " << p.second;
	return os;
}
template <class T>
std::ostream &std::operator<<(std::ostream &os, const std::vector<T> &v) {
	for(int i = 0; i < (int)v.size(); i++) {
		if(i) os << " ";
		os << v[i];
	}
	return os;
}
#pragma endregion

#pragma region view

namespace viewer {
void view(const long long e) {
	if(e == INF)
		std::cerr << "INF";
	else if(e == -INF)
		std::cerr << "-INF";
	else
		std::cerr << e;
}

void view(const int e) {
	if(e == inf)
		std::cerr << "inf";
	else if(e == -inf)
		std::cerr << "-inf";
	else
		std::cerr << e;
}

template <typename T>
void view(const T e) {
	std::cerr << e;
}

template <typename T, typename U>
void view(const std::pair<T, U> &p) {
	std::cerr << "(";
	view(p.first);
	std::cerr << ", ";
	view(p.second);
	std::cerr << ")";
}

template <class T0, class T1, class T2>
void view(const std::tuple<T0, T1, T2> &p) {
	std::cerr << "(";
	view(std::get<0>(p));
	std::cerr << ", ";
	view(std::get<1>(p));
	std::cerr << ", ";
	view(std::get<2>(p));
	std::cerr << ")";
}

template <class T0, class T1, class T2, class T3>
void view(const std::tuple<T0, T1, T2, T3> &p) {
	std::cerr << "(";
	view(std::get<0>(p));
	std::cerr << ", ";
	view(std::get<1>(p));
	std::cerr << ", ";
	view(std::get<2>(p));
	std::cerr << ", ";
	view(std::get<3>(p));
	std::cerr << ")";
}

template <typename T>
void view(const std::set<T> &s) {
	if(s.empty()) {
		std::cerr << "{ }";
		return;
	}
	std::cerr << "{ ";
	for(auto &t : s) {
		view(t);
		std::cerr << ", ";
	}
	std::cerr << "\b\b }";
}

template <typename T>
void view(const std::unordered_set<T> &s) {
	if(s.empty()) {
		std::cerr << "{ }";
		return;
	}
	std::cerr << "{ ";
	for(auto &t : s) {
		view(t);
		std::cerr << ", ";
	}
	std::cerr << "\b\b }";
}

template <typename T>
void view(const std::vector<T> &v) {
	if(v.empty()) {
		std::cerr << "{ }";
		return;
	}
	std::cerr << "{ ";
	for(const auto &e : v) {
		view(e);
		std::cerr << ", ";
	}
	std::cerr << "\b\b }";
}

template <typename T>
void view(const std::vector<std::vector<T>> &vv) {
	std::cerr << "{\n";
	for(const auto &v : vv) {
		std::cerr << "\t";
		view(v);
		std::cerr << '\n';
	}
	std::cerr << "}";
}

template <typename T, typename U>
void view(const std::vector<std::pair<T, U>> &v) {
	std::cerr << "{\n";
	for(const auto &c : v) {
		std::cerr << "\t(";
		view(c.first);
		std::cerr << ", ";
		view(c.second);
		std::cerr << ")\n";
	}
	std::cerr << "}";
}

template <class T0, class T1, class T2>
void view(const std::vector<std::tuple<T0, T1, T2>> &v) {
	if(v.empty()) {
		std::cerr << "{ }";
		return;
	}
	std::cerr << '{';
	for(const auto &t : v) {
		std::cerr << "\n\t";
		view(t);
		std::cerr << ",";
	}
	std::cerr << "\n}";
}

template <class T0, class T1, class T2, class T3>
void view(const std::vector<std::tuple<T0, T1, T2, T3>> &v) {
	if(v.empty()) {
		std::cerr << "{ }";
		return;
	}
	std::cerr << '{';
	for(const auto &t : v) {
		std::cerr << "\n\t";
		view(t);
		std::cerr << ",";
	}
	std::cerr << "\n}";
}

template <typename T, typename U>
void view(const std::map<T, U> &m) {
	std::cerr << "{\n";
	for(const auto &t : m) {
		std::cerr << "\t[";
		view(t.first);
		std::cerr << "] : ";
		view(t.second);
		std::cerr << '\n';
	}
	std::cerr << "}";
}

template <typename T, typename U>
void view(const std::unordered_map<T, U> &m) {
	std::cerr << "{\n";
	for(const auto &t : m) {
		std::cerr << "\t[";
		view(t.first);
		std::cerr << "] : ";
		view(t.second);
		std::cerr << '\n';
	}
	std::cerr << "}";
}
}  // namespace viewer

#pragma endregion

// when debugging : g++ foo.cpp -DLOCAL
#ifdef LOCAL
void debug_out() {}
template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {
	viewer::view(H);
	std::cerr << ", ";
	debug_out(T...);
}
#define debug(...)                                                \
	do {                                                          \
		std::cerr << __LINE__ << " [" << #__VA_ARGS__ << "] : ["; \
		debug_out(__VA_ARGS__);                                   \
		std::cerr << "\b\b]\n";                                   \
	} while(0)
#define dump(x)                                      \
	do {                                             \
		std::cerr << __LINE__ << " " << #x << " : "; \
		viewer::view(x);                             \
		std::cerr << '\n';                           \
	} while(0)

#else
#define debug(...) (void(0))
#define dump(x) (void(0))
#endif

#pragma endregion

#pragma region modint

const int mod = mod_1000000007;

template <int mod>
struct ModInt {
	int x;
	ModInt() : x(0) {}
	ModInt(long long y) : x(y >= 0 ? y % mod : (mod - (-y) % mod) % mod) {}
	ModInt &operator+=(const ModInt &p) {
		if((x += p.x) >= mod) x -= mod;
		return *this;
	}
	ModInt &operator-=(const ModInt &p) {
		if((x += mod - p.x) >= mod) x -= mod;
		return *this;
	}
	ModInt &operator*=(const ModInt &p) {
		x = (int)(1LL * x * p.x % mod);
		return *this;
	}
	ModInt &operator/=(const ModInt &p) {
		*this *= p.inverse();
		return *this;
	}
	ModInt operator-() const { return ModInt(-x); }
	ModInt operator+(const ModInt &p) const { return ModInt(*this) += p; }
	ModInt operator-(const ModInt &p) const { return ModInt(*this) -= p; }
	ModInt operator*(const ModInt &p) const { return ModInt(*this) *= p; }
	ModInt operator/(const ModInt &p) const { return ModInt(*this) /= p; }
	bool operator==(const ModInt &p) const { return x == p.x; }
	bool operator!=(const ModInt &p) const { return x != p.x; }
	ModInt inverse() const {
		int a = x, b = mod, u = 1, v = 0, t;
		while(b > 0) {
			t = a / b;
			swap(a -= t * b, b);
			swap(u -= t * v, v);
		}
		return ModInt(u);
	}
	ModInt pow(long long n) const {
		ModInt ret(1), mul(x);
		while(n > 0) {
			if(n & 1) ret *= mul;
			mul *= mul;
			n >>= 1;
		}
		return ret;
	}
	friend ostream &operator<<(ostream &os, const ModInt &p) { return os << p.x; }
	friend istream &operator>>(istream &is, ModInt &a) {
		long long t;
		is >> t;
		a = ModInt<mod>(t);
		return (is);
	}
	static int get_mod() { return mod; }
};
using mint = ModInt<mod>;

#pragma endregion

mint solve(int n) {
	int q;
	cin >> q;
	LL(A, B, C);
	INT(D, E, F, G);
	D--, E--, F--, G--;

	map<pair<int, int>, ll> non_empty;
	bool is_transposed = false;
	bool row_is_reversed = false;
	bool col_is_reversed = false;

	vector<int> row(n), col(n);
	// もとのi行目がrow[i]行目にいる
	vector<int> row_rev(n), col_rev(n);
	// もとのrow_rev[i]行目がi行目にいる

	iota(row.begin(), row.end(), 0);
	iota(col.begin(), col.end(), 0);
	iota(row_rev.begin(), row_rev.end(), 0);
	iota(col_rev.begin(), col_rev.end(), 0);

	auto value_in_original_mat = [&](int row, int col) {
		if(non_empty.count(pair(row, col))) return non_empty[pair(row, col)];
		return (((row + 1) * A + (col + 1) * B) % C + C) % C;
	};

	auto update = [&](int r, int c, ll val) {
		non_empty[(pair(r, c))] = val;
		return;
	};

	auto copy_cell = [&](int rfrom, int cfrom, int rto, int cto) {
		update(rto, cto, value_in_original_mat(rfrom, cfrom));
		return;
	};

	auto swap_permutaiton = [](vi &perm, vi &rev, int x, int y) -> void {
		swap(perm[x], perm[y]);
		swap(rev[perm[x]], rev[perm[y]]);
		return;
	};

	auto rl = [&is_transposed, &col_is_reversed, &row_is_reversed]() {	// ok
		if(!is_transposed)
			col_is_reversed = !col_is_reversed;
		else
			row_is_reversed = !row_is_reversed;
		is_transposed = !is_transposed;
		return;
	};

	auto rr = [&rl]() {	 // ok
		rep(3) rl();
		return;
	};

	auto swap_row = [&swap_permutaiton, &row_rev, &row](int r1, int r2) {
		swap_permutaiton(row, row_rev, r1, r2);
		return;
	};

	auto swap_col = [&swap_permutaiton, &col_rev, &col](int r1, int r2) {
		swap_permutaiton(col, col_rev, r1, r2);
		return;
	};

	auto rh = [&is_transposed, &col_is_reversed, &row_is_reversed]() {
		if(is_transposed)
			col_is_reversed = !col_is_reversed;
		else
			row_is_reversed = !row_is_reversed;
		return;
	};

	auto rv = [&is_transposed, &col_is_reversed, &row_is_reversed]() {
		if(is_transposed)
			row_is_reversed = !row_is_reversed;
		else
			col_is_reversed = !col_is_reversed;
		return;
	};

	auto former_index = [&row_rev, &col_rev, &is_transposed, &row_is_reversed, &col_is_reversed, &n](int &r_now, int &c_now) {
		if(is_transposed) swap(r_now, c_now);
		if(row_is_reversed) r_now = n - 1 - r_now;
		if(col_is_reversed) c_now = n - 1 - c_now;
		r_now = row_rev[r_now];
		c_now = col_rev[c_now];
		return;
	};

	auto former_row_idx = [&row_is_reversed, &row_rev, &n](int idx) -> int {
		if(row_is_reversed) idx = n - 1 - idx;
		return row_rev[idx];
	};

	auto former_col_idx = [&col_is_reversed, &col_rev, &n](int idx) -> int {
		if(col_is_reversed) idx = n - 1 - idx;
		return col_rev[idx];
	};

	rep(q) {
		string s;
		cin >> s;
		if(s == "WR") {
			int r, c;
			ll v;
			cin >> r >> c >> v;
			r--;
			c--;
			former_index(r, c);
			update(r, c, v);
		} else if("CP" == s) {
			int rf, cf, rt, ct;
			cin >> rf >> cf >> rt >> ct;
			rf--;
			cf--;
			rt--;
			ct--;
			former_index(rf, cf);
			former_index(rt, ct);
			copy_cell(rf, cf, rt, ct);
		} else if("SR" == s) {
			int r1, r2;
			cin >> r1 >> r2;
			r1--;
			r2--;
			if(is_transposed) {
				r1 = former_col_idx(r1);
				r2 = former_col_idx(r2);
				swap_col(r1, r2);
			} else {
				r1 = former_row_idx(r1);
				r2 = former_row_idx(r2);
				swap_row(r1, r2);
			}
		} else if("SC" == s) {
			int r1, r2;
			cin >> r1 >> r2;
			r1--;
			r2--;
			if(!is_transposed) {
				r1 = former_col_idx(r1);
				r2 = former_col_idx(r2);
				swap_col(r1, r2);
			} else {
				r1 = former_row_idx(r1);
				r2 = former_row_idx(r2);
				swap_row(r1, r2);
			}
		} else if("RL" == s) {
			rl();
		} else if("RR" == s) {
			rr();
		} else if("RH" == s) {
			rh();
		} else if("RV" == s) {
			rv();
		} else {
			assert(false);
		}
	}

	auto hash = [&value_in_original_mat, &D, &former_index, &E, &F, &G, &non_empty]() -> mint {
		mint h = 314159265;
		rep(r, D, E + 1) rep(c, F, G + 1) {
			h *= 31;
			int x, y;
			x = r, y = c;
			former_index(x, y);
			ll ad = value_in_original_mat(x, y);
			h += ad;
		}
		return h;
	};

	return hash();
}

int main() {
	std::ios::sync_with_stdio(false);
	std::cin.tie(nullptr);
	std::cout << std::fixed << std::setprecision(15);
	srand((unsigned)time(NULL));

	int n;
	while(cin >> n && n) {
		cout << solve(n) << dl;
	}

	return 0;
}
accuracy: 1
time_ms: 30
memory_kb: 4364
final_rank: 7

code_snippet: /*	author:  Kite_kuma
	created: 2021.10.30 17:38:41 */

// #ifdef LOCAL
// #define _GLIBCXX_DEBUG
// #endif
#include <bits/stdc++.h>
using namespace std;

#pragma region macros

#define foa(s, v) for(auto &s : v)
#define all(v) (v).begin(), (v).end()
#define rall(v) (v).rbegin(), (v).rend()
#define popcnt(n) __builtin_popcountll((long long)n)

#define REPname(a, b, c, d, e, ...) e
#define rep(...) REPname(__VA_ARGS__, REP3, REP2, REP1, REP0)(__VA_ARGS__)
#define REP0(x) for(int Counter_in_rep_macro = 0; Counter_in_rep_macro < (x); ++Counter_in_rep_macro)
#define REP1(i, x) for(int i = 0; i < (x); ++i)
#define REP2(i, l, r) for(int i = (l); i < (r); ++i)
#define REP3(i, l, r, c) for(int i = (l); i < (r); i += (c))

#define DREPname(a, b, c, d, e, ...) e
#define drep(...) DREPname(__VA_ARGS__, DREP3, DREP2, DREP1)(__VA_ARGS__)
#define DREP1(i, x) for(int i = (x)-1; i >= 0; --i)
#define DREP2(i, l, r) for(int i = (r)-1; i >= (l); --i)
#define DREP3(i, l, r, c) for(int i = (r)-1; i >= (l); i -= (c))

#pragma endregion

#pragma region aliases

using ll = long long;
using ld = long double;
using ull = unsigned long long;
using vi = std::vector<int>;
using vvi = std::vector<std::vector<int>>;
using vvvi = std::vector<std::vector<std::vector<int>>>;
using vll = std::vector<ll>;
using vvll = std::vector<vll>;
using vvvll = std::vector<vvll>;
using pii = std::pair<int, int>;
using pll = std::pair<long long, long long>;
template <class T = ll>
using V = std::vector<T>;
template <class T = ll>
using VV = V<V<T>>;
template <class T = ll>
using VVV = V<V<V<T>>>;
template <class T = ll>
using pqup = std::priority_queue<T, std::vector<T>, std::greater<T>>;
template <class T = ll>
using pqdn = std::priority_queue<T>;

#pragma endregion

#pragma region constants

const int inf = 1e9;
const long long INF = 1e18;
const long double pi = acos(-1);
const char dl = '\n';
const char sp = ' ';
int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};
int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};

const int mod_1000000007 = 1000000007;
const int mod_998244353 = 998244353;

#pragma endregion

#pragma region basic_operation

template <class T0, class T1, class T2>
inline bool in_range(T0 x, T1 lef, T2 rig) {
	return ((lef <= x) && (x < rig));
}

template <class T>
inline bool chmin(T &a, T b) {
	if(a > b) {
		a = b;
		return true;
	}
	return false;
}
template <class T>
inline bool chmax(T &a, T b) {
	if(a < b) {
		a = b;
		return true;
	}
	return false;
}

void Yes(bool f = 1) { std::cout << (f ? "Yes" : "No") << '\n'; }
void No() { std::cout << "No\n"; }
void YES(bool f = 1) { std::cout << (f ? "YES" : "NO") << '\n'; }
void NO() { std::cout << "NO\n"; }

template <class T>
void drop(T answer) {
	std::cout << answer << '\n';
	exit(0);
}

void err(bool flag = true) {
	if(!flag) return;
	std::cout << -1 << '\n';
	exit(0);
}

template <class T>
void vout(std::vector<T> const &v, bool vertically = 0) {
	if(vertically) {
		for(auto const &a : v) {
			std::cout << a << '\n';
		}
		return;
	}
	for(auto it = v.begin(); it != v.end(); it++) {
		std::cout << (*it);
		if(it != v.end() - 1) {
			std::cout << ' ';
		}
	}
	std::cout << '\n';
	return;
}

inline void print() { std::cout << '\n'; }
template <class T>
inline void print(T x) {
	std::cout << x << '\n';
	return;
}
template <typename Head, typename... Tail>
void print(Head H, Tail... T) {
	std::cout << H << " ";
	print(T...);
}

template <class T>
void add(std::vector<T> &v, T val) {
	for(auto &a : v) a += val;
	return;
}

template <class T>
T dup(T a, T b) {
	assert(b != 0);
	return (a + b - 1) / b;
}

template <class T>
T greatest_lower_multiple(T x, T d) {
	if(d == 0) return 0;
	if(d < 0) d *= -1;
	T y = x % d;
	if(y < 0) y += d;
	return x - y;
}

template <class T>
T least_upper_multiple(T x, T d) {
	return -greatest_lower_multiple(-x, d);
}

long long POW(long long a, long long n) {
	long long res = 1;
	while(n > 0) {
		if(n & 1) res = res * a;
		a = a * a;
		n >>= 1;
	}
	return res;
}

long long modpow(long long a, long long n, long long mod) {	 // a^n mod
	assert(n >= 0 && mod);
	if(mod == 1) return 0LL;
	long long res = 1;
	a %= mod;
	while(n > 0) {
		if(n & 1) res = res * a % mod;
		a = a * a % mod;
		n >>= 1;
	}
	return res < 0 ? res + mod : res;
}

// return x which satisfies a * x % mod == gcd(a, mod)
// not (mod | a)
long long modinv(long long a, long long mod) {
	long long b = mod, u = 1, v = 0;
	while(b) {
		long long t = a / b;
		a -= t * b;
		std::swap(a, b);
		u -= t * v;
		std::swap(u, v);
	}
	u %= mod;
	if(u < 0) u += mod;
	return u;
}

template <class T>
int lb(const std::vector<T> &a, const T x) {
	return std::distance(a.begin(), std::lower_bound(a.begin(), a.end(), x));
}
template <class T>
int ub(const std::vector<T> &a, const T x) {
	return std::distance(a.begin(), std::upper_bound(a.begin(), a.end(), x));
}
template <class T>
void unq_sort(std::vector<T> &a) {
	std::sort(a.begin(), a.end());
	a.erase(std::unique(a.begin(), a.end()), a.end());
}
template <class T>
std::vector<int> press(std::vector<T> &a) {
	auto vec = a;
	unq_sort(vec);
	std::vector<int> ret;
	for(auto &v : a) ret.push_back(lb(vec, v));
	return ret;
}

#pragma endregion

#pragma region input
#define VEC(type, name, size) \
	vector<type> name(size);  \
	scanner::INPUT(name)
#define VVEC(type, name, h, w)                     \
	vector<vector<type>> name(h, vector<type>(w)); \
	scanner::INPUT(name)
#define INT(...)     \
	int __VA_ARGS__; \
	scanner::INPUT(__VA_ARGS__)
#define LL(...)            \
	long long __VA_ARGS__; \
	scanner::INPUT(__VA_ARGS__)
#define STR(...)        \
	string __VA_ARGS__; \
	scanner::INPUT(__VA_ARGS__)
#define CHR(...)      \
	char __VA_ARGS__; \
	scanner::INPUT(__VA_ARGS__)
#define DBL(...)        \
	double __VA_ARGS__; \
	scanner::INPUT(__VA_ARGS__)
#define LD(...)              \
	long double __VA_ARGS__; \
	scanner::INPUT(__VA_ARGS__)
#define TPL3(type0, type1, type2, name)   \
	std::tuple<type0, type1, type2> name; \
	scanner::INPUT(name);
#define TPL4(type0, type1, type2, type3, name)   \
	std::tuple<type0, type1, type2, type3> name; \
	scanner::INPUT(name);

namespace scanner {
template <class T>
void scan(T &a) {
	std::cin >> a;
}

template <class T, class L>
void scan(std::pair<T, L> &p) {
	scan(p.first);
	scan(p.second);
}

template <class T0, class T1, class T2>
void scan(std::tuple<T0, T1, T2> &p) {
	T0 t0;
	T1 t1;
	T2 t2;
	scan(t0);
	scan(t1);
	scan(t2);
	p = std::make_tuple(t0, t1, t2);
}

template <class T0, class T1, class T2, class T3>
void scan(std::tuple<T0, T1, T2, T3> &p) {
	T0 t0;
	T1 t1;
	T2 t2;
	T3 t3;
	scan(t0);
	scan(t1);
	scan(t2);
	scan(t3);
	p = std::make_tuple(t0, t1, t2, t3);
}

template <class T>
void scan(std::vector<T> &a) {
	for(auto &i : a) scan(i);
}

void INPUT() {}
template <class Head, class... Tail>
void INPUT(Head &head, Tail &... tail) {
	scan(head);
	INPUT(tail...);
}
}  // namespace scanner

template <typename T1, typename T2>
std::istream &operator>>(std::istream &is, std::pair<T1, T2> &p) {
	is >> p.first >> p.second;
	return is;
}
#pragma endregion

#pragma region debug

#pragma region output
template <typename T1, typename T2>
std::ostream &std::operator<<(std::ostream &os, const std::pair<T1, T2> &p) {
	os << p.first << " " << p.second;
	return os;
}
template <class T>
std::ostream &std::operator<<(std::ostream &os, const std::vector<T> &v) {
	for(int i = 0; i < (int)v.size(); i++) {
		if(i) os << " ";
		os << v[i];
	}
	return os;
}
#pragma endregion

#pragma region view

namespace viewer {
void view(const long long e) {
	if(e == INF)
		std::cerr << "INF";
	else if(e == -INF)
		std::cerr << "-INF";
	else
		std::cerr << e;
}

void view(const int e) {
	if(e == inf)
		std::cerr << "inf";
	else if(e == -inf)
		std::cerr << "-inf";
	else
		std::cerr << e;
}

template <typename T>
void view(const T e) {
	std::cerr << e;
}

template <typename T, typename U>
void view(const std::pair<T, U> &p) {
	std::cerr << "(";
	view(p.first);
	std::cerr << ", ";
	view(p.second);
	std::cerr << ")";
}

template <class T0, class T1, class T2>
void view(const std::tuple<T0, T1, T2> &p) {
	std::cerr << "(";
	view(std::get<0>(p));
	std::cerr << ", ";
	view(std::get<1>(p));
	std::cerr << ", ";
	view(std::get<2>(p));
	std::cerr << ")";
}

template <class T0, class T1, class T2, class T3>
void view(const std::tuple<T0, T1, T2, T3> &p) {
	std::cerr << "(";
	view(std::get<0>(p));
	std::cerr << ", ";
	view(std::get<1>(p));
	std::cerr << ", ";
	view(std::get<2>(p));
	std::cerr << ", ";
	view(std::get<3>(p));
	std::cerr << ")";
}

template <typename T>
void view(const std::set<T> &s) {
	if(s.empty()) {
		std::cerr << "{ }";
		return;
	}
	std::cerr << "{ ";
	for(auto &t : s) {
		view(t);
		std::cerr << ", ";
	}
	std::cerr << "\b\b }";
}

template <typename T>
void view(const std::unordered_set<T> &s) {
	if(s.empty()) {
		std::cerr << "{ }";
		return;
	}
	std::cerr << "{ ";
	for(auto &t : s) {
		view(t);
		std::cerr << ", ";
	}
	std::cerr << "\b\b }";
}

template <typename T>
void view(const std::vector<T> &v) {
	if(v.empty()) {
		std::cerr << "{ }";
		return;
	}
	std::cerr << "{ ";
	for(const auto &e : v) {
		view(e);
		std::cerr << ", ";
	}
	std::cerr << "\b\b }";
}

template <typename T>
void view(const std::vector<std::vector<T>> &vv) {
	std::cerr << "{\n";
	for(const auto &v : vv) {
		std::cerr << "\t";
		view(v);
		std::cerr << '\n';
	}
	std::cerr << "}";
}

template <typename T, typename U>
void view(const std::vector<std::pair<T, U>> &v) {
	std::cerr << "{\n";
	for(const auto &c : v) {
		std::cerr << "\t(";
		view(c.first);
		std::cerr << ", ";
		view(c.second);
		std::cerr << ")\n";
	}
	std::cerr << "}";
}

template <class T0, class T1, class T2>
void view(const std::vector<std::tuple<T0, T1, T2>> &v) {
	if(v.empty()) {
		std::cerr << "{ }";
		return;
	}
	std::cerr << '{';
	for(const auto &t : v) {
		std::cerr << "\n\t";
		view(t);
		std::cerr << ",";
	}
	std::cerr << "\n}";
}

template <class T0, class T1, class T2, class T3>
void view(const std::vector<std::tuple<T0, T1, T2, T3>> &v) {
	if(v.empty()) {
		std::cerr << "{ }";
		return;
	}
	std::cerr << '{';
	for(const auto &t : v) {
		std::cerr << "\n\t";
		view(t);
		std::cerr << ",";
	}
	std::cerr << "\n}";
}

template <typename T, typename U>
void view(const std::map<T, U> &m) {
	std::cerr << "{\n";
	for(const auto &t : m) {
		std::cerr << "\t[";
		view(t.first);
		std::cerr << "] : ";
		view(t.second);
		std::cerr << '\n';
	}
	std::cerr << "}";
}

template <typename T, typename U>
void view(const std::unordered_map<T, U> &m) {
	std::cerr << "{\n";
	for(const auto &t : m) {
		std::cerr << "\t[";
		view(t.first);
		std::cerr << "] : ";
		view(t.second);
		std::cerr << '\n';
	}
	std::cerr << "}";
}
}  // namespace viewer

#pragma endregion

// when debugging : g++ foo.cpp -DLOCAL
#ifdef LOCAL
void debug_out() {}
template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {
	viewer::view(H);
	std::cerr << ", ";
	debug_out(T...);
}
#define debug(...)                                                \
	do {                                                          \
		std::cerr << __LINE__ << " [" << #__VA_ARGS__ << "] : ["; \
		debug_out(__VA_ARGS__);                                   \
		std::cerr << "\b\b]\n";                                   \
	} while(0)
#define dump(x)                                      \
	do {                                             \
		std::cerr << __LINE__ << " " << #x << " : "; \
		viewer::view(x);                             \
		std::cerr << '\n';                           \
	} while(0)

#else
#define debug(...) (void(0))
#define dump(x) (void(0))
#endif

#pragma endregion

#pragma region modint

const int mod = mod_1000000007;

template <int mod>
struct ModInt {
	int x;
	ModInt() : x(0) {}
	ModInt(long long y) : x(y >= 0 ? y % mod : (mod - (-y) % mod) % mod) {}
	ModInt &operator+=(const ModInt &p) {
		if((x += p.x) >= mod) x -= mod;
		return *this;
	}
	ModInt &operator-=(const ModInt &p) {
		if((x += mod - p.x) >= mod) x -= mod;
		return *this;
	}
	ModInt &operator*=(const ModInt &p) {
		x = (int)(1LL * x * p.x % mod);
		return *this;
	}
	ModInt &operator/=(const ModInt &p) {
		*this *= p.inverse();
		return *this;
	}
	ModInt operator-() const { return ModInt(-x); }
	ModInt operator+(const ModInt &p) const { return ModInt(*this) += p; }
	ModInt operator-(const ModInt &p) const { return ModInt(*this) -= p; }
	ModInt operator*(const ModInt &p) const { return ModInt(*this) *= p; }
	ModInt operator/(const ModInt &p) const { return ModInt(*this) /= p; }
	bool operator==(const ModInt &p) const { return x == p.x; }
	bool operator!=(const ModInt &p) const { return x != p.x; }
	ModInt inverse() const {
		int a = x, b = mod, u = 1, v = 0, t;
		while(b > 0) {
			t = a / b;
			swap(a -= t * b, b);
			swap(u -= t * v, v);
		}
		return ModInt(u);
	}
	ModInt pow(long long n) const {
		ModInt ret(1), mul(x);
		while(n > 0) {
			if(n & 1) ret *= mul;
			mul *= mul;
			n >>= 1;
		}
		return ret;
	}
	friend ostream &operator<<(ostream &os, const ModInt &p) { return os << p.x; }
	friend istream &operator>>(istream &is, ModInt &a) {
		long long t;
		is >> t;
		a = ModInt<mod>(t);
		return (is);
	}
	static int get_mod() { return mod; }
};
using mint = ModInt<mod>;

#pragma endregion

mint solve(int n) {
	int q;
	cin >> q;
	LL(A, B, C);
	INT(D, E, F, G);
	D--, E--, F--, G--;

	map<pair<int, int>, ll> non_empty;
	bool is_transposed = false;
	bool row_is_reversed = false;
	bool col_is_reversed = false;

	vector<int> row(n), col(n);
	// もとのi行目がrow[i]行目にいる
	vector<int> row_rev(n), col_rev(n);
	// もとのrow_rev[i]行目がi行目にいる

	iota(row.begin(), row.end(), 0);
	iota(col.begin(), col.end(), 0);
	iota(row_rev.begin(), row_rev.end(), 0);
	iota(col_rev.begin(), col_rev.end(), 0);

	auto value_in_original_mat = [&](int row, int col) {
		if(non_empty.count(pair(row, col))) return non_empty[pair(row, col)];
		return (((row + 1) * A + (col + 1) * B) % C + C) % C;
	};

	auto update = [&](int r, int c, ll val) {
		non_empty[(pair(r, c))] = val;
		return;
	};

	auto copy_cell = [&](int rfrom, int cfrom, int rto, int cto) {
		update(rto, cto, value_in_original_mat(rfrom, cfrom));
		return;
	};

	auto swap_permutaiton = [](vi &perm, vi &rev, int x, int y) -> void {
		// int from_x = rev[x];
		// int from_y = rev[y];
		// swap(perm[from_x], perm[from_y]);
		// swap(rev[x], rev[y]);
		swap(perm[x], perm[y]);
		swap(rev[perm[x]], rev[perm[y]]);
		return;
	};

	auto rl = [&is_transposed, &col_is_reversed, &row_is_reversed]() {  // ok
		if(!is_transposed)
			col_is_reversed = !col_is_reversed;
		else
			row_is_reversed = !row_is_reversed;
		is_transposed = !is_transposed;
		return;
	};

	auto rr = [&rl]() {	 // ok
		rep(3) rl();
		return;
	};

	auto swap_row = [&swap_permutaiton, &row_rev, &row](int r1, int r2) {
		swap_permutaiton(row, row_rev, r1, r2);
		return;
	};

	auto swap_col = [&swap_permutaiton, &col_rev, &col](int r1, int r2) {
		swap_permutaiton(col, col_rev, r1, r2);
		return;
	};

	auto rh = [&is_transposed, &col_is_reversed, &row_is_reversed]() {
		if(is_transposed)
			col_is_reversed = !col_is_reversed;
		else
			row_is_reversed = !row_is_reversed;
		return;
	};

	auto rv = [&is_transposed, &col_is_reversed, &row_is_reversed]() {
		if(is_transposed)
			row_is_reversed = !row_is_reversed;
		else
			col_is_reversed = !col_is_reversed;
		return;
	};

	auto former_index = [&q, &row_rev, &col_rev, &is_transposed, &row_is_reversed, &col_is_reversed, &n](int &r_now, int &c_now) {
		if(q == 5) debug(r_now, c_now);
		if(is_transposed) swap(r_now, c_now);
		if(row_is_reversed) r_now = n - 1 - r_now;
		if(q == 5) debug(r_now, c_now);
		if(col_is_reversed) c_now = n - 1 - c_now;
		if(q == 5) debug(r_now, c_now);
		r_now = row_rev[r_now];
		c_now = col_rev[c_now];
		if(q == 5) debug(r_now, c_now);
		if(q == 5) debug(is_transposed, row_rev, col_rev, row_is_reversed, col_is_reversed);
		return;
	};

	auto former_row_idx = [&row_is_reversed, &row_rev, &n](int idx) -> int {
		if(row_is_reversed) idx = n - 1 - idx;
		return row_rev[idx];
	};

	auto former_col_idx = [&col_is_reversed, &col_rev, &n](int idx) -> int {
		if(col_is_reversed) idx = n - 1 - idx;
		return col_rev[idx];
	};

	rep(q) {
		string s;
		cin >> s;
		if(s == "WR") {
			int r, c;
			ll v;
			cin >> r >> c >> v;
			r--;
			c--;
			former_index(r, c);
			update(r, c, v);
		} else if("CP" == s) {
			int rf, cf, rt, ct;
			cin >> rf >> cf >> rt >> ct;
			rf--;
			cf--;
			rt--;
			ct--;
			former_index(rf, cf);
			former_index(rt, ct);
			copy_cell(rf, cf, rt, ct);
		} else if("SR" == s) {
			int r1, r2;
			cin >> r1 >> r2;
			r1--;
			r2--;
			if(is_transposed) {
				r1 = former_col_idx(r1);
				r2 = former_col_idx(r2);
				swap_col(r1, r2);
			} else {
				r1 = former_row_idx(r1);
				r2 = former_row_idx(r2);
				swap_row(r1, r2);
			}
		} else if("SC" == s) {
			int r1, r2;
			cin >> r1 >> r2;
			r1--;
			r2--;
			if(!is_transposed) {
				r1 = former_col_idx(r1);
				r2 = former_col_idx(r2);
				swap_col(r1, r2);
			} else {
				r1 = former_row_idx(r1);
				r2 = former_row_idx(r2);
				swap_row(r1, r2);
			}
		} else if("RL" == s) {
			rl();
		} else if("RR" == s) {
			rr();
		} else if("RH" == s) {
			rh();
		} else if("RV" == s) {
			rv();
		} else {
			assert(false);
		}
	}

	auto hash = [&q, &value_in_original_mat, &D, &former_index, &E, &F, &G, &non_empty]() -> mint {
		mint h = 314159265;
		rep(r, D, E + 1) rep(c, F, G + 1) {
			h *= 31;
			int x, y;
			x = r, y = c;
			former_index(x, y);
			ll ad = value_in_original_mat(x, y);
			h += ad;

			if(q == 1) {
				debug(r, c);
				debug(x, y);
				debug(ad);
			}
		}
		return h;
	};

	debug(non_empty);

	return hash();
}

int main() {
	std::ios::sync_with_stdio(false);
	std::cin.tie(nullptr);
	std::cout << std::fixed << std::setprecision(15);
	srand((unsigned)time(NULL));

	int n;
	while(cin >> n && n) {
		cout << solve(n) << dl;
	}

	return 0;
}
accuracy: 1
time_ms: 30
memory_kb: 4364
final_rank: 7