#include <bits/stdc++.h>
using namespace std;
const int Mod = 998244353;
const int Nmax = 3e5 + 5;
int fact[Nmax], ap[Nmax], a[Nmax], b[Nmax];
int n;
int inm(int x, int y) { return (long long)x * y % Mod; }
int bad(int a[]) {
  int i;
  for (i = 1; i <= n; ++i) ap[i] = 0;
  for (i = 1; i <= n; ++i) ++ap[a[i]];
  int ans = 1;
  for (i = 1; i <= n; ++i) ans = inm(ans, fact[ap[i]]);
  return ans;
}
int bad2() {
  map<pair<int, int>, int> mp, mp2;
  int i;
  for (i = 1; i <= n; ++i) ++mp[{a[i], b[i]}];
  sort(a + 1, a + n + 1);
  sort(b + 1, b + n + 1);
  int ans = 1;
  for (auto it : mp) ans = inm(ans, fact[it.second]);
  for (i = 1; i <= n; ++i)
    if (!mp[{a[i], b[i]}])
      return 0;
    else
      --mp[{a[i], b[i]}];
  return ans;
}
int main() {
  cin.sync_with_stdio(false);
  cin.tie(0);
  int i;
  cin >> n;
  fact[0] = 1;
  for (i = 1; i <= n; ++i) fact[i] = inm(fact[i - 1], i);
  for (i = 1; i <= n; ++i) cin >> a[i] >> b[i];
  int ans = ((bad(a) + bad(b)) % Mod - bad2() + Mod) % Mod;
  ans = (fact[n] - ans + Mod) % Mod;
  cout << ans << '\n';
  return 0;
}
