#include <bits/stdc++.h>
using namespace std;
const long long mod = 1e9 + 7;
const int infi = 1147483600;
const long long infl = 1e18 + 5;
const char ENDL = '\n';
const long long MOD = 998244353;
struct COMB {
  int L;
  vector<long long> kaijo, gyaku;
  long long mod;
  void __init__(int sz, long long md) {
    L = sz;
    mod = md;
    kaijo.resize(L + 1, 0);
    gyaku.resize(L + 1, 0);
    kaijo[0] = 1;
    for (long long i = (long long)1; i < (long long)L + 1; i++)
      kaijo[i] = (kaijo[i - 1] * i) % mod;
    gyaku[L] = powmod(kaijo[L], mod - 2, mod);
    for (long long i = 0; i < (long long)L; i++)
      gyaku[L - 1 - i] = gyaku[L - i] * (L - i) % mod;
  }
  long long powmod(long long x, long long y, long long mod) {
    long long ans = 1;
    while (y > 0) {
      if (y & 1) {
        ans *= x;
        ans %= mod;
      }
      x = x * x % mod;
      y >>= 1;
    }
    return ans;
  }
  long long calc(int n, int k) {
    if (n > L) return -1;
    return (((kaijo[n] * gyaku[k]) % mod) * gyaku[n - k]) % mod;
  }
  long long kai(int i) { return kaijo[i]; }
  long long gya(int i) { return gyaku[i]; }
};
bool comp1(pair<long long, long long> a, pair<long long, long long> b) {
  return a.first < b.first;
}
bool comp2(pair<long long, long long> a, pair<long long, long long> b) {
  return a.second < b.second;
}
bool comp3(pair<long long, long long> a, pair<long long, long long> b) {
  if (a.first == b.first)
    return a.second < b.second;
  else
    return a.first < b.first;
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  long long n;
  cin >> n;
  vector<pair<long long, long long> > s(n);
  for (long long i = 0; i < (long long)n; i++) {
    long long a, b;
    cin >> a >> b;
    s[i] = make_pair(a, b);
  }
  COMB comb;
  comb.__init__(n, MOD);
  long long sa = 1;
  sort((s).begin(), (s).end(), comp1);
  long long bef = s[0].first;
  long long cnt = 1;
  for (long long i = (long long)1; i < (long long)n; i++) {
    if (s[i].first == bef) {
      cnt++;
    } else {
      sa *= comb.kai(cnt);
      sa %= MOD;
      cnt = 1;
      bef = s[i].first;
    }
  }
  sa *= comb.kai(cnt);
  sa %= MOD;
  long long sb = 1;
  sort((s).begin(), (s).end(), comp2);
  bef = s[0].second;
  cnt = 1;
  for (long long i = (long long)1; i < (long long)n; i++) {
    if (s[i].second == bef) {
      cnt++;
    } else {
      sb *= comb.kai(cnt);
      sb %= MOD;
      cnt = 1;
      bef = s[i].second;
    }
  }
  sb *= comb.kai(cnt);
  sb %= MOD;
  sort((s).begin(), (s).end(), comp3);
  long long ss = 1;
  long long befa = s[0].first, befb = s[0].second;
  cnt = 1;
  for (long long i = (long long)1; i < (long long)n; i++) {
    if (s[i - 1].first > s[i].first || s[i - 1].second > s[i].second) {
      ss = 0;
      break;
    }
    if (s[i].first == befa && s[i].second == befb) {
      cnt++;
    } else {
      ss *= comb.kai(cnt);
      ss %= MOD;
      cnt = 1;
      befa = s[i].first;
      befb = s[i].second;
    }
  }
  ss *= comb.kai(cnt);
  ss %= MOD;
  cout << (2 * MOD + comb.kai(n) - sa - sb + ss) % MOD << ENDL;
}
