import sys input = sys.stdin.buffer.readline sys.setrecursionlimit(10 ** 7) mod = 998244353 div2 = pow(2, mod-2, mod) div4 = pow(4, mod-2, mod) div6 = pow(6, mod-2, mod) N = int(input()) TV = [] total = 0 for _ in range(N): t, v = map(int, input().split()) t %= mod total += t total %= mod TV.append((t, v)) def F(n): x = (total + 1) * n * (n + 1) % mod y = total * n * (n + 1) * (2 * n + 1) % mod z = n * n * (n + 1) * (n + 1) % mod return (x * div2 % mod + y * div6 % mod - z * div4 % mod) * div2 % mod ans = 0 cnt = 0 for t, v in TV: x = F(cnt + t) - F(cnt) ans += v * x % mod ans %= mod cnt += t cnt %= mod print(ans)