Problem:
Score : 1600 points

Problem Statement

Snuke found a random number generator.
It generates an integer between 0 and N-1 (inclusive).
An integer sequence A_0, A_1, \cdots, A_{N-1} represents the probability that each of these integers is generated. The integer i (0 \leq i \leq N-1) is generated with probability A_i / S, where S = \sum_{i=0}^{N-1} A_i. The process of generating an integer is done independently each time the generator is executed.

Now, Snuke will repeatedly generate an integer with this generator until the following condition is satisfied:

For every i (0 \leq i \leq N-1), the integer i has been generated at least B_i times so far.

Find the expected number of times Snuke will generate an integer, and print it modulo 998244353.
More formally, represent the expected number of generations as an irreducible fraction P/Q. Then, there exists a unique integer R such that R \times Q \equiv P \pmod{998244353},\ 0 \leq R < 998244353, so print this R.

From the constraints of this problem, we can prove that the expected number of generations is a finite rational number, and its integer representation modulo 998244353 can be defined.

Constraints

1 \leq N \leq 400

1 \leq A_i

\sum_{i=0}^{N-1} A_i \leq 400

1 \leq B_i

\sum_{i=0}^{N-1} B_i \leq 400

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
A_0 B_0
A_1 B_1
\vdots
A_{N-1} B_{N-1}

Output

Print the expected number of times Snuke will generate an integer, modulo 998244353.

Sample Input 1

2
1 1
1 1

Sample Output 1

3

The expected number of times Snuke will generate an integer is 3.

Sample Input 2

3
1 3
2 2
3 1

Sample Output 2

971485877

The expected number of times Snuke will generate an integer is 132929/7200.

Sample Input 3

15
29 3
78 69
19 15
82 14
9 120
14 51
3 7
6 14
28 4
13 12
1 5
32 30
49 24
35 23
2 9

Sample Output 3

371626143

Solution in C++14 (GCC 5.4.1) (judge verdict: Accepted)

#include<bits/stdc++.h>
#define fi first
#define se second
#define pb push_back
#define SZ(x) ((int)x.size())
#define L(i,u) for (register int i=head[u]; i; i=nxt[i])
#define rep(i,a,b) for (register int i=(a); i<=(b); i++)
#define per(i,a,b) for (register int i=(a); i>=(b); i--)
using namespace std;
typedef long long ll;
typedef unsigned int ui;
typedef pair<int,int> Pii;
typedef vector<int> Vi;
template<class T> inline void read(T &x){
	x=0; char c=getchar(); int f=1;
	while (!isdigit(c)) {if (c=='-') f=-1; c=getchar();}
	while (isdigit(c)) {x=x*10+c-'0'; c=getchar();} x*=f;
}
template<class T> inline void umin(T &x, T y){x=x<y?x:y;}
template<class T> inline void umax(T &x, T y){x=x>y?x:y;}
inline ui R() {
	static ui seed=416;
	return seed^=seed>>5,seed^=seed<<17,seed^=seed>>13;
}
const int N = 404,mo=998244353,lim=400;
int n,a[N],b[N],dp[N][N],res;
inline void add(int &x, int y){x=x+y<mo?x+y:x+y-mo;}
inline int power(int a, int n) {
	int res=1;
	while (n) {
		if (n&1) res=1LL*res*a%mo;
		a=1LL*a*a%mo; n>>=1;
	}
	return res;
}
int fac[N],ifac[N];
void iniC(int n){
	fac[0]=1;rep(i,1,n)fac[i]=1LL*fac[i-1]*i%mo;
	ifac[n]=power(fac[n],mo-2);
	per(i,n-1,0)ifac[i]=1LL*ifac[i+1]*(i+1)%mo;
}
inline int C(int n, int m){return m>=0&&m<=n?1LL*fac[n]*ifac[m]%mo*ifac[n-m]%mo:0;}

int main() {
	read(n);int s=0;rep(i,1,n)read(a[i]),read(b[i]),s+=a[i];
	dp[0][0]=1;int nis=power(s,mo-2);iniC(N-1);
	rep(i,1,n){
		static int dp1[N][N];memset(dp1,0,sizeof(dp1));
		static int xs[N];
		rep(j,0,b[i]-1)xs[j]=mo-1LL*power(1LL*a[i]*nis%mo,j)*ifac[j]%mo;
		rep(p,0,lim)rep(q,0,lim)if(dp[p][q]){
			dp1[p][q+a[i]]=(dp1[p][q+a[i]]+dp[p][q])%mo;
			rep(j,0,b[i]-1)
				dp1[p+j][q]=(dp1[p+j][q]+1LL*dp[p][q]*xs[j])%mo;
		}
		memcpy(dp,dp1,sizeof(dp1));
	}
	rep(i,0,lim)rep(j,0,lim)dp[i][j]=(mo-dp[i][j])%mo;
	rep(p,0,lim)if(dp[p][0])res=(res+1LL*fac[p]*dp[p][0])%mo;
	rep(q,1,s-1){
		static int f[N];f[0]=1LL*s*power(s-q,mo-2)%mo;
		int Q=1LL*q*nis%mo;
		rep(i,1,lim)f[i]=1LL*i*Q%mo*f[i-1]%mo*power(1-Q+mo,mo-2)%mo;
		rep(p,0,lim)if(dp[p][q])
			res=(res+1LL*power(Q,mo-1-p)*f[p]%mo*dp[p][q])%mo;//,cerr<<p<<' '<<q<<endl;
	}
	cout<<res;
	return 0;
}