#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
using namespace std;
const long long inf = 1e18;
const long double pi = 3.141592653589793238;
long long MOD = 998244353;
const long long N = 3e5 + 10;
long long n;
pair<long long, long long> a[N], b[N];
long long fact[N];
bool cmp(pair<long long, long long>& a, pair<long long, long long>& b) {
  return (a.second < b.second) || (a.second == b.second && a.first < b.first);
}
bool cmp2(pair<long long, long long>& a, pair<long long, long long>& b) {
  return (a.first < b.first) || (a.first == b.first && a.second < b.second);
}
void solve() {
  cin >> n;
  for (long long i = 0; i < n; i++) cin >> a[i].first >> a[i].second;
  for (long long i = 0; i < n; i++) b[i] = a[i];
  long long op1 = 1;
  sort(a, a + n, cmp2);
  long long cur = 0;
  for (long long i = 0; i < n; i++) {
    if (i > 0 && a[i].first != a[i - 1].first) {
      op1 *= (fact[cur]);
      op1 %= MOD;
      op1 += MOD;
      op1 %= MOD;
      cur = 1;
    } else
      cur++;
  }
  op1 *= fact[cur];
  op1 %= MOD;
  op1 += MOD;
  op1 %= MOD;
  sort(b, b + n, cmp);
  long long op2 = 1;
  cur = 0;
  for (long long i = 0; i < n; i++) {
    if (i > 0 && b[i].second != b[i - 1].second) {
      op2 *= (fact[cur]);
      op2 %= MOD;
      op2 += MOD;
      op2 %= MOD;
      cur = 1;
    } else
      cur++;
  }
  op2 *= fact[cur];
  op2 %= MOD;
  op2 += MOD;
  op2 %= MOD;
  long long op3 = 1;
  cur = 0;
  for (long long i = 0; i < n; i++) {
    if ((a[i].first != b[i].first) || (a[i].second != b[i].second)) op3 = 0;
    if (i > 0 &&
        ((b[i].second != b[i - 1].second) || (a[i].first != a[i - 1].first))) {
      op3 *= (fact[cur]);
      op3 %= MOD;
      op3 += MOD;
      op3 %= MOD;
      cur = 1;
    } else
      cur++;
  }
  op3 *= fact[cur];
  op3 %= MOD;
  op3 += MOD;
  op3 %= MOD;
  long long ans = fact[n] - (op1 + op2 - op3);
  ans %= MOD;
  ans += MOD;
  ans %= MOD;
  cout << ans << '\n';
}
signed main() {
  ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
  long long t = 1;
  fact[0] = 1;
  for (long long i = 1; i < N; i++) {
    fact[i] = fact[i - 1] * i;
    fact[i] %= MOD;
    fact[i] += MOD;
    fact[i] %= MOD;
  }
  while (t--) {
    solve();
  }
}
