CF1864G.Magic Square

普及/提高-

通过率:0%

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

Aquamoon has a Rubik's Square which can be seen as an n×nn \times n matrix, the elements of the matrix constitute a permutation of numbers 1,,n21, \ldots, n^2 .

Aquamoon can perform two operations on the matrix:

  • Row shift, i.e. shift an entire row of the matrix several positions (at least 11 and at most n1n-1 ) to the right. The elements that come out of the right border of the matrix are moved to the beginning of the row. For example, shifting a row (abc)\begin{pmatrix} a & b & c \end{pmatrix} by 22 positions would result in (bca)\begin{pmatrix} b & c & a \end{pmatrix} ;
  • Column shift, i.e. shift an entire column of the matrix several positions (at least 11 and at most n1n-1 ) downwards. The elements that come out of the lower border of the matrix are moved to the beginning of the column. For example, shifting a column (abc)\begin{pmatrix} a \\ b \\ c \end{pmatrix} by 22 positions would result in (bca)\begin{pmatrix} b\\c\\a \end{pmatrix} .

The rows are numbered from 11 to nn from top to bottom, the columns are numbered from 11 to nn from left to right. The cell at the intersection of the xx -th row and the yy -th column is denoted as (x,y)(x, y) .

Aquamoon can perform several (possibly, zero) operations, but she has to obey the following restrictions:

  • each row and each column can be shifted at most once;
  • each integer of the matrix can be moved at most twice;
  • the offsets of any two integers moved twice cannot be the same. Formally, if integers aa and bb have been moved twice, assuming aa has changed its position from (x1,y1)(x_1,y_1) to (x2,y2)(x_2,y_2) , and bb has changed its position from (x3,y3)(x_3,y_3) to (x4,y4)(x_4,y_4) , then x2x1≢x4x3(modn)x_2-x_1 \not\equiv x_4-x_3 \pmod{n} or y2y1≢y4y3(modn)y_2-y_1 \not\equiv y_4-y_3 \pmod{n} .

Aquamoon wonders in how many ways she can transform the Rubik's Square from the given initial state to a given target state. Two ways are considered different if the sequences of applied operations are different. Since the answer can be very large, print the result modulo 998244353998\,244\,353 .

输入格式

Each test contains multiple test cases. The first line contains the number of test cases tt ( 1t21041 \le t \le 2\cdot 10^4 ). The description of the test cases follows.

The first line of each test case contains an integer nn ( 3n5003\le n \le 500 ).

The ii -th of the following nn lines contains nn integers ai1,,aina_{i1}, \ldots, a_{in} , representing the ii -th row of the initial matrix ( 1aijn21 \le a_{ij} \le n^2 ).

The ii -th of the following nn lines contains nn integers bi1,,binb_{i1}, \ldots, b_{in} , representing the ii -th row of the target matrix ( 1bijn21 \le b_{ij} \le n^2 ).

It is guaranteed that both the elements of the initial matrix and the elements of the target matrix constitute a permutation of numbers 1,,n21, \ldots, n^2 .

It is guaranteed that the sum of n2n^2 over all test cases does not exceed 250000250\,000 .

输出格式

For each test case, if it is possible to convert the initial state to the target state respecting all the restrictions, output one integer — the number of ways to do so, modulo 998244353998\,244\,353 .

If there is no solution, print a single integer 00 .

输入输出样例

  • 输入#1

    4
    3
    1 2 3
    4 5 6
    7 8 9
    7 2 3
    1 4 5
    6 8 9
    3
    1 2 3
    4 5 6
    7 8 9
    3 2 1
    6 5 4
    9 7 8
    3
    1 2 3
    4 5 6
    7 8 9
    7 8 1
    2 3 4
    5 6 9
    3
    1 2 3
    4 5 6
    7 8 9
    3 8 4
    5 1 9
    7 6 2

    输出#1

    1
    0
    0
    4

说明/提示

In the first test case, the only way to transform the initial matrix to the target one is to shift the second row by 11 position to the right, and then shift the first column by 11 position downwards.

In the second test case, it can be shown that there is no correct way to transform the matrix, thus, the answer is 00 .

首页