#P10061. [SNOI2024] 矩阵

[SNOI2024] 矩阵

Problem Description

You need to maintain an n×nn \times n matrix AA, where the element in row ii and column jj is denoted as Ai,jA_{i, j}. Rows and columns are indexed starting from 11. Initially, Ai,j=(i+1)jmod998244353A_{i, j} = (i + 1)^j \bmod 998244353.

You need to support qq operations. Each operation is one of the following two types.

  • 1 x1 y1 x2 y21\ x_1\ y_1\ x_2\ y_2, where it is guaranteed that y2x2=y1x1y_2 - x_2 = y_1 - x_1. Rotate the sub-rectangle [x1,x2]×[y1,y2][x_1, x_2] \times [y_1, y_2] by 9090 degrees counterclockwise.
    • Specifically, let d=x2x1+1d = x_2 - x_1 + 1.
    • First, extract a d×dd \times d submatrix AA'. For all i,ji, j (1i,jd1 \le i, j \le d), set Ai,jAx1+i1,y1+j1A'_{i, j} \gets A_{x_1 + i - 1, y_1 + j - 1}.
    • Then rotate AA' to obtain a d×dd \times d submatrix BB', and set Bi,jAj,di+1B'_{i, j} \gets A'_{j, d - i + 1}.
    • Then write BB' back into AA. For all i,ji, j (1i,jd1 \le i, j \le d), set Ai+x11,j+y11Bi,jA_{i + x_1 - 1, j + y_1 - 1} \gets B'_{i, j}.
  • 2 x1 y1 x2 y2 d2\ x_1\ y_1\ x_2\ y_2\ d. Add dd to all elements in the sub-rectangle [x1,x2]×[y1,y2][x_1, x_2] \times [y_1, y_2].
    • Specifically, for each ii (x1ix2x_1 \le i \le x_2) and jj (y1jy2y_1 \le j \le y_2), set Ai,jAi,j+dA_{i, j} \gets A_{i, j} + d.

After all operations are finished, output this matrix. Since the output can be very large, please output the value of

$$\Biggl( \sum_{i = 1}^{n} \sum_{j = 1}^{n} A_{i, j} \times {12345}^{(i - 1) n + j} \Biggr) \bmod 1000000007$$

instead.

Input Format

The first line contains two integers n,qn, q, representing the matrix size and the number of operations.
The next qq lines each contain several numbers, describing one of the operations above.

Output Format

Output one integer, representing the answer modulo 10000000071000000007.

4 4
1 1 2 3 4
2 2 1 4 2 3
1 2 1 3 2
2 1 1 1 4 5

984660761

10 10
2 5 1 10 4 689412516
1 3 4 3 4
1 3 5 4 6
1 4 1 8 5
1 1 2 1 2
1 4 2 7 5
1 2 5 2 5
2 3 3 3 9 856075030
2 4 2 5 6 308750020
2 1 5 9 7 252732904

94292030

Hint

[Sample #1 Explanation]

For the first sample, the matrix is respectively

$$\begin{bmatrix} 2 & {\textcolor{red}{4}} & {\textcolor{red}{8}} & {\textcolor{red}{16}} \\ 3 & {\textcolor{red}{9}} & {\textcolor{red}{27}} & {\textcolor{red}{81}} \\ 4 & {\textcolor{red}{16}} & {\textcolor{red}{64}} & {\textcolor{red}{256}} \\ 5 & 25 & 125 & 625 \end{bmatrix} \to \begin{bmatrix} 2 & 16 & 81 & 256 \\ {\textcolor{blue}{3}} & {\textcolor{blue}{8}} & 27 & 64 \\ {\textcolor{blue}{4}} & {\textcolor{blue}{4}} & 9 & 16 \\ {\textcolor{blue}{5}} & {\textcolor{blue}{25}} & 125 & 625 \end{bmatrix} \to \begin{bmatrix} 2 & 16 & 81 & 256 \\ {\textcolor{red}{6}} & {\textcolor{red}{11}} & 27 & 64 \\ {\textcolor{red}{7}} & {\textcolor{red}{7}} & 9 & 16 \\ 8 & 28 & 125 & 625 \end{bmatrix}$$$$\to \begin{bmatrix} {\textcolor{blue}{2}} & {\textcolor{blue}{16}} & {\textcolor{blue}{81}} & {\textcolor{blue}{256}} \\ 11 & 7 & 27 & 64 \\ 6 & 7 & 9 & 16 \\ 8 & 28 & 125 & 625 \end{bmatrix} \to \begin{bmatrix} 7 & 21 & 86 & 261 \\ 11 & 7 & 27 & 64 \\ 6 & 7 & 9 & 16 \\ 8 & 28 & 125 & 625 \end{bmatrix}$$

The numbers corresponding to each rotation operation are marked in red, and the numbers corresponding to each addition operation are marked in blue.


[Sample #3]

See the attachments matrix/matrix3.in and matrix/matrix3.ans. This sample satisfies the constraints of test points 565 \sim 6.


[Sample #4]

See the attachments matrix/matrix3.in and matrix/matrix3.ans. This sample satisfies the constraints of test points 9109 \sim 10.


[Constraints]

For all testdata, it is guaranteed that 1n30001 \le n \le 3000 and 1q30001 \le q \le 3000.
For each operation, it is guaranteed that 1x1x2n1 \le x_1 \le x_2 \le n, 1y1y2n1 \le y_1 \le y_2 \le n, and 1d1091 \le d \le {10}^9.

Details are as follows:

Test Point ID nn \le qq \le Special Property
11 100100 30003000 None
22 30003000 A
343 \sim 4 20002000 B
565 \sim 6 30003000
787 \sim 8 20002000 None
9109 \sim 10 30003000

Special property A: It is guaranteed that there are no type 1 rotation operations.
Special property B: It is guaranteed that there are no type 2 addition operations.

Translated by ChatGPT 5