#include <bits/stdc++.h>
using namespace std;
const long double PI = 3.1415926535897932384626433832795L;
template <class T>
bool uin(T &, const T &);
template <class T>
bool uax(T &, const T &);
template <class T>
T gcd(T, T);
template <class T>
T lcm(T, T);
template <class _T>
inline string tostr(const _T &);
template <typename T>
void input(T &);
template <typename T = long long>
T nxt();
bool checkp(long long);
template <typename T>
T pw(T a, T n, T m);
template <typename T>
T inv(T a, T p);
template <class _T>
_T sqr(const _T &first);
int TTT;
const long long mod = 998244353;
void pre() {}
void solve(int) {
  int n = nxt();
  vector<vector<int>> a(n, vector<int>(n));
  for (int i = 0; i < (int)(n); ++i) {
    for (int j = 0; j < (int)(n); ++j) a[i][j] = nxt() - 1;
  }
  vector<vector<long long>> dp(n + 1, vector<long long>(n + 1));
  dp[0][0] = 1;
  for (int i = 1; i <= n; ++i) {
    dp[i][0] = dp[i - 1][0] * i % mod;
    dp[i][1] = (i - 1) * dp[i - 1][0] % mod;
    for (int j = 2; j <= i; ++j) {
      dp[i][j] =
          ((j - 1) * dp[i - 1][j - 2] + (i - j) * dp[i - 1][j - 1]) % mod;
    }
  }
  vector<long long> s(n);
  s[0] = 1;
  for (int i = 0; i < (int)(n - 1); ++i) {
    s[i + 1] = s[i] * dp[n][n] % mod;
  }
  vector<int> prev(n, n);
  long long answer = 0;
  vector<bitset<2001>> pref(n);
  for (int i = 0; i < (int)(n - 1); ++i) {
    pref[i + 1] = pref[i];
    pref[i + 1][i] = true;
  }
  for (int i = 0; i < (int)(n); ++i) {
    bitset<2001> was;
    bitset<2001> used;
    if (i)
      for (int first : prev) was[first] = true;
    long long Z = 0;
    for (int j = 0; j < (int)(n); ++j) {
      was[prev[j]] = false;
      size_t rem = (~used & was).count();
      auto CUR = (~used & pref[a[i][j]]);
      CUR[prev[j]] = false;
      size_t A = CUR.count();
      size_t B = (CUR & was).count();
      A -= B;
      assert(rem > 0 || B == 0);
      Z +=
          (rem > 0 ? (dp[n - j - 1][rem - 1] * B) : 0) + dp[n - j - 1][rem] * A;
      Z %= mod;
      assert(prev[j] != a[i][j]);
      used[a[i][j]] = true;
    }
    answer += Z * s[n - 1 - i];
    answer %= mod;
    prev = a[i];
  }
  cout << answer << "\n";
}
int main(int argc, char **argv) {
  ::TTT = 1;
  pre();
  for (int test = 1; test <= ::TTT; ++test) {
    solve(test);
  }
  return 0;
}
template <typename T>
T gcd(T first, T second) {
  while (second > 0) {
    first %= second;
    swap(first, second);
  }
  return first;
}
template <class T>
T lcm(T a, T b) {
  return a / gcd(a, b) * b;
}
template <class _T>
inline _T sqr(const _T &first) {
  return first * first;
}
template <class _T>
inline string tostr(const _T &a) {
  ostringstream os("");
  os << a;
  return os.str();
}
template <typename T>
inline void input(T &a) {
  static int ed;
  a = 0;
  while (!isdigit(ed = getchar()) && ed != '-') {
  }
  char neg = 0;
  if (ed == '-') {
    neg = 1;
    ed = getchar();
  }
  while (isdigit(ed)) {
    a = 10 * a + ed - '0';
    ed = getchar();
  }
  if (neg) a = -a;
}
template <typename T>
inline T nxt() {
  T res;
  input(res);
  return res;
}
void myassert(bool v) { assert(v); }
mt19937 generator;
bool checkp(long long v) {
  if (v < 2) return false;
  for (long long i = 2; i * i <= v; ++i) {
    if (v % i == 0) {
      return false;
    }
  }
  return true;
}
template <typename T>
T pw(T a, T n, T m) {
  T res = 1;
  while (n) {
    if (n & 1) {
      res = res * a % m;
    }
    a = a * a % m;
    n >>= 1;
  }
  return res;
}
template <typename T>
T inv(T a, T p) {
  T res = 1;
  while (a > 1) {
    res = res * (p - p / a) % p;
    a = p % a;
  }
  return res;
}
template <class T>
bool uin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return true;
  }
  return false;
}
template <class T>
bool uax(T &a, const T &b) {
  if (b > a) {
    a = b;
    return true;
  }
  return false;
}

### 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;
const long double PI = 3.1415926535897932384626433832795L;
template <class T>
bool uin(T &, const T &);
template <class T>
bool uax(T &, const T &);
template <class T>
T gcd(T, T);
template <class T>
T lcm(T, T);
template <class _T>
inline string tostr(const _T &);
template <typename T>
void input(T &);
template <typename T = long long>
T nxt();
bool checkp(long long);
template <typename T>
T pw(T a, T n, T m);
template <typename T>
T inv(T a, T p);
template <class _T>
_T sqr(const _T &first);
int TTT;
const long long mod = 998244353;
void pre() {}
void solve(int) {
  int n = nxt();
  vector<vector<int>> a(n, vector<int>(n));
  for (int i = 0; i < (int)(n); ++i) {
    for (int j = 0; j < (int)(n); ++j) a[i][j] = nxt() - 1;
  }
  vector<vector<long long>> dp(n + 1, vector<long long>(n + 1));
  dp[0][0] = 1;
  for (int i = 1; i <= n; ++i) {
    dp[i][0] = dp[i - 1][0] * i % mod;
    dp[i][1] = (i - 1) * dp[i - 1][0] % mod;
    for (int j = 2; j <= i; ++j) {
      dp[i][j] =
          ((j - 1) * dp[i - 1][j - 2] + (i - j) * dp[i - 1][j - 1]) % mod;
    }
  }
  vector<long long> s(n);
  s[0] = 1;
  for (int i = 0; i < (int)(n - 1); ++i) {
    s[i + 1] = s[i] * dp[n][n] % mod;
  }
  vector<int> prev(n, n);
  long long answer = 0;
  vector<bitset<2001>> pref(n);
  for (int i = 0; i < (int)(n - 1); ++i) {
    pref[i + 1] = pref[i];
    pref[i + 1][i] = true;
  }
  for (int i = 0; i < (int)(n); ++i) {
    bitset<2001> was;
    bitset<2001> used;
    if (i)
      for (int first : prev) was[first] = true;
    long long Z = 0;
    for (int j = 0; j < (int)(n); ++j) {
      was[prev[j]] = false;
      size_t rem = (~used & was).count();
      auto CUR = (~used & pref[a[i][j]]);
      CUR[prev[j]] = false;
      size_t A = CUR.count();
      size_t B = (CUR & was).count();
      A -= B;
      assert(rem > 0 || B == 0);
      Z +=
          (rem > 0 ? (dp[n - j - 1][rem - 1] * B) : 0) + dp[n - j - 1][rem] * A;
      Z %= mod;
      assert(prev[j] != a[i][j]);
      used[a[i][j]] = true;
    }
    answer += Z * s[n - 1 - i];
    answer %= mod;
    prev = a[i];
  }
  cout << answer << "\n";
}
int main(int argc, char **argv) {
  ::TTT = 1;
  pre();
  for (int test = 1; test <= ::TTT; ++test) {
    solve(test);
  }
  return 0;
}
template <typename T>
T gcd(T first, T second) {
  while (second > 0) {
    first %= second;
    swap(first, second);
  }
  return first;
}
template <class T>
T lcm(T a, T b) {
  return a / gcd(a, b) * b;
}
template <class _T>
inline _T sqr(const _T &first) {
  return first * first;
}
template <class _T>
inline string tostr(const _T &a) {
  ostringstream os("");
  os << a;
  return os.str();
}
template <typename T>
inline void input(T &a) {
  static int ed;
  a = 0;
  while (!isdigit(ed = getchar()) && ed != '-') {
  }
  char neg = 0;
  if (ed == '-') {
    neg = 1;
    ed = getchar();
  }
  while (isdigit(ed)) {
    a = 10 * a + ed - '0';
    ed = getchar();
  }
  if (neg) a = -a;
}
template <typename T>
inline T nxt() {
  T res;
  input(res);
  return res;
}
void myassert(bool v) { assert(v); }
mt19937 generator;
bool checkp(long long v) {
  if (v < 2) return false;
  for (long long i = 2; i * i <= v; ++i) {
    if (v % i == 0) {
      return false;
    }
  }
  return true;
}
template <typename T>
T pw(T a, T n, T m) {
  T res = 1;
  while (n) {
    if (n & 1) {
      res = res * a % m;
    }
    a = a * a % m;
    n >>= 1;
  }
  return res;
}
template <typename T>
T inv(T a, T p) {
  T res = 1;
  while (a > 1) {
    res = res * (p - p / a) % p;
    a = p % a;
  }
  return res;
}
template <class T>
bool uin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return true;
  }
  return false;
}
template <class T>
bool uax(T &a, const T &b) {
  if (b > a) {
    a = b;
    return true;
  }
  return false;
}

```