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 def f(x): if x == -1: return 0 ret = 0 three = x * (x + 1) * pow(2, MOD - 2, MOD) three = pow(three, 2, MOD) ret -= three ret %= MOD two = x * (x + 1) * (2 * x + 1) * pow(6, MOD - 2, MOD) ret += (n - 3) * two ret %= MOD one = x * (x + 1) * pow(2, MOD - 2, MOD) ret += (3 * n - 2) * one ret %= MOD ret += 2 * n * (x + 1) ret *= pow(2, MOD - 2, MOD) 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)