#include #include using mint = atcoder::modint998244353; using ll = long long; using namespace std; template struct SegmentTree { SegmentTree(int n) : n(n) { for (m = 2; m < n; m *= 2); t = vector(m + n + n % 2); } T &operator[](int i) { return t[m + i]; } const T &root() const { return t[1]; } void build() { for (int i = (n - 1 + m) / 2; i > 0; i--) t[i] = t[i * 2] * t[i * 2 + 1]; } void update(int i, const T &o) { for (t[i += m] = o; i /= 2, i > 0;) t[i] = t[i * 2] * t[i * 2 + 1]; } T query(int l, int r) { T o0, o1; for (l += m, r += m; l < r; l /= 2, r /= 2) { if (l & 1) o0 = o0 * t[l++]; if (r & 1) o1 = t[--r] * o1; } return o0 * o1; } vector t; int n, m; }; struct Sum { Sum() : x(0), y(0) {} Sum(mint x, mint y) : x(x), y(y) {} Sum operator*(const Sum &o) const { return { x + o.x, y + o.y }; } mint x, y; }; struct P { bool operator<(const P &p) const { return b < p.b; } int b, c; }; int main() { ios::sync_with_stdio(false); cin.tie(0); int n; cin >> n; vector

p(n); for (int i = 0; i < n; i++) { int b, c; cin >> b >> c; b--; p[i] = { b, c }; } sort(p.begin(), p.end()); vector y(n); for (int i = 0; i < n; i++) { y[i] = mint(p[i].c - p[i].b).inv(); } vector u(n); for (int i = 0; i < n; i++) { u[i] = i; } sort(u.begin(), u.end(), [&](const int &i0, const int &i1) { return p[i0].c < p[i1].c; }); SegmentTree st(n); vector sb(n + 1, 0), sy(n + 1, 0); for (int i = 0; i < n; i++) { sb[i + 1] = sb[i] + y[i] * p[i].b; } for (int i = 0; i < n; i++) { sy[i + 1] = sy[i] + y[i]; } mint x = 0; for (int j = 0; j < n; j++) { int i = u[j]; int i0 = i + 1; int i1 = lower_bound(p.begin(), p.end(), P{ p[i].c, 0 }) - p.begin(); auto o = st.query(i0, i1); mint t = 0; t += o.x; t += p[i].c * (sy[i1] - sy[i0] - o.y); t -= sb[i1] - sb[i0]; st.update(i, { (mint)p[i].c * y[i], y[i] }); //for (int j = i0; j < i1; j++) { // int l = min(p[i].c, p[j].c) - p[j].b; // t += (mint)l / (p[j].c - p[j].b); //} x += t * y[i]; } x = (mint)n * (n - 1) / 2 - x; x /= 2; for (int i = 1; i <= n; i++) { x *= i; } cout << x.val() << endl; return 0; }