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

Followings are the detail of the operations:

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



Input

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

Output

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


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


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

Examples

Input

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


Output

676573821


Input

2 1 3 6 12 1 2 1 2
RL


Output

676636559


Input

2 1 3 6 12 1 2 1 2
RH


Output

676547189


Input

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


Output

458797120

Solution (in CPP):

Solution:
#include<bits/stdc++.h>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define HUGE_NUM 99999999999999999
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;


#define NUM 40005

struct Info{
	Info(){
		row = col = 0;
	}

	Info(int arg_row,int arg_col){
		row = arg_row;
		col = arg_col;
	}
	bool operator<(const struct Info &arg) const{
		if(row != arg.row){
			return row < arg.row;
		}else{
			return col < arg.col;
		}
	}
	int row,col;
};

ll N,num_query,A,B,C,D,E,F,G;
int clock_table[8] = {1,2,3,0,7,4,5,6};
int rev_clock_table[8] = {3,0,1,2,5,6,7,4};
int LR[8] = {4,5,6,7,0,1,2,3};
int UD[8] = {6,7,4,5,2,3,0,1};
map<Info,ll> MAP;

int STATE;
int row_table[NUM],col_table[NUM];

ll get_value(ll row,ll col){

	auto at = MAP.find(Info(row,col));

	if(at == MAP.end()){
		MAP[Info(row,col)] = (row*A+col*B)%C;
	}
	return MAP[Info(row,col)];
}

Info get_pos(ll row,ll col){

	Info ret;

	switch(STATE){
	case 0:
		ret.row = row;
		ret.col = col;
		break;
	case 1:
		ret.row = N+1-col;
		ret.col = row;
		break;
	case 2:
		ret.row = N+1-row;
		ret.col = N+1-col;
		break;
	case 3:
		ret.row = col;
		ret.col = N+1-row;
		break;
	case 4:
		ret.row = row;
		ret.col = N+1-col;
		break;
	case 5:
		ret.row = col;
		ret.col = row;
		break;
	case 6:
		ret.row = N+1-row;
		ret.col = col;
		break;
	case 7:
		ret.row = N+1-col;
		ret.col = N+1-row;
		break;
	}

	return ret;
}

Info get_loc(ll row,ll col){

	Info ret = get_pos(row,col);

	ret.row = row_table[ret.row];
	ret.col = col_table[ret.col];

	return ret;
}

Info get_loc2(ll row,ll col){

	return get_pos(row,col);
}


int main(){

	scanf("%lld %lld %lld %lld %lld %lld %lld %lld %lld",&N,&num_query,&A,&B,&C,&D,&E,&F,&G);

	STATE = 0;

	for(int i = 1; i <= N; i++){
		row_table[i] = i;
		col_table[i] = i;
	}

	char buf[3];

	ll tmp_row1,tmp_col1,tmp_row2,tmp_col2,tmp_value;
	Info info,info2;

	for(int loop = 0; loop < num_query; loop++){

		scanf("%s",buf);

		switch(buf[0]){
		case 'W': //書き込み命令

			scanf("%lld %lld %lld",&tmp_row1,&tmp_col1,&tmp_value);
			info = get_loc(tmp_row1,tmp_col1);
			MAP[Info(info.row,info.col)] = tmp_value;

			break;

		case 'C': //コピー命令

			scanf("%lld %lld %lld %lld",&tmp_row1,&tmp_col1,&tmp_row2,&tmp_col2);
			info = get_loc(tmp_row1,tmp_col1);
			info2 = get_loc(tmp_row2,tmp_col2);
			MAP[Info(info2.row,info2.col)] = get_value(info.row,info.col);

			break;
		case 'S':

			if(buf[1] == 'R'){ //行スワップ

				scanf("%lld %lld",&tmp_row1,&tmp_row2);
				info = get_loc2(tmp_row1,0);
				info2 = get_loc2(tmp_row2,0);

				if(STATE%2){
					swap(col_table[info.col],col_table[info2.col]);
				}else{
					swap(row_table[info.row],row_table[info2.row]);
				}

			}else{ //列スワップ

				scanf("%lld %lld",&tmp_col1,&tmp_col2);
				info = get_loc2(0,tmp_col1);
				info2 = get_loc2(0,tmp_col2);

				if(STATE%2){
					swap(row_table[info.row],row_table[info2.row]);
				}else{
					swap(col_table[info.col],col_table[info2.col]);
				}
			}

			break;
		case 'R':

			switch(buf[1]){
			case 'L': //反時計90

				STATE = rev_clock_table[STATE];

				break;
			case 'R': //時計90

				STATE = clock_table[STATE];

				break;
			case 'H': //上下反転

				STATE = UD[STATE];

				break;
			case 'V': //左右反転

				STATE = LR[STATE];
				break;
			}
			break;
		}
	}


	ll ans = 314159265;

	for(int row = D; row <= E;row++){
		for(int col = F; col <=G; col++){

			info = get_loc(row,col);

			ans = ans*31 + get_value(info.row,info.col);
			ans %=MOD;
	    }
	}

	printf("%lld\n",ans);

	return 0;
}