#include <bits/stdc++.h>
using namespace std;
#pragma GCC optimize("O2")
#pragma GCC target("sse2")
void opting() {
  ios::sync_with_stdio(false);
  cout.tie(0);
  cin.tie(0);
}
long long inp() {
  long long x;
  cin >> x;
  return x;
}
void comping(vector<long long> &vec) {
  sort((vec).begin(), (vec).end());
  vec.resize(unique((vec).begin(), (vec).end()) - vec.begin());
}
const long long maxn = 2e3 + 69, inf = 1e9 + 420, lg = 21, delta = 998244353;
long long fact[maxn];
long long dera[maxn];
void calc_fact() {
  fact[0] = 1;
  for (long long i = 1; i < maxn; i++) fact[i] = (i * fact[i - 1]) % delta;
}
void calc_dera() {
  long long tmp[maxn];
  fill(tmp, tmp + maxn, 0);
  tmp[0] = tmp[1] = 0;
  dera[0] = 1;
  dera[1] = 0;
  for (long long i = 2; i < maxn; i++) {
    tmp[i] = (dera[i - 2] + (i - 2) * tmp[i - 1]) % delta;
    dera[i] = ((i - 1) * tmp[i]) % delta;
  }
}
long long dp[maxn][maxn];
void calc_DP() {
  for (long long i = 0; i < maxn; i++) {
    dp[0][i] = fact[i];
    dp[i][0] = dera[i];
    dp[1][i] = (i * fact[i]) % delta;
  }
  for (long long a = 2; a < maxn; a++)
    for (long long b = 1; b < maxn - 2; b++)
      dp[a][b] = ((((a - 1) * dp[a - 2][b + 1]) % delta) +
                  ((b * dp[a - 1][b]) % delta)) %
                 delta;
}
void predo() {
  calc_dera();
  calc_fact();
  calc_DP();
}
long long A_fen[maxn], B_fen[maxn];
void add_A(long long idx, long long val) {
  if (!idx) return;
  for (; idx < maxn; idx += (idx & -idx)) A_fen[idx] += val;
}
void add_B(long long idx, long long val) {
  if (!idx) return;
  for (; idx < maxn; idx += (idx & -idx)) B_fen[idx] += val;
}
long long sum_A(long long idx) {
  long long sm = 0;
  for (; idx; idx -= (idx & -idx)) sm += A_fen[idx];
  return sm;
}
long long sum_B(long long idx) {
  long long sm = 0;
  for (; idx; idx -= (idx & -idx)) sm += B_fen[idx];
  return sm;
}
void reset_fen() {
  memset(A_fen, 0, sizeof A_fen);
  memset(B_fen, 0, sizeof B_fen);
}
long long ans;
long long Xe = 1;
long long n;
long long mat[maxn][maxn];
void calc_line(long long id) {
  set<long long> above, below;
  long long same = 0;
  reset_fen();
  long long Ye = 0;
  for (long long i = n - 1; i > -1; i--) {
    long long v = mat[id - 1][i];
    long long u = mat[id][i];
    if (above.find(u) != above.end()) {
      add_A(u, 1);
      same++;
    } else
      add_B(u, 1);
    long long st_a = sum_A(u - 1);
    long long st_b = sum_B(u - 1) - (below.find(v) != below.end() and v < u);
    long long a = same;
    long long b = (n - i) - same;
    Ye = (Ye + st_a * dp[a - 1][b] + st_b * dp[a][b - 1]) % delta;
    if (below.find(v) != below.end()) {
      add_B(v, -1);
      add_A(v, 1);
      same++;
    }
    above.insert(v);
    below.insert(u);
  }
  ans = (ans + Xe * Ye) % delta;
  Xe = (Xe * dera[n]) % delta;
}
void input() {
  cin >> n;
  for (long long i = 1; i <= n; i++)
    for (long long j = 0; j < n; j++) cin >> mat[i][j];
}
void solve() {
  for (long long i = n; i; i--) calc_line(i);
  cout << ans << endl;
}
int main() {
  opting();
  predo();
  while (0) {
    long long a, b;
    cin >> a >> b;
    cout << dp[a][b] << endl;
    cout << fact[a] << endl;
    cout << dera[a] << endl;
  }
  input();
  solve();
}

### Prompt

Develop a solution in Cpp to the problem described below:

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;
#pragma GCC optimize("O2")
#pragma GCC target("sse2")
void opting() {
  ios::sync_with_stdio(false);
  cout.tie(0);
  cin.tie(0);
}
long long inp() {
  long long x;
  cin >> x;
  return x;
}
void comping(vector<long long> &vec) {
  sort((vec).begin(), (vec).end());
  vec.resize(unique((vec).begin(), (vec).end()) - vec.begin());
}
const long long maxn = 2e3 + 69, inf = 1e9 + 420, lg = 21, delta = 998244353;
long long fact[maxn];
long long dera[maxn];
void calc_fact() {
  fact[0] = 1;
  for (long long i = 1; i < maxn; i++) fact[i] = (i * fact[i - 1]) % delta;
}
void calc_dera() {
  long long tmp[maxn];
  fill(tmp, tmp + maxn, 0);
  tmp[0] = tmp[1] = 0;
  dera[0] = 1;
  dera[1] = 0;
  for (long long i = 2; i < maxn; i++) {
    tmp[i] = (dera[i - 2] + (i - 2) * tmp[i - 1]) % delta;
    dera[i] = ((i - 1) * tmp[i]) % delta;
  }
}
long long dp[maxn][maxn];
void calc_DP() {
  for (long long i = 0; i < maxn; i++) {
    dp[0][i] = fact[i];
    dp[i][0] = dera[i];
    dp[1][i] = (i * fact[i]) % delta;
  }
  for (long long a = 2; a < maxn; a++)
    for (long long b = 1; b < maxn - 2; b++)
      dp[a][b] = ((((a - 1) * dp[a - 2][b + 1]) % delta) +
                  ((b * dp[a - 1][b]) % delta)) %
                 delta;
}
void predo() {
  calc_dera();
  calc_fact();
  calc_DP();
}
long long A_fen[maxn], B_fen[maxn];
void add_A(long long idx, long long val) {
  if (!idx) return;
  for (; idx < maxn; idx += (idx & -idx)) A_fen[idx] += val;
}
void add_B(long long idx, long long val) {
  if (!idx) return;
  for (; idx < maxn; idx += (idx & -idx)) B_fen[idx] += val;
}
long long sum_A(long long idx) {
  long long sm = 0;
  for (; idx; idx -= (idx & -idx)) sm += A_fen[idx];
  return sm;
}
long long sum_B(long long idx) {
  long long sm = 0;
  for (; idx; idx -= (idx & -idx)) sm += B_fen[idx];
  return sm;
}
void reset_fen() {
  memset(A_fen, 0, sizeof A_fen);
  memset(B_fen, 0, sizeof B_fen);
}
long long ans;
long long Xe = 1;
long long n;
long long mat[maxn][maxn];
void calc_line(long long id) {
  set<long long> above, below;
  long long same = 0;
  reset_fen();
  long long Ye = 0;
  for (long long i = n - 1; i > -1; i--) {
    long long v = mat[id - 1][i];
    long long u = mat[id][i];
    if (above.find(u) != above.end()) {
      add_A(u, 1);
      same++;
    } else
      add_B(u, 1);
    long long st_a = sum_A(u - 1);
    long long st_b = sum_B(u - 1) - (below.find(v) != below.end() and v < u);
    long long a = same;
    long long b = (n - i) - same;
    Ye = (Ye + st_a * dp[a - 1][b] + st_b * dp[a][b - 1]) % delta;
    if (below.find(v) != below.end()) {
      add_B(v, -1);
      add_A(v, 1);
      same++;
    }
    above.insert(v);
    below.insert(u);
  }
  ans = (ans + Xe * Ye) % delta;
  Xe = (Xe * dera[n]) % delta;
}
void input() {
  cin >> n;
  for (long long i = 1; i <= n; i++)
    for (long long j = 0; j < n; j++) cin >> mat[i][j];
}
void solve() {
  for (long long i = n; i; i--) calc_line(i);
  cout << ans << endl;
}
int main() {
  opting();
  predo();
  while (0) {
    long long a, b;
    cin >> a >> b;
    cout << dp[a][b] << endl;
    cout << fact[a] << endl;
    cout << dera[a] << endl;
  }
  input();
  solve();
}

```