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 JAVA):

Solution:

import java.util.*;

public class Main {
	public static void main(String[] args) {
		new Main().run();
	}

	void run() {
		solver();
	}

	int N;
	int Q;
	long A, B, C;
	int[] row;
	int[] column;
	int[][] back = new int[][] { { 1, 0 }, { 0, 1 } };
	final int[][] RR = new int[][] { { 0, -1 }, { 1, 0 } };
	final int[][] RL = new int[][] { { 0, 1 }, { -1, 0 } };
	final int[][] RH = new int[][] { { 1, 0 }, { 0, -1 } };
	final int[][] RV = new int[][] { { -1, 0 }, { 0, 1 } };
	HashMap<List<Integer>, Long> map = new HashMap<>();
	final long MODULO = 1_000_000_000 + 7;

	void solver() {
		Scanner sc = new Scanner(System.in);
		N = sc.nextInt();
		Q = sc.nextInt();
		A = sc.nextLong();
		B = sc.nextLong();
		C = sc.nextLong();
		row = new int[N + 1];
		column = new int[N + 1];
		for (int i = 0; i <= N; ++i) {
			row[i] = i;
			column[i] = i;
		}
		int D = sc.nextInt();
		int E = sc.nextInt();
		int F = sc.nextInt();
		int G = sc.nextInt();
		for (int q = 0; q < Q; ++q) {
			String s = sc.next();
			if (s.equals("WR")) {
				int r = sc.nextInt();
				int c = sc.nextInt();
				long v = sc.nextLong();
				int[][] pos = get(r, c);
				c = pos[0][0];
				r = pos[1][0];
				map.put(Arrays.asList(row[r], column[c]), v);
			} else if (s.equals("CP")) {
				int r1 = sc.nextInt();
				int c1 = sc.nextInt();
				int r2 = sc.nextInt();
				int c2 = sc.nextInt();
				long v = getValue(r1, c1);
				int[][] pos = get(r2, c2);
				int r = pos[1][0];
				int c = pos[0][0];
				map.put(Arrays.asList(row[r], column[c]), v);
			} else if (s.equals("RL")) {
				back = mul(back, RR);
			} else if (s.equals("RR")) {
				back = mul(back, RL);
			} else if (s.equals("RH")) {
				back = mul(back, RH);
			} else if (s.equals("RV")) {
				back = mul(back, RV);
			} else if (s.equals("SR")) {
				int r1 = sc.nextInt();
				int r2 = sc.nextInt();
				int[][] v1 = get(r1, 0);
				int[][] v2 = get(r2, 0);
				swap(v1, v2);
			} else if (s.equals("SC")) {
				int c1 = sc.nextInt();
				int c2 = sc.nextInt();
				int[][] v1 = get(0, c1);
				int[][] v2 = get(0, c2);
				swap(v1, v2);
			}
		}

		long h = 314159265;
		for (int r = D; r <= E; ++r) {
			for (int c = F; c <= G; ++c) {
				h = (31 * h + getValue(r, c)) % MODULO;
			}
		}
		System.out.println(h);
		sc.close();
	}

	long getValue(int r, int c) {
		int[][] pos = get(r, c);
		int rp = pos[1][0];
		int cp = pos[0][0];
		if (map.containsKey(Arrays.asList(row[rp], column[cp]))) {
			return map.get(Arrays.asList(row[rp], column[cp])) % MODULO;
		} else {
			return init(row[rp], column[cp]);
		}
	}

	int[][] get(int r, int c) {
		int[][] v = new int[][] { { c }, { r } };
		v = mul(back, v);
		if (v[0][0] < 0)
			v[0][0] += N + 1;
		if (v[1][0] < 0)
			v[1][0] += N + 1;
		return v;
	}

	long init(long r, long c) {
		return (r * A + c * B) % C;
	}

	void swap(int[][] v1, int[][] v2) {
		int c1 = v1[0][0];
		int c2 = v2[0][0];
		int r1 = v1[1][0];
		int r2 = v2[1][0];
		int d = row[r1];
		row[r1] = row[r2];
		row[r2] = d;
		d = column[c1];
		column[c1] = column[c2];
		column[c2] = d;
	}

	int[][] mul(int[][] a, int[][] b) {
		assert a[0].length == b.length;
		int n = a[0].length;
		int[][] ret = new int[a.length][b[0].length];
		for (int i = 0; i < a.length; ++i) {
			for (int j = 0; j < b[0].length; ++j) {
				for (int k = 0; k < n; ++k) {
					ret[i][j] += a[i][k] * b[k][j];
				}
			}
		}
		return ret;
	}

	void tr(Object... objects) {
		System.out.println(Arrays.deepToString(objects));
	}
}