Matrix Operation#3287

Matrix Operation

2 seconds
64 megabytes
Very hard

You are a student looking for a job. Today, you took an employment examination at an IT company. You were asked to write an efficient program to perform several operations on a square matrix.

Initially, you are given an N \times N matrix. Then you are given a series of operations. All operations except the last modify the matrix. After all operations, you must output a hash value computed from the resulting matrix as described below.

The available operations are:

Input

The first line of each test case contains nine integers: , , , , , , , , .

  • 1 \le N, Q \le 40\,000

  • 1 \le A, B, C \le 1\,000\,000

  • 1 \le r, c \le N

  • A_{r,c} = (r \times A + c \times B) \bmod C is the initial matrix value at position

  • 1 \le D \le E \le N, 1 \le F \le G \le N, E-D \le 1\,000, G-F \le 1\,000

Next, lines follow, each describing an operation in the format above.

Output

Output the integer hash value computed from the final matrix using the following algorithm:

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

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

Examples

Example 1

Loading
Loading