#include <bits/stdc++.h>
using namespace std;
const int mod = 998244353;
long long fact[300005];
struct node {
  int a, b;
} p[300005];
bool cmp1(node q, node w) {
  if (q.a != w.a) return q.a < w.a;
  return q.b < w.b;
}
bool cmp2(node q, node w) { return q.b < w.b; }
int main() {
  int n;
  cin >> n;
  fact[0] = 1;
  for (register int i = 1; i <= n; i++) {
    scanf("%d%d", &p[i].a, &p[i].b);
    fact[i] = (fact[i - 1] * i) % mod;
  }
  sort(p + 1, p + 1 + n, cmp1);
  long long ans = 1;
  long long x2 = 1;
  int mrk = 1;
  int cnt = 1;
  for (register int i = n; i >= 1; i--) {
    if (p[i].a == p[i - 1].a) {
      if (p[i].b > p[i - 1].b) {
        x2 = (x2 * fact[mrk]) % mod;
        mrk = 1;
      } else {
        mrk++;
      }
      cnt++;
    } else {
      if (p[i].b < p[i - 1].b) {
        x2 = 0;
      } else {
        x2 = (x2 * fact[mrk]) % mod;
        mrk = 1;
      }
      ans = (ans * fact[cnt]) % mod;
      cnt = 1;
    }
  }
  ans = (ans - x2 + mod) % mod;
  sort(p + 1, p + 1 + n, cmp2);
  cnt = 1;
  long long ans2 = 1;
  for (register int i = n; i >= 1; i--) {
    if (p[i].b == p[i - 1].b) {
      cnt++;
    } else {
      ans2 = (ans2 * fact[cnt]) % mod;
      cnt = 1;
    }
  }
  ans = (ans + ans2) % mod;
  ans = (fact[n] - ans + mod) % mod;
  printf("%I64d\n", ans);
  return 0;
}
