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;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
inline long long int rand(long long int x, long long int y) {
  ++y;
  return (rng() % (y - x)) + x;
}
string to_string(char c) {
  string second(1, c);
  return second;
}
string to_string(bool b) { return (b ? "true" : "false"); }
template <typename A, typename B>
string to_string(pair<A, B> p) {
  return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";
}
template <typename A>
string to_string(A v) {
  bool first = true;
  string res = "{";
  for (const auto &x : v) {
    if (!first) {
      res += ", ";
    }
    first = false;
    res += to_string(x);
  }
  res += "}";
  return res;
}
void degug_out() { cerr << endl; }
template <typename Head, typename... Tail>
void degug_out(Head H, Tail... T) {
  cerr << " " << to_string(H);
  degug_out(T...);
}
inline long long int gcd(long long int a, long long int b) {
  if (a > b) swap(a, b);
  if (a == 0) return b;
  return gcd(b % a, a);
}
long long int n;
pair<long long int, long long int> A[(300006)];
long long int first[(300006)];
const long long int MOD = 998244353;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cin >> n;
  for (long long int i = (1); i <= (long long int)(n); ++i)
    cin >> A[i].first >> A[i].second;
  first[0] = first[1] = 1;
  for (long long int i = (2); i <= (long long int)((300006) - 1); ++i)
    first[i] = first[i - 1] * i % MOD;
  long long int one = 1, two = 1, both = 1;
  sort(A + 1, A + n + 1);
  long long int co = 1;
  long long int ans = first[n];
  for (long long int i = (2); i <= (long long int)(n); ++i)
    if (A[i].first == A[i - 1].first) {
      ++co;
    } else {
      one *= first[co];
      one %= MOD;
      co = 1;
    }
  one *= first[co];
  one %= MOD;
  sort(A + 1, A + n + 1,
       [](pair<long long int, long long int> x,
          pair<long long int, long long int> y) {
         return pair<long long int, long long int>(x.second, x.first) <
                pair<long long int, long long int>(y.second, y.first);
       });
  co = 1;
  for (long long int i = (2); i <= (long long int)(n); ++i)
    if (A[i].second == A[i - 1].second) {
      ++co;
    } else {
      two *= first[co];
      two %= MOD;
      co = 1;
    }
  two *= first[co];
  two %= MOD;
  ans = (ans - one + MOD) % MOD;
  ans = (ans - two + MOD) % MOD;
  sort(A + 1, A + n + 1);
  co = 1;
  for (long long int i = (2); i <= (long long int)(n); ++i) {
    if (A[i].first >= A[i - 1].first && A[i].second >= A[i - 1].second) {
      if (A[i] == A[i - 1])
        ++co;
      else {
        both *= first[co];
        both %= MOD;
        co = 1;
      }
    } else
      both = 0;
  }
  both *= first[co];
  both %= MOD;
  ans = (ans + both) % MOD;
  cout << ans << '\n';
}
