You are given a sequence of n pairs of integers: (a_1, b_1), (a_2, b_2), ... , (a_n, b_n). This sequence is called bad if it is sorted in non-descending order by first elements or if it is sorted in non-descending order by second elements. Otherwise the sequence is good. There are examples of good and bad sequences:

  * s = [(1, 2), (3, 2), (3, 1)] is bad because the sequence of first elements is sorted: [1, 3, 3]; 
  * s = [(1, 2), (3, 2), (1, 2)] is bad because the sequence of second elements is sorted: [2, 2, 2]; 
  * s = [(1, 1), (2, 2), (3, 3)] is bad because both sequences (the sequence of first elements and the sequence of second elements) are sorted; 
  * s = [(1, 3), (3, 3), (2, 2)] is good because neither the sequence of first elements ([1, 3, 2]) nor the sequence of second elements ([3, 3, 2]) is sorted. 



Calculate the number of permutations of size n such that after applying this permutation to the sequence s it turns into a good sequence. 

A permutation p of size n is a sequence p_1, p_2, ... , p_n consisting of n distinct integers from 1 to n (1 ≤ p_i ≤ n). If you apply permutation p_1, p_2, ... , p_n to the sequence s_1, s_2, ... , s_n you get the sequence s_{p_1}, s_{p_2}, ... , s_{p_n}. For example, if s = [(1, 2), (1, 3), (2, 3)] and p = [2, 3, 1] then s turns into [(1, 3), (2, 3), (1, 2)].

Input

The first line contains one integer n (1 ≤ n ≤ 3 ⋅ 10^5).

The next n lines contains description of sequence s. The i-th line contains two integers a_i and b_i (1 ≤ a_i, b_i ≤ n) — the first and second elements of i-th pair in the sequence.

The sequence s may contain equal elements.

Output

Print the number of permutations of size n such that after applying this permutation to the sequence s it turns into a good sequence. Print the answer modulo 998244353 (a prime number).

Examples

Input


3
1 1
2 2
3 1


Output


3


Input


4
2 3
2 2
2 1
2 4


Output


0


Input


3
1 1
1 1
2 3


Output


4

Note

In first test case there are six permutations of size 3: 

  1. if p = [1, 2, 3], then s = [(1, 1), (2, 2), (3, 1)] — bad sequence (sorted by first elements); 
  2. if p = [1, 3, 2], then s = [(1, 1), (3, 1), (2, 2)] — bad sequence (sorted by second elements); 
  3. if p = [2, 1, 3], then s = [(2, 2), (1, 1), (3, 1)] — good sequence; 
  4. if p = [2, 3, 1], then s = [(2, 2), (3, 1), (1, 1)] — good sequence; 
  5. if p = [3, 1, 2], then s = [(3, 1), (1, 1), (2, 2)] — bad sequence (sorted by second elements); 
  6. if p = [3, 2, 1], then s = [(3, 1), (2, 2), (1, 1)] — good sequence. 
#include <bits/stdc++.h>
using namespace std;
inline void fileio(const char* in, const char* out) {
  freopen(in, "r", stdin);
  freopen(out, "w", stdout);
}
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> VI;
typedef vector<bool> VB;
typedef vector<ll> VL;
const char nl = '\n';
const int INF = 0x3f3f3f3f;
const ll LINF = 0x3f3f3f3f3f3f3f3fll;
const ld EPS = 1e-9, PI = acos(-1);
class ModInt {
  int val;
  static const int MOD = 998244353;
  static_assert(MOD > 0, "Modulus not set!");
  int modInverse(int a) {
    int b = MOD, x = 0, x_ = 1, q;
    while (a != 0) {
      q = b / a;
      swap(b, a);
      a %= b;
      swap(x, x_);
      x_ -= q * x;
    }
    assert(b == 1);
    return (x %= MOD) < 0 ? (x + MOD) : x;
  }

 public:
  ModInt() : val(0) {}
  ModInt(int v) : val(v) {
    if ((val %= MOD) < 0) val += MOD;
  }
  ModInt& operator+=(const ModInt& b) {
    if ((val += b.val) >= MOD) val -= MOD;
    return *this;
  }
  ModInt& operator-=(const ModInt& b) {
    if ((val -= b.val) < 0) val += MOD;
    return *this;
  }
  ModInt& operator*=(const ModInt& b) {
    val = ((ll)val * (ll)b.val) % MOD;
    return *this;
  }
  ModInt& operator/=(const ModInt& b) { return (*this) *= modInverse(b.val); }
  ModInt operator+(const ModInt& b) const {
    ModInt ans = *this;
    return ans += b;
  };
  ModInt operator-(const ModInt& b) const {
    ModInt ans = *this;
    return ans -= b;
  };
  ModInt operator*(const ModInt& b) const {
    ModInt ans = *this;
    return ans *= b;
  };
  ModInt operator/(const ModInt& b) const {
    ModInt ans = *this;
    return ans /= b;
  };
  ModInt operator+(int b) const {
    ModInt ans = *this;
    return ans += b;
  };
  ModInt operator-(int b) const {
    ModInt ans = *this;
    return ans -= b;
  };
  ModInt operator*(int b) const {
    ModInt ans = *this;
    return ans *= b;
  };
  ModInt operator/(int b) const {
    ModInt ans = *this;
    return ans /= b;
  };
  int toInt() const {
    assert(0 <= val && val < MOD);
    return val;
  }
};
const int N = 3e5 + 5;
ModInt fact[N];
void pre() {
  fact[0] = 1;
  for (int i = 1; i < N; ++i) {
    fact[i] = fact[i - 1] * i;
  }
}
int main() {
  atexit([]() { cerr << "Time: " << (ld)clock() / CLOCKS_PER_SEC << nl; });
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  pre();
  int n;
  cin >> n;
  vector<pii> nums(n);
  for (int i = 0; i < (n); ++i) {
    cin >> nums[i].first >> nums[i].second;
  }
  sort(nums.begin(), nums.end());
  ModInt cnt1 = 1;
  ModInt cnt2 = 1;
  ModInt cnt3 = 1;
  pii prev;
  int cntPii = 0;
  int cntFF = 0;
  for (int i = 0; i < (n); ++i) {
    if (i < n - 1 && nums[i].second > nums[i + 1].second) {
      cnt3 = 0;
    }
    if (i == 0) {
      cntPii = cntFF = 0;
    } else {
      if (prev != nums[i]) {
        cnt3 *= fact[cntPii];
        cntPii = 0;
      }
      if (prev.first != nums[i].first) {
        cnt1 *= fact[cntFF];
        cntFF = 0;
      }
    }
    cntPii++;
    cntFF++;
    prev = nums[i];
  }
  cnt1 *= fact[cntFF];
  cnt3 *= fact[cntPii];
  sort(nums.begin(), nums.end(), [](pii& a, pii& b) {
    if (a.second == b.second) return a.first < b.first;
    return a.second < b.second;
  });
  int cntSS = 0;
  for (int i = 0; i < (n); ++i) {
    if (i == 0) {
      cntSS = 0;
    } else {
      if (prev.second != nums[i].second) {
        cnt2 *= fact[cntSS];
        cntSS = 0;
      }
    }
    cntSS++;
    prev = nums[i];
  }
  cnt2 *= fact[cntSS];
  {};
  {};
  {};
  ModInt bad = cnt1 + cnt2 - cnt3;
  cout << (fact[n] - bad).toInt() << nl;
  return 0;
}
