#include <bits/stdc++.h>
template <int MOD>
struct Integral {
  int v_ = 0;
  template <typename T>
  Integral(T v) : v_(norm(v)) {
    static_assert(std::is_integral<T>::value, "input should be an integral.");
  }
  Integral() = default;
  ~Integral() = default;
  template <typename T>
  T norm(T v) const {
    if (std::is_same<long long, T>::value) {
      v %= MOD;
      if (v < 0) v += MOD;
    } else {
      if (v >= MOD) v -= MOD;
      if (v < 0) v += MOD;
      if (v >= MOD || v < 0) {
        v %= MOD;
        if (v < 0) v += MOD;
      }
    }
    return v;
  }
  int val() const { return v_; }
  Integral& operator+=(const Integral& rhs) {
    v_ += rhs.val();
    if (v_ >= MOD) v_ -= MOD;
    return *this;
  }
  Integral& operator-=(const Integral& rhs) {
    v_ += MOD - rhs.val();
    if (v_ >= MOD) v_ -= MOD;
    return *this;
  }
  Integral& operator*=(const Integral& rhs) {
    v_ = v_ * 1LL * rhs.val() % MOD;
    return *this;
  }
  Integral& operator/=(const Integral& rhs) {
    v_ = v_ * 1LL * power(rhs.val(), MOD - 2) % MOD;
    return *this;
  }
  Integral operator+(const Integral& rhs) const {
    Integral ret = *this;
    return ret += rhs;
  }
  Integral operator-(const Integral& rhs) const {
    Integral ret = *this;
    return ret -= rhs;
  }
  Integral operator*(const Integral& rhs) const {
    Integral ret = *this;
    return ret *= rhs;
  }
  Integral operator/(const Integral& rhs) const {
    Integral ret = *this;
    return ret /= rhs;
  }
  bool operator==(const Integral& rhs) const { return val() == rhs.val(); }
  bool operator!=(const Integral& rhs) const { return !(*this == rhs); }
  const Integral operator-() const { return Integral(-val()); }
  const Integral& operator++() {
    v_ += 1;
    if (v_ >= MOD) v_ -= MOD;
    return *this;
  }
  const Integral operator++(int) {
    Integral ret = *this;
    ++(*this);
    return ret;
  }
  const Integral& operator--() {
    v_ += MOD - 1;
    if (v_ >= MOD) v_ -= MOD;
    return *this;
  }
  const Integral operator--(int) {
    Integral ret = *this;
    --(*this);
    return ret;
  }
  Integral power(long long b) const {
    long long ret = 1 % MOD, a = v_;
    for (; b; b >>= 1, a = a * a % MOD)
      if (b & 1) ret = ret * a % MOD;
    return ret;
  }
  Integral inv() const { return power(MOD - 2); }
};
template <int MOD>
std::string to_string(const Integral<MOD>& v) {
  return std::string("Int<>{") + std::to_string(v.val()) + "}";
}
template <int MOD, bool kAllowBruteForce = false>
struct Binomial {
  std::vector<Integral<MOD>> factor, inv_factor;
  explicit Binomial(int n = 0) : factor(n + 1), inv_factor(n + 1) {
    factor[0] = 1;
    for (int i = 1; i <= n; ++i) factor[i] = factor[i - 1] * i;
    inv_factor[n] = factor[n].inv();
    for (int i = n; i >= 1; --i) inv_factor[i - 1] = inv_factor[i] * i;
  }
  ~Binomial() = default;
  template <typename T>
  Integral<MOD> operator()(T a, T b) const {
    if (a < b || b < 0) return 0;
    if (a < factor.size()) return factor[a] * inv_factor[b] * inv_factor[a - b];
    if (!kAllowBruteForce) {
      throw std::out_of_range("Binomial");
    } else {
      b = std::min(b, a - b);
      Integral<MOD> ret = 1;
      for (T i = 1; i <= b; ++i) ret = ret * (a + 1 - i) / i;
      return ret;
    }
  }
};
template <int MOD>
struct PowerTable : public std::vector<Integral<MOD>> {
  PowerTable(int n, const Integral<MOD>& g) {
    static_assert(sizeof(PowerTable) == sizeof(std::vector<Integral<MOD>>), "");
    this->resize(n + 1);
    this->at(0) = 1;
    this->at(1) = g;
    for (int i = 2; i < this->size(); ++i)
      this->at(i) = this->at(i - 1) * this->at(1);
  }
};
const int MOD = 998244353;
using Mint = Integral<MOD>;
using Binom = Binomial<MOD>;
Binom binom(200000);
struct FenwickTree {
  std::vector<int> a;
  explicit FenwickTree(int n) : a(n) {}
  void update(int p, int dt) {
    for (int i = p; i < (int)a.size(); i += ~i & i + 1) a[i] += dt;
  }
  int query(int p) const {
    int ret = 0;
    for (int i = p; i >= 0; i -= ~i & i + 1) ret += a[i];
    return ret;
  }
};
int main() {
  std::ios::sync_with_stdio(false);
  std::cin.tie(nullptr);
  std::istream& reader = std::cin;
  int n;
  reader >> n;
  std::vector<std::vector<int>> a(n, std::vector<int>(n));
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < n; ++j) {
      reader >> a[i][j];
      --a[i][j];
    }
  }
  std::vector<std::vector<Mint>> f(n + 1, std::vector<Mint>(n + 1));
  f[0][0] = 1;
  for (int i = 1; i <= n; ++i) {
    f[i][0] = f[i - 1][0] * i;
    for (int j = 1; j <= i; ++j) {
      if (j >= 2) f[i][j] += f[i - 1][j - 2] * (j - 1);
      if (j >= 1) f[i][j] += f[i - 1][j - 1] * (i - j);
    }
  }
  Mint result = 0;
  Mint coef = 1;
  for (int at = n - 1; at >= 0; --at, coef *= f[n][n]) {
    Mint ways = 0;
    if (at) {
      std::vector<int> up(n, 1), down(n, 1);
      FenwickTree bit_down(n);
      for (int i = 0; i < n; ++i) bit_down.update(i, 1);
      FenwickTree bit_lim(n);
      for (int i = 0; i < n; ++i) bit_lim.update(i, 1);
      int limited = n;
      auto erase_down = [&](int p) {
        if (!down[p]) return;
        down[p] = 0;
        bit_down.update(p, -1);
        if (up[p]) {
          --limited;
          bit_lim.update(p, -1);
        }
      };
      auto erase_up = [&](int p) {
        if (!up[p]) return;
        up[p] = 0;
        if (down[p]) {
          --limited;
          bit_lim.update(p, -1);
        }
      };
      for (int i = 0; i < n; ++i) {
        int x = bit_down.query(a[at][i] - 1);
        int y = bit_lim.query(a[at][i] - 1);
        int z = down[a[at - 1][i]];
        if (limited - 1 - z >= 0) {
          int r =
              a[at - 1][i] < a[at][i] && down[a[at - 1][i]] && up[a[at - 1][i]];
          ways += f[n - 1 - i][limited - 1 - z] * (y - r);
        }
        if (limited - z >= 0) {
          int r = a[at - 1][i] < a[at][i] && down[a[at - 1][i]] &&
                  !up[a[at - 1][i]];
          ways += f[n - 1 - i][limited - z] * (x - y - r);
        }
        erase_down(a[at][i]);
        erase_up(a[at - 1][i]);
      }
    } else {
      FenwickTree cur(n);
      for (int i = 0; i < n; ++i) cur.update(i, 1);
      for (int i = 0; i < n; ++i) {
        ways += binom.factor[n - 1 - i] * cur.query(a[at][i] - 1);
        cur.update(a[at][i], -1);
      }
    }
    result += ways * coef;
  }
  std::cout << result.val() << std::endl;
}

### 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>
template <int MOD>
struct Integral {
  int v_ = 0;
  template <typename T>
  Integral(T v) : v_(norm(v)) {
    static_assert(std::is_integral<T>::value, "input should be an integral.");
  }
  Integral() = default;
  ~Integral() = default;
  template <typename T>
  T norm(T v) const {
    if (std::is_same<long long, T>::value) {
      v %= MOD;
      if (v < 0) v += MOD;
    } else {
      if (v >= MOD) v -= MOD;
      if (v < 0) v += MOD;
      if (v >= MOD || v < 0) {
        v %= MOD;
        if (v < 0) v += MOD;
      }
    }
    return v;
  }
  int val() const { return v_; }
  Integral& operator+=(const Integral& rhs) {
    v_ += rhs.val();
    if (v_ >= MOD) v_ -= MOD;
    return *this;
  }
  Integral& operator-=(const Integral& rhs) {
    v_ += MOD - rhs.val();
    if (v_ >= MOD) v_ -= MOD;
    return *this;
  }
  Integral& operator*=(const Integral& rhs) {
    v_ = v_ * 1LL * rhs.val() % MOD;
    return *this;
  }
  Integral& operator/=(const Integral& rhs) {
    v_ = v_ * 1LL * power(rhs.val(), MOD - 2) % MOD;
    return *this;
  }
  Integral operator+(const Integral& rhs) const {
    Integral ret = *this;
    return ret += rhs;
  }
  Integral operator-(const Integral& rhs) const {
    Integral ret = *this;
    return ret -= rhs;
  }
  Integral operator*(const Integral& rhs) const {
    Integral ret = *this;
    return ret *= rhs;
  }
  Integral operator/(const Integral& rhs) const {
    Integral ret = *this;
    return ret /= rhs;
  }
  bool operator==(const Integral& rhs) const { return val() == rhs.val(); }
  bool operator!=(const Integral& rhs) const { return !(*this == rhs); }
  const Integral operator-() const { return Integral(-val()); }
  const Integral& operator++() {
    v_ += 1;
    if (v_ >= MOD) v_ -= MOD;
    return *this;
  }
  const Integral operator++(int) {
    Integral ret = *this;
    ++(*this);
    return ret;
  }
  const Integral& operator--() {
    v_ += MOD - 1;
    if (v_ >= MOD) v_ -= MOD;
    return *this;
  }
  const Integral operator--(int) {
    Integral ret = *this;
    --(*this);
    return ret;
  }
  Integral power(long long b) const {
    long long ret = 1 % MOD, a = v_;
    for (; b; b >>= 1, a = a * a % MOD)
      if (b & 1) ret = ret * a % MOD;
    return ret;
  }
  Integral inv() const { return power(MOD - 2); }
};
template <int MOD>
std::string to_string(const Integral<MOD>& v) {
  return std::string("Int<>{") + std::to_string(v.val()) + "}";
}
template <int MOD, bool kAllowBruteForce = false>
struct Binomial {
  std::vector<Integral<MOD>> factor, inv_factor;
  explicit Binomial(int n = 0) : factor(n + 1), inv_factor(n + 1) {
    factor[0] = 1;
    for (int i = 1; i <= n; ++i) factor[i] = factor[i - 1] * i;
    inv_factor[n] = factor[n].inv();
    for (int i = n; i >= 1; --i) inv_factor[i - 1] = inv_factor[i] * i;
  }
  ~Binomial() = default;
  template <typename T>
  Integral<MOD> operator()(T a, T b) const {
    if (a < b || b < 0) return 0;
    if (a < factor.size()) return factor[a] * inv_factor[b] * inv_factor[a - b];
    if (!kAllowBruteForce) {
      throw std::out_of_range("Binomial");
    } else {
      b = std::min(b, a - b);
      Integral<MOD> ret = 1;
      for (T i = 1; i <= b; ++i) ret = ret * (a + 1 - i) / i;
      return ret;
    }
  }
};
template <int MOD>
struct PowerTable : public std::vector<Integral<MOD>> {
  PowerTable(int n, const Integral<MOD>& g) {
    static_assert(sizeof(PowerTable) == sizeof(std::vector<Integral<MOD>>), "");
    this->resize(n + 1);
    this->at(0) = 1;
    this->at(1) = g;
    for (int i = 2; i < this->size(); ++i)
      this->at(i) = this->at(i - 1) * this->at(1);
  }
};
const int MOD = 998244353;
using Mint = Integral<MOD>;
using Binom = Binomial<MOD>;
Binom binom(200000);
struct FenwickTree {
  std::vector<int> a;
  explicit FenwickTree(int n) : a(n) {}
  void update(int p, int dt) {
    for (int i = p; i < (int)a.size(); i += ~i & i + 1) a[i] += dt;
  }
  int query(int p) const {
    int ret = 0;
    for (int i = p; i >= 0; i -= ~i & i + 1) ret += a[i];
    return ret;
  }
};
int main() {
  std::ios::sync_with_stdio(false);
  std::cin.tie(nullptr);
  std::istream& reader = std::cin;
  int n;
  reader >> n;
  std::vector<std::vector<int>> a(n, std::vector<int>(n));
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < n; ++j) {
      reader >> a[i][j];
      --a[i][j];
    }
  }
  std::vector<std::vector<Mint>> f(n + 1, std::vector<Mint>(n + 1));
  f[0][0] = 1;
  for (int i = 1; i <= n; ++i) {
    f[i][0] = f[i - 1][0] * i;
    for (int j = 1; j <= i; ++j) {
      if (j >= 2) f[i][j] += f[i - 1][j - 2] * (j - 1);
      if (j >= 1) f[i][j] += f[i - 1][j - 1] * (i - j);
    }
  }
  Mint result = 0;
  Mint coef = 1;
  for (int at = n - 1; at >= 0; --at, coef *= f[n][n]) {
    Mint ways = 0;
    if (at) {
      std::vector<int> up(n, 1), down(n, 1);
      FenwickTree bit_down(n);
      for (int i = 0; i < n; ++i) bit_down.update(i, 1);
      FenwickTree bit_lim(n);
      for (int i = 0; i < n; ++i) bit_lim.update(i, 1);
      int limited = n;
      auto erase_down = [&](int p) {
        if (!down[p]) return;
        down[p] = 0;
        bit_down.update(p, -1);
        if (up[p]) {
          --limited;
          bit_lim.update(p, -1);
        }
      };
      auto erase_up = [&](int p) {
        if (!up[p]) return;
        up[p] = 0;
        if (down[p]) {
          --limited;
          bit_lim.update(p, -1);
        }
      };
      for (int i = 0; i < n; ++i) {
        int x = bit_down.query(a[at][i] - 1);
        int y = bit_lim.query(a[at][i] - 1);
        int z = down[a[at - 1][i]];
        if (limited - 1 - z >= 0) {
          int r =
              a[at - 1][i] < a[at][i] && down[a[at - 1][i]] && up[a[at - 1][i]];
          ways += f[n - 1 - i][limited - 1 - z] * (y - r);
        }
        if (limited - z >= 0) {
          int r = a[at - 1][i] < a[at][i] && down[a[at - 1][i]] &&
                  !up[a[at - 1][i]];
          ways += f[n - 1 - i][limited - z] * (x - y - r);
        }
        erase_down(a[at][i]);
        erase_up(a[at - 1][i]);
      }
    } else {
      FenwickTree cur(n);
      for (int i = 0; i < n; ++i) cur.update(i, 1);
      for (int i = 0; i < n; ++i) {
        ways += binom.factor[n - 1 - i] * cur.query(a[at][i] - 1);
        cur.update(a[at][i], -1);
      }
    }
    result += ways * coef;
  }
  std::cout << result.val() << std::endl;
}

```