#include #include using namespace std; using namespace atcoder; using ll = long long; using mint = modint998244353; using S = pair; using F = mint; S op(S lhs, S rhs){ return make_pair(lhs.first + rhs.first, lhs.second + rhs.second); } S e(){ return make_pair(0, 0); } S mapping(F f, S x){ return make_pair(x.first + f * x.second, x.second); } F composition(F f, F g){ return f + g; } F id(){return 0;} int main(){ ios::sync_with_stdio(false); cin.tie(0); int n; cin >> n; vector> a(n), b(n); vector ca(2 * n); for(int i = 0; i < n; i++){ int l, r; cin >> l >> r; a[i] = make_pair(l, r); ca[2 * i] = l; ca[2 * i + 1] = r + 1; } sort(ca.begin(), ca.end()); ca.erase(unique(ca.begin(), ca.end()), ca.end()); vector tmp(ca.size()); for(int i = 0; i + 1 < ca.size(); i++){ tmp[i].second = ca[i + 1] - ca[i]; } lazy_segtree seg(tmp); mint ans; for(int i = 0; i < n; i++){ int l, r; tie(l, r) = a[i]; mint div = mint(1) / (r - l + 1); l = lower_bound(ca.begin(), ca.end(), l) - ca.begin(); r = upper_bound(ca.begin(), ca.end(), r) - ca.begin(); ans += seg.prod(l, r).first * div; seg.apply(l, r, div); } seg = lazy_segtree(tmp); for(int i = n - 1; i >= 0; i--){ int l, r; tie(l, r) = a[i]; mint div = mint(1) / (r - l + 1); l = lower_bound(ca.begin(), ca.end(), l) - ca.begin(); r = upper_bound(ca.begin(), ca.end(), r) - ca.begin(); ans += seg.prod(l, r).first * div; seg.apply(l, r, div); } ans = mint(n) * (n - 1) - ans; ans /= 2; for(int i = 3; i <= n; i++) ans *= i; cout << ans.val() << '\n'; }