#include <bits/stdc++.h>
using namespace std;
const long long INF = 1LL << 62;
const int maxn = 3e5 + 10;
const int mod = 998244353;
map<int, int> cntf, cnts;
map<pair<int, int>, int> pcnt;
pair<int, int> p[maxn];
long long f[maxn];
void pre(int n) {
  f[0] = 1;
  for (int i = 1; i <= n; ++i) f[i] = f[i - 1] * i % mod;
}
int main() {
  int n;
  scanf("%d", &n);
  pre(n);
  for (int i = 0; i < n; ++i) scanf("%d%d", &p[i].first, &p[i].second);
  sort(p, p + n);
  bool ok = 1;
  for (int i = 0; i < n; ++i) {
    ++cntf[p[i].first];
    ++cnts[p[i].second];
    if (i != n - 1 && p[i].second > p[i + 1].second) {
      ok = 0;
    }
  }
  long long ans = f[n], tmp = 1;
  for (auto& x : cntf) {
    int num = x.second;
    if (num < 2) continue;
    tmp = tmp * f[num] % mod;
  }
  ans -= tmp;
  tmp = 1;
  for (auto& x : cnts) {
    int num = x.second;
    if (num < 2) continue;
    tmp = tmp * f[num] % mod;
  }
  ans = (ans - tmp) % mod;
  ans = (ans + mod) % mod;
  if (ok) {
    for (int i = 0; i < n; ++i) {
      ++pcnt[p[i]];
    }
    tmp = 1;
    for (auto& x : pcnt) {
      int num = x.second;
      if (num < 2) continue;
      tmp = tmp * f[num] % mod;
    }
    ans = (ans + tmp) % mod;
  }
  printf("%lld\n", ans);
  return 0;
}
