#include <bits/stdc++.h>
using namespace std;
using ll = long long int;
using int64 = long long int;
template <typename T>
void chmax(T &a, T b) {
  a = max(a, b);
}
template <typename T>
void chmin(T &a, T b) {
  a = min(a, b);
}
template <typename T>
void chadd(T &a, T b) {
  a = a + b;
}
int dx[] = {0, 0, 1, -1};
int dy[] = {1, -1, 0, 0};
const ll INF = 1001001001001001LL;
const ll MOD = 998244353LL;
int main() {
  int N;
  scanf("%d", &N);
  vector<ll> perm(N + 1);
  perm[0] = 1;
  for (int i = 1; i <= N; i++) perm[i] = (perm[i - 1] * i) % MOD;
  map<pair<int, int>, int> cnt;
  for (int i = 0; i < N; i++) {
    int a, b;
    scanf("%d%d", &a, &b);
    cnt[make_pair(a, b)]++;
  }
  vector<pair<int, int> > P;
  for (auto e : cnt) P.emplace_back(e.first);
  bool both_sorted = true;
  for (size_t i = 1; i < P.size(); i++) {
    both_sorted &= (P[i - 1].first <= P[i].first);
    both_sorted &= (P[i - 1].second <= P[i].second);
  }
  ll ans = 0;
  {
    ll sum = 1;
    map<int, int> cntA;
    for (auto e : P) {
      int val = e.first;
      int num = cnt[e];
      cntA[val] += num;
    }
    for (auto e : cntA) {
      (sum *= perm[e.second]) %= MOD;
    }
    fprintf(stderr, "sum = %lld\n", sum);
    ans += sum;
    ans %= MOD;
  }
  {
    ll sum = 1;
    map<int, int> cntB;
    for (auto e : P) {
      int val = e.second;
      int num = cnt[e];
      cntB[val] += num;
    }
    for (auto e : cntB) {
      (sum *= perm[e.second]) %= MOD;
    }
    fprintf(stderr, "sum = %lld\n", sum);
    ans += sum;
    ans %= MOD;
  }
  if (both_sorted) {
    ll sum = 1;
    for (auto e : cnt) {
      (sum *= perm[e.second]) %= MOD;
    }
    fprintf(stderr, "sum = %lld\n", sum);
    ans += MOD - sum;
    ans %= MOD;
  }
  ans = perm[N] + MOD - ans;
  ans %= MOD;
  cout << ans << endl;
  return 0;
}
