#include <bits/stdc++.h>
using namespace std;
inline long long read() {
  long long x = 0, f = 1;
  char c = getchar();
  for (; !isdigit(c); c = getchar())
    if (c == '-') f = -1;
  for (; isdigit(c); c = getchar()) x = x * 10 + c - '0';
  return x * f;
}
long long N, M;
const long long MAXN = 5000;
const long long INF = 2147483600;
const long long Mod = 998244353LL;
long long a[2010][2010];
long long g[2010][2010];
namespace BIT {
long long c[3][MAXN + 1];
inline void Add(long long n, long long k, long long x) {
  for (; k <= N; k += (k & (-k))) c[n][k] += x;
}
inline long long Query(long long n, long long k) {
  long long ans = 0;
  for (; k; k -= (k & (-k))) ans += c[n][k];
  return ans;
}
}  // namespace BIT
using namespace BIT;
long long f[2010], fac[2010];
bool vis[2010];
long long vall[2010];
int main() {
  N = read();
  for (long long i = 1; i <= N; i++) {
    for (long long j = 1; j <= N; j++) a[i][j] = read();
  }
  g[0][0] = 1;
  g[1][0] = 0;
  for (long long i = 2; i <= N; i++) {
    g[i][0] = 1LL * (i - 1) * (g[i - 1][0] + g[i - 2][0]) % Mod;
  }
  for (long long i = 1; i <= N; i++) {
    for (long long j = 1; j <= i; j++)
      g[i][j] = (g[i - 1][j - 1] + g[i][j - 1]) % Mod;
  }
  long long Fn = g[N][0];
  f[0] = 1;
  long long rak = 0;
  for (long long i = 1; i <= N; i++) f[i] = f[i - 1] * Fn % Mod;
  fac[0] = 1;
  long long ans = 0;
  for (long long i = 1; i <= N; i++) fac[i] = fac[i - 1] * i % Mod;
  for (long long i = 1; i <= N; i++)
    rak = (rak + (a[1][i] - 1 - Query(0, a[1][i])) * fac[N - i] % Mod) % Mod,
    Add(0, a[1][i], 1);
  (ans += rak * f[N - 1] % Mod) %= Mod;
  for (long long i = 2; i <= N; i++) {
    for (long long j = 1; j <= N; j++)
      c[0][j] = c[1][j] = c[2][j] = 0, vall[j] = vis[j] = 0;
    long long cnt = 0;
    for (long long j = 1; j <= N; j++) {
      if (vall[a[i - 1][j]]) ++cnt;
      long long d1 = a[i][j] - 1 - Query(0, a[i][j] - 1) +
                     Query(1, a[i][j] - 1) -
                     (a[i - 1][j] < a[i][j] && !vall[a[i - 1][j]]);
      long long a1 = a[i][j] - 1 - Query(2, a[i][j] - 1) -
                     (a[i - 1][j] < a[i][j] && !vis[a[i - 1][j]]) - d1;
      (ans += g[N - j][j - cnt] * d1 % Mod * f[N - i] % Mod) %= Mod;
      if (2 * j != cnt)
        (ans += g[N - j][j - cnt - 1] * a1 % Mod * f[N - i] % Mod) %= Mod;
      if (vall[a[i][j]]) Add(1, a[i][j], 1);
      if (vall[a[i - 1][j]]) Add(1, a[i - 1][j], 1);
      vall[a[i][j]]++;
      vall[a[i - 1][j]]++;
      Add(0, a[i][j], 1);
      Add(0, a[i - 1][j], 1);
      Add(2, a[i][j], 1);
      vis[a[i][j]] = true;
      if (vall[a[i][j]] == 2) ++cnt;
    }
  }
  cout << ans << endl;
  return 0;
}

### Prompt

Your task is to create a Cpp solution to the following problem:

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 998 244 353.

Input

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 a_{i,j} (1 ≤ a_{i,j} ≤ n) — the elements of a.

It is guaranteed that a is a beautiful matrix.

Output

Print one integer — the rarity of matrix a, taken modulo 998 244 353.

Examples

Input


2
2 1
1 2


Output


1


Input


3
1 2 3
2 3 1
3 1 2


Output


1


Input


3
1 2 3
3 1 2
2 3 1


Output


3

Note

There are only 2 beautiful matrices of size 2 × 2:

<image>

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

<image>

### Response

```cpp
#include <bits/stdc++.h>
using namespace std;
inline long long read() {
  long long x = 0, f = 1;
  char c = getchar();
  for (; !isdigit(c); c = getchar())
    if (c == '-') f = -1;
  for (; isdigit(c); c = getchar()) x = x * 10 + c - '0';
  return x * f;
}
long long N, M;
const long long MAXN = 5000;
const long long INF = 2147483600;
const long long Mod = 998244353LL;
long long a[2010][2010];
long long g[2010][2010];
namespace BIT {
long long c[3][MAXN + 1];
inline void Add(long long n, long long k, long long x) {
  for (; k <= N; k += (k & (-k))) c[n][k] += x;
}
inline long long Query(long long n, long long k) {
  long long ans = 0;
  for (; k; k -= (k & (-k))) ans += c[n][k];
  return ans;
}
}  // namespace BIT
using namespace BIT;
long long f[2010], fac[2010];
bool vis[2010];
long long vall[2010];
int main() {
  N = read();
  for (long long i = 1; i <= N; i++) {
    for (long long j = 1; j <= N; j++) a[i][j] = read();
  }
  g[0][0] = 1;
  g[1][0] = 0;
  for (long long i = 2; i <= N; i++) {
    g[i][0] = 1LL * (i - 1) * (g[i - 1][0] + g[i - 2][0]) % Mod;
  }
  for (long long i = 1; i <= N; i++) {
    for (long long j = 1; j <= i; j++)
      g[i][j] = (g[i - 1][j - 1] + g[i][j - 1]) % Mod;
  }
  long long Fn = g[N][0];
  f[0] = 1;
  long long rak = 0;
  for (long long i = 1; i <= N; i++) f[i] = f[i - 1] * Fn % Mod;
  fac[0] = 1;
  long long ans = 0;
  for (long long i = 1; i <= N; i++) fac[i] = fac[i - 1] * i % Mod;
  for (long long i = 1; i <= N; i++)
    rak = (rak + (a[1][i] - 1 - Query(0, a[1][i])) * fac[N - i] % Mod) % Mod,
    Add(0, a[1][i], 1);
  (ans += rak * f[N - 1] % Mod) %= Mod;
  for (long long i = 2; i <= N; i++) {
    for (long long j = 1; j <= N; j++)
      c[0][j] = c[1][j] = c[2][j] = 0, vall[j] = vis[j] = 0;
    long long cnt = 0;
    for (long long j = 1; j <= N; j++) {
      if (vall[a[i - 1][j]]) ++cnt;
      long long d1 = a[i][j] - 1 - Query(0, a[i][j] - 1) +
                     Query(1, a[i][j] - 1) -
                     (a[i - 1][j] < a[i][j] && !vall[a[i - 1][j]]);
      long long a1 = a[i][j] - 1 - Query(2, a[i][j] - 1) -
                     (a[i - 1][j] < a[i][j] && !vis[a[i - 1][j]]) - d1;
      (ans += g[N - j][j - cnt] * d1 % Mod * f[N - i] % Mod) %= Mod;
      if (2 * j != cnt)
        (ans += g[N - j][j - cnt - 1] * a1 % Mod * f[N - i] % Mod) %= Mod;
      if (vall[a[i][j]]) Add(1, a[i][j], 1);
      if (vall[a[i - 1][j]]) Add(1, a[i - 1][j], 1);
      vall[a[i][j]]++;
      vall[a[i - 1][j]]++;
      Add(0, a[i][j], 1);
      Add(0, a[i - 1][j], 1);
      Add(2, a[i][j], 1);
      vis[a[i][j]] = true;
      if (vall[a[i][j]] == 2) ++cnt;
    }
  }
  cout << ans << endl;
  return 0;
}

```