#include <bits/stdc++.h>
using namespace std;
const long long mod = 998244353;
long long a[301000], b[301000], fact[301000];
pair<long long, long long> p[301000];
void f(long long n) {
  fact[0] = 1;
  for (long long i = 1; i <= n; i++) fact[i] = fact[i - 1] * i % mod;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  long long i, n, cnt, ans, kol;
  cin >> n;
  for (i = 0; i < n; i++) {
    cin >> a[i] >> b[i];
    p[i] = {a[i], b[i]};
  }
  f(n);
  ans = fact[n];
  sort(a, a + n);
  cnt = 1;
  kol = 1;
  for (i = 1; i < n; i++)
    if (a[i] == a[i - 1])
      cnt++;
    else {
      kol = kol * fact[cnt] % mod;
      cnt = 1;
    }
  kol = kol * fact[cnt] % mod;
  ans = (ans - kol + mod) % mod;
  sort(b, b + n);
  cnt = 1;
  kol = 1;
  for (i = 1; i < n; i++)
    if (b[i] == b[i - 1])
      cnt++;
    else {
      kol = kol * fact[cnt] % mod;
      cnt = 1;
    }
  kol = kol * fact[cnt] % mod;
  ans = (ans - kol + mod) % mod;
  sort(p, p + n);
  cnt = 1;
  kol = 1;
  for (i = 1; i < n; i++)
    if (p[i] == p[i - 1])
      cnt++;
    else {
      kol = kol * fact[cnt] % mod;
      cnt = 1;
    }
  kol = kol * fact[cnt] % mod;
  for (i = 1; i < n; i++)
    if (p[i - 1].second > p[i].second) break;
  if (i == n) ans = (ans + kol) % mod;
  cout << ans;
  return 0;
}
