#include <bits/stdc++.h>
#pragma GCC optimize("Ofast,unroll-loops,no-stack-protector")
using namespace std;
using ld = long double;
using lli = long long;
using ii = pair<int, int>;
using vi = vector<int>;
template <int p>
struct Modular {
  Modular operator+(const Modular &m) const { return Modular(v + m.v); }
  Modular operator-(const Modular &m) const { return Modular(v - m.v + p); }
  Modular operator*(const Modular &m) const { return Modular(1LL * v * m.v); }
  Modular inv() const { return Modular(minv(v, p)); }
  Modular operator-() const { return Modular(-v); }
  Modular &operator+=(const Modular &m) { return *this = *this + m; }
  Modular &operator-=(const Modular &m) { return *this = *this - m; }
  Modular &operator*=(const Modular &m) { return *this = *this * m; }
  friend ostream &operator<<(ostream &os, const Modular &m) {
    return os << m.v;
  }
  friend istream &operator>>(istream &is, Modular &m) { return is >> m.v; }
  int v;
  Modular(lli x = 0) : v((x %= p) < 0 ? x + p : x) {}

 private:
  static int minv(int a, int m) {
    a %= m;
    assert(a);
    return a == 1 ? 1 : int(m - lli(minv(m, a)) * lli(m) / a);
  }
};
using Mint = Modular<998244353>;
Mint fac(int i) {
  static vector<Mint> dp(1, 1);
  while (dp.size() <= i) dp.push_back(dp.back() * dp.size());
  return dp[i];
}
const int N = 3e5 + 5;
ii second[N];
int n;
int main() {
  cin.tie(0)->sync_with_stdio(0), cout.tie(0);
  while (cin >> n) {
    for (auto i = (0) - ((0) > (n)); i != (n) - ((0) > (n));
         i += 1 - 2 * ((0) > (n))) {
      cin >> second[i].first >> second[i].second;
    }
    Mint ans = fac(n);
    for (auto it = (0) - ((0) > (2)); it != (2) - ((0) > (2));
         it += 1 - 2 * ((0) > (2))) {
      map<int, int> cnt;
      for (auto i = (0) - ((0) > (n)); i != (n) - ((0) > (n));
           i += 1 - 2 * ((0) > (n))) {
        cnt[second[i].first]++;
        swap(second[i].first, second[i].second);
      }
      Mint bad = 1;
      ;
      for (auto [x, k] : cnt) bad *= fac(k);
      ans -= bad;
    }
    sort(second, second + n);
    Mint common = 1;
    for (auto i = (1) - ((1) > (n)); i != (n) - ((1) > (n));
         i += 1 - 2 * ((1) > (n)))
      if (second[i].second < second[i - 1].second) {
        common = 0;
      }
    {
      map<ii, int> cnt;
      for (auto i = (0) - ((0) > (n)); i != (n) - ((0) > (n));
           i += 1 - 2 * ((0) > (n)))
        cnt[second[i]]++;
      ;
      for (auto [x, k] : cnt) common *= fac(k);
      ans += common;
    }
    cout << ans << '\n';
  }
  return 0;
}
