#include <bits/stdc++.h>
using namespace std;
long long _mod = 998244353;
long long mod(long long x) { return ((x % _mod) + _mod) % _mod; }
long long f[(int)3e5 + 7];
void init() {
  f[0] = 1;
  for (int i = 1; i < 3e5 + 7; ++i) f[i] = mod(f[i - 1] * i);
}
int n;
pair<int, int> a[(int)3e5 + 7];
int main() {
  cin.tie(0), cout.tie(0), ios::sync_with_stdio(0);
  init();
  cin >> n;
  for (int i = 0; i < n; ++i) cin >> a[i].first >> a[i].second;
  sort(a, a + n);
  long long c = 1, b = 1, ab = 0;
  bool ok = true;
  for (int i = 1; i < n; ++i) ok &= a[i - 1].second <= a[i].second;
  if (ok) ab = 1;
  for (int l = 0, r = 1; r <= n; ++r) {
    if (r == n || a[r] != a[l]) ab = mod(ab * f[r - l]), l = r;
  }
  for (int l = 0, r = 1; r <= n; ++r) {
    if (r == n || a[r].first != a[l].first) c = mod(c * f[r - l]), l = r;
  }
  sort(a, a + n,
       [](pair<int, int> a, pair<int, int> b) { return a.second < b.second; });
  for (int l = 0, r = 1; r <= n; ++r) {
    if (r == n || a[r].second != a[l].second) b = mod(b * f[r - l]), l = r;
  }
  cout << mod(f[n] - c - b + ab) << endl;
  return 0;
}
