#include <bits/stdc++.h>
using namespace std;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const long long maxn = 2e3 + 5, mod = 998244353, inf = mod;
long long n;
long long arr[maxn][maxn];
long long dp[maxn][maxn];
long long pw[maxn], fac[maxn];
struct bit {
  vector<long long> sum = vector<long long>(maxn, 0);
  void add(long long i, long long v) {
    for (; i < maxn; i += i & -i) {
      sum[i] += v;
      assert(sum[i] >= 0);
    }
  }
  long long get(long long i) {
    long long r = 0;
    for (; i; i -= i & -i) {
      r += sum[i];
    }
    return r;
  }
  void reset() {
    fill(begin(sum), end(sum), 0);
    for (long long i = (long long)1; i <= (long long)n; i++) add(i, 1);
  }
} avoid, can;
bool first[maxn], us[maxn];
signed main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> n;
  fac[0] = 1;
  for (long long i = (long long)1; i <= (long long)n; i++) {
    fac[i] = fac[i - 1] * i % mod;
  }
  for (long long i = (long long)1; i <= (long long)n; i++) {
    for (long long j = (long long)1; j <= (long long)n; j++) {
      cin >> arr[i][j];
    }
  }
  dp[0][0] = 1;
  for (long long i = (long long)1; i <= (long long)n; i++) {
    dp[i][0] = dp[i - 1][0] * i % mod;
    for (long long av = (long long)1; av <= (long long)i; av++) {
      dp[i][av] = (dp[i - 1][av - 1] * (i - av) % mod +
                   (av > 1 ? (av - 1) * dp[i - 1][av - 2] % mod : 0)) %
                  mod;
    }
  }
  pw[0] = 1;
  for (long long i = (long long)1; i <= (long long)n; i++) {
    pw[i] = pw[i - 1] * dp[n][n] % mod;
  }
  long long ans = 0;
  can.reset();
  for (long long i = (long long)1; i <= (long long)n; i++) {
    can.add(arr[1][i], -1);
    ans += can.get(arr[1][i] - 1) * fac[n - i] % mod * pw[n - 1] % mod;
    ans %= mod;
  }
  for (long long i = (long long)2; i <= (long long)n; i++) {
    avoid.reset();
    can.reset();
    fill(begin(first), end(first), 0);
    fill(begin(us), end(us), 0);
    long long av = n;
    for (long long j = (long long)1; j <= (long long)n; j++) {
      if (us[arr[i - 1][j]]) {
        long long notf = avoid.get(arr[i][j] - 1), tot = can.get(arr[i][j] - 1);
        ans += notf * dp[n - j][avoid.get(n) - 1] % mod * pw[n - i] % mod;
        ans += (tot - notf) * dp[n - j][avoid.get(n)] % mod * pw[n - i] % mod;
        ans %= mod;
      } else {
        long long notf = avoid.get(arr[i][j] - 1) - (arr[i - 1][j] < arr[i][j]);
        long long tot = can.get(arr[i][j] - 1) - (arr[i - 1][j] < arr[i][j]);
        ans += notf * dp[n - j][avoid.get(n) - 2] % mod * pw[n - i] % mod;
        ans +=
            (tot - notf) * dp[n - j][avoid.get(n) - 1] % mod * pw[n - i] % mod;
        ans %= mod;
      }
      first[arr[i - 1][j]] = 1;
      us[arr[i][j]] = 1;
      if (!first[arr[i][j]]) avoid.add(arr[i][j], -1);
      if (!us[arr[i - 1][j]]) avoid.add(arr[i - 1][j], -1);
      can.add(arr[i][j], -1);
    }
  }
  cout << ans;
}

### Prompt

Generate 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;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const long long maxn = 2e3 + 5, mod = 998244353, inf = mod;
long long n;
long long arr[maxn][maxn];
long long dp[maxn][maxn];
long long pw[maxn], fac[maxn];
struct bit {
  vector<long long> sum = vector<long long>(maxn, 0);
  void add(long long i, long long v) {
    for (; i < maxn; i += i & -i) {
      sum[i] += v;
      assert(sum[i] >= 0);
    }
  }
  long long get(long long i) {
    long long r = 0;
    for (; i; i -= i & -i) {
      r += sum[i];
    }
    return r;
  }
  void reset() {
    fill(begin(sum), end(sum), 0);
    for (long long i = (long long)1; i <= (long long)n; i++) add(i, 1);
  }
} avoid, can;
bool first[maxn], us[maxn];
signed main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> n;
  fac[0] = 1;
  for (long long i = (long long)1; i <= (long long)n; i++) {
    fac[i] = fac[i - 1] * i % mod;
  }
  for (long long i = (long long)1; i <= (long long)n; i++) {
    for (long long j = (long long)1; j <= (long long)n; j++) {
      cin >> arr[i][j];
    }
  }
  dp[0][0] = 1;
  for (long long i = (long long)1; i <= (long long)n; i++) {
    dp[i][0] = dp[i - 1][0] * i % mod;
    for (long long av = (long long)1; av <= (long long)i; av++) {
      dp[i][av] = (dp[i - 1][av - 1] * (i - av) % mod +
                   (av > 1 ? (av - 1) * dp[i - 1][av - 2] % mod : 0)) %
                  mod;
    }
  }
  pw[0] = 1;
  for (long long i = (long long)1; i <= (long long)n; i++) {
    pw[i] = pw[i - 1] * dp[n][n] % mod;
  }
  long long ans = 0;
  can.reset();
  for (long long i = (long long)1; i <= (long long)n; i++) {
    can.add(arr[1][i], -1);
    ans += can.get(arr[1][i] - 1) * fac[n - i] % mod * pw[n - 1] % mod;
    ans %= mod;
  }
  for (long long i = (long long)2; i <= (long long)n; i++) {
    avoid.reset();
    can.reset();
    fill(begin(first), end(first), 0);
    fill(begin(us), end(us), 0);
    long long av = n;
    for (long long j = (long long)1; j <= (long long)n; j++) {
      if (us[arr[i - 1][j]]) {
        long long notf = avoid.get(arr[i][j] - 1), tot = can.get(arr[i][j] - 1);
        ans += notf * dp[n - j][avoid.get(n) - 1] % mod * pw[n - i] % mod;
        ans += (tot - notf) * dp[n - j][avoid.get(n)] % mod * pw[n - i] % mod;
        ans %= mod;
      } else {
        long long notf = avoid.get(arr[i][j] - 1) - (arr[i - 1][j] < arr[i][j]);
        long long tot = can.get(arr[i][j] - 1) - (arr[i - 1][j] < arr[i][j]);
        ans += notf * dp[n - j][avoid.get(n) - 2] % mod * pw[n - i] % mod;
        ans +=
            (tot - notf) * dp[n - j][avoid.get(n) - 1] % mod * pw[n - i] % mod;
        ans %= mod;
      }
      first[arr[i - 1][j]] = 1;
      us[arr[i][j]] = 1;
      if (!first[arr[i][j]]) avoid.add(arr[i][j], -1);
      if (!us[arr[i - 1][j]]) avoid.add(arr[i - 1][j], -1);
      can.add(arr[i][j], -1);
    }
  }
  cout << ans;
}

```