#include <bits/stdc++.h>
using namespace std;
struct node {
  long long x;
  long long y;
} p[300005];
long long jc[300005] = {0};
inline long long rd() {
  long long s = 0, w = 1;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-') w = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    s = s * 10 + ch - '0';
    ch = getchar();
  }
  return s * w;
}
bool cmp1(node a, node b) { return a.x < b.x; }
bool cmp2(node a, node b) { return a.y < b.y; }
bool cmp3(node a, node b) {
  if (a.x == b.x)
    return a.y < b.y;
  else
    return a.x < b.x;
}
int main() {
  long long n;
  cin >> n;
  jc[0] = 1;
  for (int i = 1; i <= n; i++) {
    jc[i] = jc[i - 1] * i % 998244353;
  }
  for (int i = 0; i < n; i++) {
    cin >> p[i].x >> p[i].y;
  }
  sort(p, p + n, cmp1);
  long long ans1 = 1;
  for (int i = 1; i < n; i++) {
    long long t = 1;
    while (i < n && p[i].x == p[i - 1].x) {
      t++;
      i++;
    }
    ans1 = ((ans1 + 998244353) % 998244353 * jc[t] + 998244353) % 998244353;
  }
  sort(p, p + n, cmp2);
  long long ans2 = 1;
  for (int i = 1; i < n; i++) {
    long long t = 1;
    while (i < n && p[i].y == p[i - 1].y) {
      t++;
      i++;
    }
    ans2 = ((ans2 + 998244353) % 998244353 * jc[t] + 998244353) % 998244353;
  }
  sort(p, p + n, cmp3);
  long long ans3 = 1;
  for (int i = 1; i < n; i++) {
    if (p[i].y < p[i - 1].y) ans3 = 0;
  }
  for (int i = 1; i < n; i++) {
    long long t = 1;
    while (i < n && p[i].x == p[i - 1].x && p[i].y == p[i - 1].y) {
      t++;
      i++;
    }
    ans3 = ((ans3 + 998244353) % 998244353 * jc[t] + 998244353) % 998244353;
  }
  long long ans;
  ans = (jc[n] - ans1 - ans2 + ans3) % 998244353;
  ans += 998244353;
  ans %= 998244353;
  cout << ans;
  return 0;
}
