#include <bits/stdc++.h>
using namespace std;
const int N = 300005, md = 998244353;
int n, ans, sa, sb, sc;
struct node {
  int a, b;
} nm[N];
int fac[N];
bool cmp1(node a, node b) {
  if (a.a == b.a) return a.b < b.b;
  return a.a < b.a;
}
bool cmp2(node a, node b) { return a.b < b.b; }
template <class I>
inline void get(I &a) {
  char c = getchar();
  a = 0;
  for (; c < '0' || c > '9'; c = getchar())
    ;
  for (; c >= '0' && c <= '9'; c = getchar()) a = a * 10 + c - '0';
}
int main() {
  get(n);
  for (int i = 1; i <= n; i++) {
    get(nm[i].a);
    get(nm[i].b);
  }
  fac[0] = 1;
  for (int i = 1; i <= n; i++) fac[i] = (long long)fac[i - 1] * i % md;
  sort(nm + 1, nm + 1 + n, cmp1);
  sa = sb = sc = 1;
  ans = fac[n];
  int la = 1;
  for (int i = 1; i <= n; i++)
    if (i != 1 && nm[i].a != nm[i - 1].a) {
      sa = (long long)sa * fac[i - la] % md;
      la = i;
    }
  sa = (long long)sa * fac[n - la + 1] % md;
  la = 1;
  for (int i = 1; i <= n; i++)
    if (!(nm[i].a >= nm[i - 1].a && nm[i].b >= nm[i - 1].b)) {
      sb = 0;
      break;
    }
  for (int i = 1; i <= n; i++)
    if (i != 1 && (nm[i].a != nm[i - 1].a || nm[i].b != nm[i - 1].b)) {
      sb = (long long)sb * fac[i - la] % md;
      la = i;
    }
  sb = (long long)sb * fac[n - la + 1] % md;
  sort(nm + 1, nm + 1 + n, cmp2);
  la = 1;
  for (int i = 1; i <= n; i++)
    if (i != 1 && nm[i].b != nm[i - 1].b) {
      sc = (long long)sc * fac[i - la] % md;
      la = i;
    }
  sc = (long long)sc * fac[n - la + 1] % md;
  ans = ((long long)ans - sa - sc + sb + 2 * md) % md;
  printf("%d\n", ans);
  return 0;
}
