#include <bits/stdc++.h>
using namespace std;
const int maxn = 3e5 + 10, mod = 998244353;
long long p[maxn];
void add(long long &x, long long y) {
  x += y;
  if (x >= mod) x -= mod;
  if (x < 0) x += mod;
}
int X[maxn], Y[maxn];
int mx[maxn], mn[maxn], vis[maxn];
vector<int> G[maxn];
set<int> s;
int main() {
  p[0] = 1;
  for (int i = 1; i < maxn; i++) p[i] = p[i - 1] * i % mod;
  int n, x, y;
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) mn[i] = n + 1;
  for (int i = 1; i <= n; i++) {
    scanf("%d%d", &x, &y);
    X[x]++;
    Y[y]++;
    mx[x] = max(mx[x], y);
    mn[x] = min(mn[x], y);
    G[x].push_back(y);
  }
  long long ans = p[n], t1 = 1, t2 = 1;
  for (int i = 1; i <= n; i++) {
    t1 = t1 * p[X[i]] % mod;
    t2 = t2 * p[Y[i]] % mod;
  }
  add(ans, -t1);
  add(ans, -t2);
  int ok = 1;
  for (int i = 1; i <= n && ok; i++) {
    if (mn[i] < mx[i - 1])
      ok = 0;
    else
      mx[i] = max(mx[i], mx[i - 1]);
  }
  if (ok) {
    long long tmp = 1;
    for (int i = 1; i <= n; i++) {
      if (G[i].empty()) continue;
      s.clear();
      for (auto v : G[i]) vis[v]++, s.insert(v);
      for (auto it : s) tmp = tmp * p[vis[it]] % mod;
      for (auto it : s) vis[it] = 0;
    }
    add(ans, tmp);
  }
  cout << ans;
}
