#include <bits/stdc++.h>
using namespace std;
template <class T>
inline void read(T &k) {
  k = 0;
  int flag = 1;
  char c = getchar();
  while (c < '0' || c > '9') {
    if (c == '-') flag = -1;
    c = getchar();
  }
  while (c >= '0' && c <= '9') {
    k = k * 10 + c - '0';
    c = getchar();
  }
  k *= flag;
}
const int N = 300005;
struct node {
  int x, y;
  bool operator<(const node &k) const {
    if (x != k.x) return x < k.x;
    return y < k.y;
  }
} a[N];
int n, m, k, s, t;
long long ans, b[N];
void calc() {
  long long l = 1, xx = 1;
  while (l <= n) {
    long long r = l + 1;
    while (r <= n && a[l].x == a[r].x) ++r;
    xx = xx * b[r - l] % 998244353;
    l = r;
  }
  ans -= xx;
}
void calc1() {
  for (int i = 2; i <= n; ++i)
    if (a[i].y < a[i - 1].y) return;
  long long l = 1, xx = 1;
  while (l <= n) {
    long long r = l + 1;
    while (r <= n && a[l].x == a[r].x && a[l].y == a[r].y) ++r;
    xx = xx * b[r - l] % 998244353;
    l = r;
  }
  ans += xx;
}
int main() {
  read(n);
  for (int i = 1; i <= n; ++i) {
    read(a[i].x);
    read(a[i].y);
  }
  b[0] = 1;
  for (int i = 1; i <= n; ++i) b[i] = b[i - 1] * i % 998244353;
  ans = b[n];
  sort(a + 1, a + 1 + n);
  calc();
  for (int i = 1; i <= n; ++i) swap(a[i].x, a[i].y);
  sort(a + 1, a + 1 + n);
  calc();
  calc1();
  ans = (ans % 998244353 + 998244353) % 998244353;
  printf("%lld\n", ans);
}
