#include <bits/stdc++.h>
using namespace std;
pair<int, int> a[300005];
int b[300005];
int c[300005];
long long ans;
long long fact[300005];
int n;
const long long MOD = 998244353;
int main() {
  cin.tie(0);
  cout.tie(0);
  ios_base::sync_with_stdio(0);
  cin >> n;
  int i;
  fact[0] = 1;
  for (i = 1; i <= n; i++) {
    fact[i] = fact[i - 1] * i % MOD;
    cin >> a[i].first >> a[i].second;
    b[a[i].first]++;
    c[a[i].second]++;
  }
  long long k = 1;
  ans = fact[n];
  for (i = 1; i <= n; i++) {
    k *= fact[b[i]];
    k %= MOD;
  }
  ans -= k;
  k = 1;
  for (i = 1; i <= n; i++) {
    k *= fact[c[i]];
    k %= MOD;
  }
  ans -= k;
  ans += 2 * MOD;
  sort(a + 1, a + n + 1);
  int cnt = 1;
  k = 1;
  int isok = 1;
  for (i = 2; i <= n; i++) {
    if (a[i] == a[i - 1])
      cnt++;
    else {
      k = fact[cnt] * k % MOD;
      cnt = 1;
    }
    if (a[i].second < a[i - 1].second) isok = 0;
  }
  k = fact[cnt] * k % MOD;
  ans += k * isok;
  ans %= MOD;
  cout << ans;
  return 0;
}
