#include <bits/stdc++.h>
using namespace std;
const int MOD = 998244353;
int add(int x, int y) {
  x += y;
  if (x >= MOD) x -= MOD;
  return x;
}
int sub(int x, int y) {
  x -= y;
  if (x < 0) x += MOD;
  return x;
}
int mul(int x, int y) { return 1ll * x * y % MOD; }
const int MAXN = 2000 + 100;
int dp[MAXN][MAXN];
int a[MAXN][MAXN];
struct BIT {
  int sum[MAXN];
  int n;
  void init(int n_) {
    n = n_;
    fill(sum, sum + n + 1, 0);
  }
  int ask(int x) {
    int res = 0;
    while (x) {
      res += sum[x];
      x -= x & (-x);
    }
    return res;
  }
  void add(int id) {
    const int k = 1;
    while (id <= n) {
      sum[id] += k;
      id += id & (-id);
    }
  }
} cnt_this, cnt_both, cnt_lst;
int f(int n, int k) {
  if (k < 0) return 0;
  return dp[n][k];
}
int main() {
  dp[0][0] = 1;
  for (int i = 1; i < MAXN; i++) {
    dp[i][0] = mul(dp[i - 1][0], i);
    for (int j = 1; j <= i; j++) {
      dp[i][j] = sub(dp[i][j - 1], dp[i - 1][j - 1]);
    }
  }
  int n;
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      scanf("%d", &a[i][j]);
    }
  }
  int ans = 0;
  for (int i = 0; i < n; i++) {
    int remain_row = 1;
    for (int c = i + 1; c < n; c++) remain_row = mul(remain_row, dp[n][n]);
    vector<bool> this_row(n + 1), lst_row(n + 1);
    cnt_this.init(n);
    cnt_both.init(n);
    cnt_lst.init(n);
    int both = 0;
    (void)0;
    for (int j = 0; j < n; j++) {
      if (i > 0) {
        lst_row[a[i - 1][j]] = true;
        cnt_lst.add(a[i - 1][j]);
        if (this_row[a[i - 1][j]]) {
          both++;
          cnt_both.add(a[i - 1][j]);
        }
      } else {
        both = 0;
      }
      int re_slot = n - j - 1;
      (void)0;
      int kase1 = f(re_slot, re_slot - (j + 1 - both));
      int kase2 = f(re_slot, re_slot - (j + 1 - (both + 1)));
      if (i == 0) {
        kase1 = f(re_slot, 0);
      }
      int sml_num = a[i][j] - 1 - cnt_this.ask(a[i][j]);
      if (i > 0 && a[i - 1][j] < a[i][j] && !this_row[a[i - 1][j]]) sml_num--;
      int num2 = cnt_lst.ask(a[i][j] - 1) - cnt_both.ask(a[i][j] - 1);
      if (i > 0 && a[i - 1][j] < a[i][j] && !this_row[a[i - 1][j]]) num2--;
      int num1 = sml_num - num2;
      (void)0;
      (void)0;
      (void)0;
      assert(num1 >= 0);
      assert(num2 >= 0);
      int total = mul(remain_row, add(mul(kase1, num1), mul(kase2, num2)));
      ans = add(ans, total);
      this_row[a[i][j]] = true;
      if (lst_row[a[i][j]]) {
        both++;
        cnt_both.add(a[i][j]);
      }
      cnt_this.add(a[i][j]);
    }
  }
  printf("%d\n", ans);
  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;
const int MOD = 998244353;
int add(int x, int y) {
  x += y;
  if (x >= MOD) x -= MOD;
  return x;
}
int sub(int x, int y) {
  x -= y;
  if (x < 0) x += MOD;
  return x;
}
int mul(int x, int y) { return 1ll * x * y % MOD; }
const int MAXN = 2000 + 100;
int dp[MAXN][MAXN];
int a[MAXN][MAXN];
struct BIT {
  int sum[MAXN];
  int n;
  void init(int n_) {
    n = n_;
    fill(sum, sum + n + 1, 0);
  }
  int ask(int x) {
    int res = 0;
    while (x) {
      res += sum[x];
      x -= x & (-x);
    }
    return res;
  }
  void add(int id) {
    const int k = 1;
    while (id <= n) {
      sum[id] += k;
      id += id & (-id);
    }
  }
} cnt_this, cnt_both, cnt_lst;
int f(int n, int k) {
  if (k < 0) return 0;
  return dp[n][k];
}
int main() {
  dp[0][0] = 1;
  for (int i = 1; i < MAXN; i++) {
    dp[i][0] = mul(dp[i - 1][0], i);
    for (int j = 1; j <= i; j++) {
      dp[i][j] = sub(dp[i][j - 1], dp[i - 1][j - 1]);
    }
  }
  int n;
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      scanf("%d", &a[i][j]);
    }
  }
  int ans = 0;
  for (int i = 0; i < n; i++) {
    int remain_row = 1;
    for (int c = i + 1; c < n; c++) remain_row = mul(remain_row, dp[n][n]);
    vector<bool> this_row(n + 1), lst_row(n + 1);
    cnt_this.init(n);
    cnt_both.init(n);
    cnt_lst.init(n);
    int both = 0;
    (void)0;
    for (int j = 0; j < n; j++) {
      if (i > 0) {
        lst_row[a[i - 1][j]] = true;
        cnt_lst.add(a[i - 1][j]);
        if (this_row[a[i - 1][j]]) {
          both++;
          cnt_both.add(a[i - 1][j]);
        }
      } else {
        both = 0;
      }
      int re_slot = n - j - 1;
      (void)0;
      int kase1 = f(re_slot, re_slot - (j + 1 - both));
      int kase2 = f(re_slot, re_slot - (j + 1 - (both + 1)));
      if (i == 0) {
        kase1 = f(re_slot, 0);
      }
      int sml_num = a[i][j] - 1 - cnt_this.ask(a[i][j]);
      if (i > 0 && a[i - 1][j] < a[i][j] && !this_row[a[i - 1][j]]) sml_num--;
      int num2 = cnt_lst.ask(a[i][j] - 1) - cnt_both.ask(a[i][j] - 1);
      if (i > 0 && a[i - 1][j] < a[i][j] && !this_row[a[i - 1][j]]) num2--;
      int num1 = sml_num - num2;
      (void)0;
      (void)0;
      (void)0;
      assert(num1 >= 0);
      assert(num2 >= 0);
      int total = mul(remain_row, add(mul(kase1, num1), mul(kase2, num2)));
      ans = add(ans, total);
      this_row[a[i][j]] = true;
      if (lst_row[a[i][j]]) {
        both++;
        cnt_both.add(a[i][j]);
      }
      cnt_this.add(a[i][j]);
    }
  }
  printf("%d\n", ans);
  return 0;
}

```