CF1085G.Beautiful Matrix
省选/NOI-
通过率:0%
时间限制:4.00s
内存限制:256MB
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
Petya collects beautiful matrix.
A matrix of size n×n is beautiful if:
- All elements of the matrix are integers between 1 and n;
- For every row of the matrix, all elements of this row are different;
- For every pair of vertically adjacent elements, these elements are different.
Today Petya bought a beautiful matrix a of size n×n, and now he wants to determine its rarity.
The rarity of the matrix is its index in the list of beautiful matrices of size n×n, sorted in lexicographical order. Matrix comparison is done row by row. (The index of lexicographically smallest matrix is zero).
Since the number of beautiful matrices may be huge, Petya wants you to calculate the rarity of the matrix a modulo 998244353.
佩佳收集漂亮的矩阵。
一个 n×n 的矩阵被称为漂亮矩阵,当且仅当满足以下条件:
- 矩阵中所有元素均为介于 1 和 n 之间的整数;
- 矩阵的每一行中,所有元素互不相同;
- 对于任意一对垂直相邻的元素(即位于同一列、相邻两行中的元素),这两个元素互不相同。
今天佩佳购买了一个 n×n 的漂亮矩阵 a,现在他想确定该矩阵的稀有度。
矩阵的稀有度定义为:在所有 n×n 漂亮矩阵按字典序升序排列所得的列表中,该矩阵所处的索引值(从 0 开始计数,即字典序最小的矩阵索引为 0)。
由于漂亮矩阵的总数可能非常巨大,佩佳希望你计算矩阵 a 的稀有度对 998244353 取模的结果。
输入格式
The first line contains one integer n (1≤n≤2000) — the number of rows and columns in a.
Each of the next n lines contains n integers ai,j (1≤ai,j≤n) — the elements of a.
It is guaranteed that a is a beautiful matrix.
第一行包含一个整数 n(1≤n≤2000)—— 表示矩阵 a 的行数与列数。
接下来的 n 行,每行包含 n 个整数 ai,j(1≤ai,j≤n)—— 表示矩阵 a 的元素。
保证 a 是一个优美的矩阵。
输出格式
Print one integer — the rarity of matrix a, taken modulo 998244353.
输出一个整数——矩阵 a 的稀有度对 998244353 取模的结果。
输入输出样例
输入#1
2 2 1 1 2
输出#1
1
输入#2
3 1 2 3 2 3 1 3 1 2
输出#2
1
输入#3
3 1 2 3 3 1 2 2 3 1
输出#3
3
说明/提示
There are only 2 beautiful matrices of size 2×2:

There are the first 5 beautiful matrices of size 3×3 in lexicographical order:

仅有 2 个 2×2 的优美矩阵:

按字典序排列的前 5 个 3×3 优美矩阵如下:

输入解题思路,AI测评打分。不知道怎么写?