#include using namespace std; struct iofast_t { iofast_t() { ios::sync_with_stdio(false); cin.tie(nullptr); } } iofast; struct uns_t {} uns; template auto vec(Element init, Head arg, Args ...args) { if constexpr (sizeof...(Args) == 0) return std::vector(arg, init); else return std::vector(arg, vec(init, args...)); } template auto vec(uns_t, Head arg, Args ...args) { return vec(Element(), arg, args...); } template > T &chmin(T &l, T r, Compare &&f = less()) { return l = min(l, r, f); } template > T &chmax(T &l, T r, Compare &&f = less()) { return l = max(l, r, f); } #include using mint = atcoder::modint998244353; int main() { int n; cin >> n; auto acc = vec(uns, n + 1); acc[0] = 0; auto t = vec(uns, n); auto v = vec(uns, n); for (int i = 0; i < n; ++i) { cin >> t[i] >> v[i]; acc[i + 1] = acc[i] + t[i]; } mint ans = 0; for (int i = 0; i < n; ++i) { mint c = t[i] - 1; mint b = acc[i]; mint r = acc[n] - b; mint p = b + 1; mint sub = 0; sub += (c + 1) * (p * p * r + p * r); sub += (c * (c + 1) / 2) * (-p * p + 2 * p * r - p + r); sub += (c * (c + 1) * (2 * c + 1) / 6) * (-2 * p + r - 1); sub += ((c * (c + 1) / 2) * (c * (c + 1) / 2)) * (-1); sub *= v[i]; sub /= 2; ans += sub; } cout << ans.val() << endl; }