#include <bits/stdc++.h>
using namespace std;
struct Pair {
  int f, s;
};
const int maxn = 3e5 + 20;
const int MOD = 998244353;
long long f[maxn];
Pair a[maxn];
int F(long long x) {
  if (x == 1) return f[x] = 1;
  return f[x] = (x * F(x - 1)) % MOD;
}
bool cmp(Pair a, Pair b) { return a.f < b.f; }
bool cmp2(Pair a, Pair b) { return (a.f != b.f) ? a.f < b.f : a.s < b.s; }
int main() {
  int n;
  cin >> n;
  long long ans = F(n);
  for (int i = 0; i < n; i++) cin >> a[i].f >> a[i].s;
  for (int k = 0; k < 2; k++) {
    sort(a, a + n, cmp);
    long long tp = 1;
    int top = a[0].f;
    int cnt = 1;
    for (int j = 1; j < n; j++) {
      if (a[j].f != top) {
        tp = (tp * f[cnt]) % MOD;
        cnt = 1;
        top = a[j].f;
      } else
        cnt++;
    }
    tp = (tp * f[cnt]) % MOD;
    ans = (ans - tp) % MOD;
    for (int i = 0; i < n; i++) swap(a[i].f, a[i].s);
  }
  sort(a, a + n, cmp2);
  long long tp = 1;
  Pair top = a[0];
  int cnt = 1;
  for (int j = 1; j < n; j++) {
    if (a[j].s < top.s) tp = 0;
    if (a[j].f != top.f || a[j].s != top.s) {
      tp = (tp * f[cnt]) % MOD;
      cnt = 1;
      top = a[j];
    } else
      cnt++;
  }
  tp = (tp * f[cnt]) % MOD;
  ans = (ans + tp) % MOD;
  if (ans < 0) ans += MOD;
  cout << ans << endl;
}
