#include namespace { #pragma GCC diagnostic ignored "-Wunused-function" #include #pragma GCC diagnostic warning "-Wunused-function" using namespace std; using namespace atcoder; #define rep(i,n) for(int i = 0; i < (int)(n); i++) #define rrep(i,n) for(int i = (int)(n) - 1; i >= 0; i--) #define all(x) begin(x), end(x) #define rall(x) rbegin(x), rend(x) template bool chmax(T& a, const T& b) { if (a < b) { a = b; return true; } else return false; } template bool chmin(T& a, const T& b) { if (b < a) { a = b; return true; } else return false; } using ll = long long; using P = pair; using VI = vector; using VVI = vector; using VL = vector; using VVL = vector; using mint = modint998244353; struct S { int len; mint v; }; S op(S x, S y) { return {x.len + y.len, x.v + y.v}; } S e() { return {0, mint()}; } mint composition(mint f, mint g) { return f + g; } mint id() { return mint(); } S mapping(mint f, S x) { return {x.len, x.v + x.len * f}; } template struct value_compression : vector { bool built = false; using VS = vector; using VS::VS; value_compression(vector v) : vector(move(v)) {} void build() { sort(this->begin(), this->end()); this->erase(unique(this->begin(), this->end()), this->end()); built = true; } template void convert(T first, T last) { assert(built); for (; first != last; ++first) *first = (*this)(*first); } int operator()(S x) { assert(built); return lower_bound(this->begin(), this->end(), x) - this->begin(); } void clear() { this->clear(); built = false; } }; } int main() { ios::sync_with_stdio(false); cin.tie(0); int n; cin >> n; vector

bc(n); value_compression vc; for(auto& [b, c] : bc) { cin >> b >> c; b--; vc.emplace_back(b); vc.emplace_back(c); } vc.build(); int sz = int(vc.size()) - 1; vector init(sz); rep(i, sz) init[i] = S{vc[i+1] - vc[i], mint()}; lazy_segtree seg(init); mint ans = mint(n) * (n - 1) / 2; for(auto [b, c] : bc) { int l = vc(b), r = vc(c); int len = c - b; mint inv = mint(len).inv(); ans -= seg.prod(l, r).v * inv; seg.apply(l, r, inv); } ans /= 2; rep(i, n) ans *= i + 1; cout << ans.val() << '\n'; }