#include <bits/stdc++.h>
using namespace std;
long long s1, s2, s3, s4;
int n;
struct mzls {
  int x, y;
  bool operator<(const mzls &a1) const {
    return x == a1.x ? y < a1.y : x < a1.x;
  }
} a[1000005];
map<int, map<int, int> > mp;
int t1[1000005], t2[1000005];
int main() {
  s1 = s2 = s3 = s4 = 1;
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%d%d", &a[i].x, &a[i].y);
    mp[a[i].x][a[i].y]++;
    t1[a[i].x]++, t2[a[i].y]++;
    s3 = s3 * t1[a[i].x] % 998244353;
    s4 = s4 * t2[a[i].y] % 998244353;
    s1 = s1 * mp[a[i].x][a[i].y] % 998244353;
    s2 = s2 * i % 998244353;
  }
  sort(a + 1, a + n + 1);
  bool f1 = 0;
  for (int i = 1; i < n; i++)
    if (a[i].x > a[i + 1].x || a[i].y > a[i + 1].y) f1 = 1;
  s2 = ((s2 - s3 + 998244353) % 998244353 - s4 + 998244353) % 998244353;
  if (!f1) s2 = (s2 + s1) % 998244353;
  printf("%lld", s2);
  return 0;
}
