#include <bits/stdc++.h>
using namespace std;
const int maxn = 3e5 + 5;
const int Mod = 998244353;
const int INF = 0x3f3f3f3f;
const long long LL_INF = 0x3f3f3f3f3f3f3f3f;
const double e = exp(1);
const double PI = acos(-1);
const double ERR = 1e-10;
struct node {
  int a, b;
} data[maxn];
bool cmp1(node A, node B) { return A.a < B.a; }
bool cmp2(node A, node B) { return A.b < B.b; }
bool cmp3(node A, node B) { return A.a < B.a || (A.a == B.a && A.b < B.b); }
long long fac[maxn];
int main() {
  int n;
  scanf("%d", &n);
  fac[0] = 1;
  for (int i = 1; i <= 300000; i++) {
    fac[i] = fac[i - 1] * 1LL * i % Mod;
  }
  for (int i = 1; i <= n; i++) {
    scanf("%d%d", &data[i].a, &data[i].b);
  }
  sort(data + 1, data + 1 + n, cmp1);
  long long res1 = 1;
  int cnt = 1;
  for (int i = 2; i <= n + 1; i++) {
    if (data[i].a == data[i - 1].a) {
      cnt++;
    } else {
      res1 = res1 * fac[cnt] % Mod;
      cnt = 1;
    }
  }
  sort(data + 1, data + 1 + n, cmp2);
  long long res2 = 1;
  cnt = 1;
  for (int i = 2; i <= n + 1; i++) {
    if (data[i].b == data[i - 1].b) {
      cnt++;
    } else {
      res2 = res2 * fac[cnt] % Mod;
      cnt = 1;
    }
  }
  sort(data + 1, data + 1 + n, cmp3);
  bool isok = true;
  for (int i = 2; i <= n; i++) {
    if (!(data[i].a >= data[i - 1].a && data[i].b >= data[i - 1].b)) {
      isok = false;
      break;
    }
  }
  long long res3 = (long long)isok;
  cnt = 1;
  for (int i = 2; i <= n + 1; i++) {
    if (data[i].a == data[i - 1].a && data[i].b == data[i - 1].b) {
      cnt++;
    } else {
      res3 = res3 * fac[cnt] % Mod;
      cnt = 1;
    }
  }
  long long ans = fac[n];
  ans = (ans - res1 + Mod) % Mod;
  ans = (ans - res2 + Mod) % Mod;
  ans = (ans + res3) % Mod;
  printf("%lld\n", ans);
  return 0;
}
