#include <bits/stdc++.h>
using namespace std;
const long long M = 998244353LL;
long long fact[1000005];
void init() {
  fact[0] = 1;
  for (long long i = 1; i <= 1000000; i++) {
    fact[i] = (i * fact[i - 1]) % M;
    fact[i] %= M;
  }
}
void solve() {
  init();
  int n;
  scanf("%d", &n);
  map<long long, long long> first, second;
  map<pair<long long, long long>, long long> both;
  vector<pair<long long, long long>> v;
  for (int i = 0; i < n; i++) {
    long long a, b;
    scanf("%d %d", &a, &b);
    first[a]++;
    second[b]++;
    v.emplace_back(a, b);
    both[make_pair(a, b)]++;
  }
  sort(v.begin(), v.end());
  long long f = 1, s = 1, b = 1;
  for (auto it : first) {
    f *= fact[it.second];
    f %= M;
  }
  for (auto it : second) {
    s *= fact[it.second];
    s %= M;
  }
  for (auto it : both) {
    b *= fact[it.second];
    b %= M;
  }
  for (int i = 0; i < n - 1; i++) {
    if (v[i + 1].second < v[i].second) {
      b = 0;
      break;
    }
  }
  long long ans = fact[n] - f - s + b;
  while (ans < 0) ans = (ans + M) % M;
  printf("%lld\n", ans);
}
int main() {
  int tc = 1;
  while (tc--) {
    solve();
  }
  return 0;
}
