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;
const long long INFL = 1LL << 60;
const long long INF = 1 << 30;
long long MOD = 998244353;
vector<long long> dy = {0, 1, 0, -1, 1, 1, -1, -1, 0};
vector<long long> dx = {1, 0, -1, 0, 1, -1, 1, -1, 0};
void pres(double A) { printf("%.9lf\n", A); }
void BinarySay(long long x, long long y = 60) {
  for (long long i = 0; i < (y); i++) cout << (x >> (y - 1 - i) & 1);
  cout << endl;
}
long long cnt_bit(long long x) { return __builtin_popcountll(x); }
long long pow_long(long long x, long long k) {
  long long res = 1;
  while (k > 0) {
    if (k % 2) res *= x;
    x *= x;
    k /= 2;
  }
  return res;
}
long long pow_mod(long long x, long long k) {
  long long res = 1;
  while (k > 0) {
    if (k % 2) {
      res *= x;
      res %= MOD;
    }
    x *= x;
    x %= MOD;
    k /= 2;
  }
  return res;
}
long long inverse(long long x) { return pow_mod(x, MOD - 2); };
long long gcd(long long a, long long b) {
  if (b == 0) return a;
  return gcd(b, a % b);
}
long long lcm(long long x, long long y) {
  long long res = x / gcd(x, y);
  res *= y;
  return res;
};
const int MAXcomb = 2000010;
long long fac[MAXcomb], finv[MAXcomb], inv[MAXcomb];
void COMinit() {
  fac[0] = fac[1] = 1;
  finv[0] = finv[1] = 1;
  inv[1] = 1;
  for (int i = 2; i < MAXcomb; i++) {
    fac[i] = fac[i - 1] * i % MOD;
    inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD;
    finv[i] = finv[i - 1] * inv[i] % MOD;
  }
}
long long comb(int n, int k) {
  if (n < k) return 0;
  if (n < 0 || k < 0) return 0;
  return fac[n] * finv[k] % MOD * finv[n - k] % MOD;
}
const int MAXkai = 300010;
long long kai_memo[MAXkai];
long long kai(long long N) {
  if (kai_memo[N] != 0) return kai_memo[N];
  if (N <= 1) return 1;
  return kai_memo[N] = N * kai(N - 1) % MOD;
}
long long disit(long long second, long long base = 10) {
  long long res = 0;
  while (second) {
    res++;
    second /= base;
  }
  return res;
}
long long N;
vector<long long> F;
vector<long long> S;
vector<pair<long long, long long>> seg;
void solve() {
  cin >> N;
  for (long long i = 0; i < (N); i++) {
    long long a, b;
    cin >> a >> b;
    F.push_back(a);
    S.push_back(b);
    seg.push_back({a, b});
  }
  long long ans = kai(N);
  sort(F.begin(), F.end());
  sort(S.begin(), S.end());
  sort(seg.begin(), seg.end());
  F.push_back(INFL);
  S.push_back(INFL);
  seg.push_back({INFL, INFL});
  long long first = 1;
  long long cnt = 0;
  for (long long i = 0; i < (N); i++) {
    if (F[i] == F[i + 1]) {
      cnt++;
    } else {
      cnt++;
      first *= kai(cnt);
      first %= MOD;
      cnt = 0;
    }
  }
  long long second = 1;
  cnt = 0;
  for (long long i = 0; i < (N); i++) {
    if (S[i] == S[i + 1]) {
      cnt++;
    } else {
      cnt++;
      second *= kai(cnt);
      second %= MOD;
      cnt = 0;
    }
  }
  long long fs = 1;
  bool sorted = 1;
  for (long long i = 0; i < (N - 1); i++) {
    if (seg[i].second > seg[i + 1].second) sorted = 0;
  }
  fs *= sorted;
  cnt = 0;
  for (long long i = 0; i < (N); i++) {
    if (seg[i] == seg[i + 1]) {
      cnt++;
    } else {
      cnt++;
      fs *= kai(cnt);
      fs %= MOD;
      cnt = 0;
    }
  }
  cout << (ans + MOD - first + MOD - second + fs) % MOD << endl;
}
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  long long T = 1;
  for (long long i = 0; i < (T); i++) solve();
}
