#include <bits/stdc++.h>
using namespace std;
const long long M = 998244353;
long long f[300005];
vector<long long> m1[300005], m2[300005];
int32_t main() {
  std::ios::sync_with_stdio(0);
  std::cin.tie(0);
  ;
  long long n;
  cin >> n;
  for (long long i = 0; i < n; i++) {
    long long x, y;
    cin >> x >> y;
    m1[x].push_back(y);
    m2[y].push_back(x);
  }
  f[0] = 1;
  for (long long i = 1; i < 300005; i++) f[i] = f[i - 1] * i % M;
  long long ans = f[n], d1 = 1, d2 = 1, d3 = 1;
  for (long long i = 1; i <= n; i++) d1 = d1 * f[m1[i].size()] % M;
  for (long long i = 1; i <= n; i++) d2 = d2 * f[m2[i].size()] % M;
  ans = ((ans - d1 - d2) % M + M) % M;
  vector<long long> a, b;
  for (long long i = 1; i <= n; i++) {
    if (m1[i].size() == 0) continue;
    sort((m1[i]).begin(), (m1[i]).end());
    map<long long, long long> m;
    for (long long e : m1[i]) m[e]++;
    for (pair<long long, long long> p : m) d3 = d3 * f[p.second] % M;
    a.push_back(i);
    b.push_back(m1[i][0]);
    b.push_back(m1[i].back());
  }
  for (long long i = 0; i < a.size() - 1; i++)
    if (a[i + 1] < a[i]) d3 = 0;
  for (long long i = 0; i < b.size() - 1; i++)
    if (b[i + 1] < b[i]) d3 = 0;
  ans = (ans + d3) % M;
  cout << ans << '\n';
}
