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

Solution in Ruby (judge verdict: Time Limit Exceeded)

N, Q, A, B, C, D, E, F, G = gets.split.map(&:to_i)

row = (D..E).to_a
col = (F..G).to_a

op = []
Q.times do
    c, i, j, x, y = gets.chomp.split
    op << [c, i.to_i, j.to_i, x.to_i, y.to_i]
end

update = []
flag = true
op.reverse.each do |c, i, j, x, y|
    case c
    when 'WR'
        update.unshift [c, i, j, x, nil]
    when 'CP'
        update.unshift [c, i, j, x, y]
    when 'RV'
        col.map! {|c| N + 1 - c}
        update.each do |u|
            u[2] = N + 1 - u[2]
            u[4] = N + 1 - u[4] if u[0] == 'CP'
        end
    when 'RH'
        row.map! {|r| N + 1 - r}
        update.each do |u|
            u[1] = N + 1 - u[1]
            u[3] = N + 1 - u[3] if u[0] == 'CP'
        end
    when 'RL'
        flag = !flag
        row, col = col, row.map {|r| N + 1 - r}
        update.each do |u|
            u[1], u[2] = u[2], N + 1 - u[1]
            u[3], u[4] = u[4], N + 1 - u[3] if u[0] == 'CP'
        end
    when 'RR'
        flag = !flag
        row, col = col.map {|c| N + 1 - c}, row
        update.each do |u|
            u[1], u[2] = N + 1 - u[2], u[1]
            u[3], u[4] = N + 1 - u[4], u[3] if u[0] == 'CP'
        end
    when 'SR'
        h = {i => j, j => i}
        row.map! {|r| h[r] || r}
        update.each do |u|
            u[1] = h[u[1]] || u[1]
            u[3] = h[u[3]] || u[3] if u[0] == 'CP'
        end
    when 'SC'
        h = {i => j, j => i}
        col.map! {|c| h[c] || c}
        update.each do |u|
            u[2] = h[u[2]] || u[2]
            u[4] = h[u[4]] || u[4] if u[0] == 'CP'
        end
    end
end

target = []
hash = {}
if flag
    row.each do |r|
        col.each do |c|
            k = [r, c]
            v = (A * r + B * c) % C
            target << k
            hash[k] = v
        end
    end
else
    col.each do |c|
        row.each do |r|
            k = [r, c]
            v = (A * r + B * c) % C
            target << k
            hash[k] = v
        end
    end
end

update.each do |c, i, j, x, y|
    if c == 'WR'
        hash[[i, j]] = x
    else
        hash[[x, y]] = (hash[[i, j]] ||= (A * i + B * j) % C)
    end
end

h = 314159265
target.each do |k|
    h = (h * 31 + hash[k]) % 1_000_000_007
end
p h