#include <bits/stdc++.h>
using namespace std;
const int N = 3e5 + 5;
const int mod = 998244353;
int n;
pair<int, int> p[N];
long long val[N];
int c[N];
int comp(pair<int, int> a, pair<int, int> b) { return a.second < b.second; }
void init() {
  val[1] = 1;
  for (int i = 2; i <= n; i++) val[i] = (val[i - 1] * i) % mod;
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%d%d", &p[i].first, &p[i].second);
  }
  init();
  sort(p + 1, p + n + 1, comp);
  int s = 1;
  long long ans1 = 1;
  int fl = 0;
  int ma = 0;
  while (s <= n) {
    int ss = s;
    int tmp1 = 0, tmp2 = 1000000;
    while (ss <= n && p[ss].second == p[s].second)
      tmp1 = max(tmp1, p[ss].first), tmp2 = min(tmp2, p[ss].first), ss++;
    ans1 = (ans1 * val[ss - s]) % mod;
    if (tmp2 < ma) fl = 1;
    ma = max(ma, tmp1);
    s = ss;
  }
  sort(p + 1, p + n + 1);
  s = 1;
  ma = 0;
  long long ans2 = 1;
  while (s <= n) {
    int ss = s;
    int tmp1 = 0, tmp2 = 1000000;
    while (ss <= n && p[ss].first == p[s].first)
      tmp1 = max(tmp1, p[ss].second), tmp2 = min(tmp2, p[ss].second), ss++;
    ans2 = (ans2 * val[ss - s]) % mod;
    if (tmp2 < ma) fl = 1;
    ma = max(ma, tmp1);
    s = ss;
  }
  if (fl) {
    printf("%I64d\n", (val[n] - ans1 - ans2 + 2 * mod) % mod);
  } else {
    int s = 1;
    int cnt = 0;
    long long ans3 = 1;
    while (s <= n) {
      int ss = s;
      cnt = 0;
      while (ss <= n && p[ss].first == p[s].first)
        c[cnt++] = p[ss].second, ss++;
      sort(c, c + cnt);
      int t = 0;
      while (t < cnt) {
        int tt = t;
        while (tt < cnt && c[tt] == c[t]) tt++;
        ans3 = (ans3 * val[tt - t]) % mod;
        t = tt;
      }
      s = ss;
    }
    printf("%I64d\n", (val[n] - ans1 - ans2 + ans3 + 2 * mod) % mod);
  }
  return 0;
}
