#include <bits/stdc++.h>
using namespace std;
using namespace std::chrono;
const long long int mod = 998244353;
const long long int big = 2.19e15 + 1;
const long double pai = 3.141592653589793238462643383279502884197;
const long double eps = 1e-15;
template <class T, class U>
bool mineq(T& a, U b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T, class U>
bool maxeq(T& a, U b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
long long int gcd(long long int a, long long int b) {
  if (a % b == 0) {
    return b;
  } else
    return gcd(b, a % b);
}
long long int lcm(long long int a, long long int b) {
  if (a == 0) {
    return b;
  }
  return a / gcd(a, b) * b;
}
template <class T>
void SO(T& ve) {
  sort(ve.begin(), ve.end());
}
template <class T>
void REV(T& ve) {
  reverse(ve.begin(), ve.end());
}
template <class T>
long long int LBI(vector<T>& ar, T in) {
  return lower_bound(ar.begin(), ar.end(), in) - ar.begin();
}
template <class T>
long long int UBI(vector<T>& ar, T in) {
  return upper_bound(ar.begin(), ar.end(), in) - ar.begin();
}
int main(void) {
  long long int n, i, j, k, ans = 0, gyo = 1;
  cin.tie(0);
  ios::sync_with_stdio(false);
  cin >> n;
  static long long int dp[2005][2005] = {0};
  dp[0][0] = 1;
  static long long int kai[2005] = {0};
  kai[0] = 1;
  for (i = 1; i <= n; i++) {
    kai[i] = kai[i - 1] * i % mod;
  }
  long long int wa = 1;
  for (i = 1; i <= n; i++) {
    wa = (kai[i] - wa) % mod;
    if (wa < 0) {
      wa += mod;
    }
    dp[i][0] = wa;
    for (j = 1; j <= i; j++) {
      dp[i][j] = (dp[i][j - 1] + dp[i - 1][j - 1]) % mod;
      wa += dp[i][j];
      wa %= mod;
    }
  }
  vector<int> mae(n);
  for (i = 0; i < n; i++) {
    vector<int> ima(n);
    ans *= dp[n][0];
    ans %= mod;
    vector<int> seg(2048);
    vector<int> use(2048);
    long long int gen = 0;
    vector<int> aaa(n + 1);
    int dti = 0;
    for (j = 0; j < n; j++) {
      int x, bas;
      cin >> x;
      ima[j] = x;
      aaa[x]++;
      if (aaa[x] == 1) {
        dti++;
        bas = 2048 + x;
        while (bas > 1) {
          if (bas % 2 == 0) {
            use[bas / 2]++;
          }
          bas /= 2;
        }
      } else {
        dti--;
      }
      aaa[mae[j]]++;
      if (aaa[mae[j]] == 1) {
        bas = 2048 + mae[j];
        while (bas > 1) {
          if (bas % 2 == 0) {
            use[bas / 2]++;
          }
          bas /= 2;
        }
        dti++;
      } else {
        dti--;
      }
      bas = 2048 + x;
      int uuu = x - 1;
      while (bas > 1) {
        if (bas % 2 == 1) {
          uuu -= use[bas / 2];
        }
        bas /= 2;
      }
      int bbb = x - 1;
      bas = 2048 + x;
      while (bas > 1) {
        if (bas % 2 == 1) {
          bbb -= seg[bas / 2];
        } else {
          seg[bas / 2]++;
        }
        bas /= 2;
      }
      if (i > 0) {
        int y = dti / 2;
        if (aaa[x] == 1) {
          y--;
        }
        int frag = 0;
        if (x > mae[j] && aaa[mae[j]] == 1) {
          frag++;
        }
        gen += (bbb - uuu - frag) * dp[n - j - 1][y];
        gen += uuu * dp[n - j - 1][y + 1];
      } else {
        gen += bbb * kai[n - 1 - j];
      }
      gen %= mod;
    }
    ans += gen;
    ans %= mod;
    mae = ima;
  }
  cout << ans << endl;
  return 0;
}

### Prompt

Please create a solution in Cpp 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;
using namespace std::chrono;
const long long int mod = 998244353;
const long long int big = 2.19e15 + 1;
const long double pai = 3.141592653589793238462643383279502884197;
const long double eps = 1e-15;
template <class T, class U>
bool mineq(T& a, U b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T, class U>
bool maxeq(T& a, U b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
long long int gcd(long long int a, long long int b) {
  if (a % b == 0) {
    return b;
  } else
    return gcd(b, a % b);
}
long long int lcm(long long int a, long long int b) {
  if (a == 0) {
    return b;
  }
  return a / gcd(a, b) * b;
}
template <class T>
void SO(T& ve) {
  sort(ve.begin(), ve.end());
}
template <class T>
void REV(T& ve) {
  reverse(ve.begin(), ve.end());
}
template <class T>
long long int LBI(vector<T>& ar, T in) {
  return lower_bound(ar.begin(), ar.end(), in) - ar.begin();
}
template <class T>
long long int UBI(vector<T>& ar, T in) {
  return upper_bound(ar.begin(), ar.end(), in) - ar.begin();
}
int main(void) {
  long long int n, i, j, k, ans = 0, gyo = 1;
  cin.tie(0);
  ios::sync_with_stdio(false);
  cin >> n;
  static long long int dp[2005][2005] = {0};
  dp[0][0] = 1;
  static long long int kai[2005] = {0};
  kai[0] = 1;
  for (i = 1; i <= n; i++) {
    kai[i] = kai[i - 1] * i % mod;
  }
  long long int wa = 1;
  for (i = 1; i <= n; i++) {
    wa = (kai[i] - wa) % mod;
    if (wa < 0) {
      wa += mod;
    }
    dp[i][0] = wa;
    for (j = 1; j <= i; j++) {
      dp[i][j] = (dp[i][j - 1] + dp[i - 1][j - 1]) % mod;
      wa += dp[i][j];
      wa %= mod;
    }
  }
  vector<int> mae(n);
  for (i = 0; i < n; i++) {
    vector<int> ima(n);
    ans *= dp[n][0];
    ans %= mod;
    vector<int> seg(2048);
    vector<int> use(2048);
    long long int gen = 0;
    vector<int> aaa(n + 1);
    int dti = 0;
    for (j = 0; j < n; j++) {
      int x, bas;
      cin >> x;
      ima[j] = x;
      aaa[x]++;
      if (aaa[x] == 1) {
        dti++;
        bas = 2048 + x;
        while (bas > 1) {
          if (bas % 2 == 0) {
            use[bas / 2]++;
          }
          bas /= 2;
        }
      } else {
        dti--;
      }
      aaa[mae[j]]++;
      if (aaa[mae[j]] == 1) {
        bas = 2048 + mae[j];
        while (bas > 1) {
          if (bas % 2 == 0) {
            use[bas / 2]++;
          }
          bas /= 2;
        }
        dti++;
      } else {
        dti--;
      }
      bas = 2048 + x;
      int uuu = x - 1;
      while (bas > 1) {
        if (bas % 2 == 1) {
          uuu -= use[bas / 2];
        }
        bas /= 2;
      }
      int bbb = x - 1;
      bas = 2048 + x;
      while (bas > 1) {
        if (bas % 2 == 1) {
          bbb -= seg[bas / 2];
        } else {
          seg[bas / 2]++;
        }
        bas /= 2;
      }
      if (i > 0) {
        int y = dti / 2;
        if (aaa[x] == 1) {
          y--;
        }
        int frag = 0;
        if (x > mae[j] && aaa[mae[j]] == 1) {
          frag++;
        }
        gen += (bbb - uuu - frag) * dp[n - j - 1][y];
        gen += uuu * dp[n - j - 1][y + 1];
      } else {
        gen += bbb * kai[n - 1 - j];
      }
      gen %= mod;
    }
    ans += gen;
    ans %= mod;
    mae = ima;
  }
  cout << ans << endl;
  return 0;
}

```