#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 300;
const int SQRTN = 320;
const int LOGN = 20;
const double PI = acos(-1);
const double TAU = 2 * PI;
int a[2], n;
int freq[2][N];
map<pair<int, int>, int> m;
int fact[N];
vector<pair<int, int> > v;
int f(int x) {
  if (!x) return 1;
  if (fact[x]) return fact[x];
  return fact[x] = f(x - 1) * 1ll * x % 998244353;
}
bool c() {
  sort(v.begin(), v.end());
  for (int i = 1; i < v.size(); i++)
    if (v[i].second < v[i - 1].second) return false;
  return true;
  ;
}
void solve() {
  cin >> n;
  for (int i = 0; i < n; i++) {
    for (int x = 0; x < 2; x++) {
      cin >> a[x];
      freq[x][a[x]]++;
    }
    v.push_back(make_pair(a[0], a[1]));
    m[make_pair(a[0], a[1])]++;
  }
  int ans = f(n);
  int t[2] = {1, 1};
  for (int i = 0; i <= n; i++) {
    for (int j = 0; j < 2; j++) {
      t[j] = t[j] * 1ll * f(freq[j][i]) % 998244353;
    }
  }
  t[0] += t[1];
  t[0] %= 998244353;
  ans -= t[0];
  ans %= 998244353;
  ans += 998244353;
  ans %= 998244353;
  map<pair<int, int>, int>::iterator it = m.begin();
  int aa = 1;
  for (; it != m.end(); it++) {
    aa = aa * 1ll * f(it->second) % 998244353;
  }
  if (!c()) aa = 0;
  ans += aa;
  ans %= 998244353;
  cout << ans << endl;
}
signed main() {
  int t = 1;
  while (t--) {
    solve();
  }
  return 0;
}
