MOD = 998244353 """ n = 6 A = [3, 3, 2, 1, 1, 1] ans = 0 for i, a in enumerate(A): ans += a * (n - i) * (i + 1) * (i + 2) // 2 print(ans) """ n = int(input()) tv = [list(map(int, input().split())) for _ in range(n)] n = sum(t for t, _ in tv) % MOD inv2 = pow(2, MOD - 2, MOD) inv6 = pow(6, MOD - 2, MOD) def f(x): if x == -1: return 0 ret = 0 one = x * (x + 1) * inv2 three = one * one % MOD ret -= three ret %= MOD two = x * (x + 1) * (2 * x + 1) * inv6 ret += (n - 3) * two ret %= MOD ret += (3 * n - 2) * one ret %= MOD ret += 2 * n * (x + 1) ret *= inv2 return ret % MOD for i in range(x + 1): ret += (n - i) * (i + 1) * (i + 2) // 2 return ret ans = 0 l = 0 for t, v in tv: ans += v * (f(l + t - 1) - f(l - 1)) ans %= MOD l += t print(ans)