#include #include using namespace std; using namespace atcoder; //using mint = modint1000000007; //const int mod = 1000000007; using mint = modint998244353; const int mod = 998244353; //const int INF = 1e9; //const long long LINF = 1e18; #define rep(i, n) for (int i = 0; i < (n); ++i) #define rep2(i,l,r)for(int i=(l);i<(r);++i) #define rrep(i, n) for (int i = (n) - 1; i >= 0; --i) #define rrep2(i,l,r)for(int i=(r) - 1;i>=(l);--i) #define all(x) (x).begin(),(x).end() #define allR(x) (x).rbegin(),(x).rend() #define P pair template inline bool chmax(A & a, const B & b) { if (a < b) { a = b; return true; } return false; } template inline bool chmin(A & a, const B & b) { if (a > b) { a = b; return true; } return false; } template std::pair, int> compression(std::vectororg) { auto copy = org; std::sort(copy.begin(), copy.end()); copy.erase(unique(copy.begin(), copy.end()), copy.end()); for (auto &e : org) { e = std::lower_bound(copy.begin(), copy.end(), e) - copy.begin(); } return { org,copy.size() }; } void invdebug(int x, int y = 1) { mint m = x; m /= y; cout << x << "/" << y << "=" << m.val() << endl; } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int n; cin >> n; vectorl(n), r(n); rep(i, n) { cin >> l[i] >> r[i]; r[i]++; } vectorc; rep(i, n) { c.push_back(l[i]); c.push_back(r[i]); } auto compc = compression(c); mapmp, rmp; rep(i, c.size())mp[compc.first[i]] = c[i]; rep(i, c.size())rmp[c[i]] = compc.first[i]; int sz = compc.second; vectorimos(sz), simos(sz + 1); rep2(i, 1, n) { int x = rmp[l[i]]; int y = rmp[r[i]]; mint inv = mint::raw(r[i] - l[i]).inv(); imos[x] += inv; imos[y] -= inv; } rep(i, sz - 1)imos[i + 1] += imos[i]; rep(i, sz - 1)imos[i] *= mp[i + 1] - mp[i]; rep(i, sz)simos[i + 1] += simos[i] + imos[i]; int st = rmp[l[0]]; int ed = rmp[r[0]]; mint ans = 1; mint inv = mint::raw(r[0] - l[0]).inv(); rep2(i, st, ed) { { mint p = simos[sz] - simos[i + 1]; mint q = inv * (mp[i + 1] - mp[i]); ans += p * q; } { int w = mp[i + 1] - mp[i]; mint p = simos[i + 1] - simos[i]; p /= w; mint q = inv; mint r = mint::raw(w)*(w - 1) / 2; //cout << p.val() << endl; //cout << q.val() << endl; //cout << r.val() << endl; ans += p * q * r; } } cout << ans.val() << endl; //invdebug(1, 10); //invdebug(3, 10); //invdebug(45, 1); return 0; }