func_code_string: #include <bits/stdc++.h>   using namespace std;   int n, ans;   int f[2005][2005], A[2005][2005];   int mul[2005], tr1[2005], tr2[2005], vis[2005], lck[2005];   int Getsum(int *tree, int x) {     int ans = 0;     for (int i = x; i; i -= i & -i) ans += tree[i];     return ans;   }   void Update(int *tree, int x, int v) {     for (int i = x; i <= n; i += i & -i) tree[i] += v;   }   int main() {     scanf( %d , &n);     f[0][0] = 1;     for (int i = 1; i <= n; i++) {       for (int j = 0; j <= i; j++) {         if (j == 0)           f[i][j] = (long long)f[i - 1][j] * i % 998244353;         else {           f[i][j] = (f[i][j] + (long long)(i - j) * f[i - 1][j - 1] % 998244353) %                     998244353;           if (j >= 2)             f[i][j] =                 (f[i][j] + (long long)(j - 1) * f[i - 1][j - 2] % 998244353) %                 998244353;         }       }     }     for (int i = 1; i <= n; i++)       for (int j = 1; j <= n; j++) scanf( %d , &A[i][j]);     mul[0] = 1;     for (int i = 1; i <= n; i++)       mul[i] = (long long)mul[i - 1] * f[n][n] % 998244353;     for (int i = 1; i <= n; i++) {       int s = A[1][i] - 1 - Getsum(tr1, A[1][i] - 1);       ans = (ans +              (long long)s * f[n - i][0] % 998244353 * mul[n - 1] % 998244353) %             998244353;       Update(tr1, A[1][i], 1);     }     for (int i = 2; i <= n; i++) {       for (int j = 1; j <= n; j++) tr1[j] = tr2[j] = vis[j] = lck[j] = 0;       int all = 0;       for (int j = 1; j <= n; j++) {         int s = A[i][j] - 1 - Getsum(tr1, A[i][j] - 1) -                 ((A[i - 1][j] < A[i][j]) && !vis[A[i - 1][j]]);         int s1 = Getsum(tr2, A[i][j] - 1);         int s2 = s - s1;         lck[A[i - 1][j]] = 1;         if (!vis[A[i - 1][j]]) Update(tr2, A[i - 1][j], 1), all++;         if (s1)           ans = (ans + (long long)s1 * f[n - j][n - j - (all - 1)] % 998244353 *                            mul[n - i] % 998244353) %                 998244353;         if (s2)           ans = (ans + (long long)s2 * f[n - j][n - j - all] % 998244353 *                            mul[n - i] % 998244353) %                 998244353;         vis[A[i][j]] = 1;         if (lck[A[i][j]]) Update(tr2, A[i][j], -1), all--;         Update(tr1, A[i][j], 1);       }     }     printf( %d n , ans);   }   