#include <bits/stdc++.h>
#pragma GCC optimize "03"
using namespace std;
const long long int N = 3e5 + 5;
const long long int mod = 998244353;
const long long int inf = 1e9 + 9;
long long int fac[N];
pair<long long int, long long int> a[N];
bool cmp1(pair<long long int, long long int> x,
          pair<long long int, long long int> y) {
  if (x.first != y.first)
    return x.first < y.first;
  else
    return x.second < y.second;
}
bool cmp2(pair<long long int, long long int> x,
          pair<long long int, long long int> y) {
  if (x.second != y.second)
    return x.second < y.second;
  else
    return x.first < y.first;
}
signed main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  fac[0] = 1;
  for (long long int i = 1; i < N; i++) fac[i] = (fac[i - 1] * i) % mod;
  long long int n;
  cin >> n;
  for (long long int i = 1; i <= n; i++) cin >> a[i].first >> a[i].second;
  sort(a + 1, a + n + 1, cmp1);
  long long int c = 1, p = 1, ans = 0;
  for (long long int i = 2; i <= n; i++) {
    if (a[i].first == a[i - 1].first)
      c++;
    else {
      p = (p * fac[c]) % mod;
      c = 1;
    }
  }
  p = (p * fac[c]) % mod;
  ans = (ans + p) % mod;
  sort(a + 1, a + n + 1, cmp2);
  c = 1, p = 1;
  for (long long int i = 2; i <= n; i++) {
    if (a[i].second == a[i - 1].second)
      c++;
    else {
      p = (p * fac[c]) % mod;
      c = 1;
    }
  }
  p = (p * fac[c]) % mod;
  ans = (ans + p) % mod;
  bool flag = 1;
  for (long long int i = 2; i <= n; i++) {
    if (a[i].first >= a[i - 1].first && a[i].second >= a[i - 1].second)
      continue;
    else
      flag = 0;
  }
  if (flag == 1) {
    c = 1, p = 1;
    for (long long int i = 2; i <= n; i++) {
      if (a[i].second == a[i - 1].second && a[i].first == a[i - 1].first)
        c++;
      else {
        p = (p * fac[c]) % mod;
        c = 1;
      }
    }
    p = (p * fac[c]) % mod;
    ans = (ans - p + mod) % mod;
  }
  ans = (fac[n] - ans + mod) % mod;
  cout << ans;
  return 0;
}
