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 ll long long
#define p 998244353
using namespace std;
int a[1010],b[1010];
int jc[1010],njc[1010],inv[1010];
vector<int>ret,E[1010],all[1010];
vector<int>mul(vector<int>x,vector<int>y){
	vector<int>ret;
	int n=x.size(),m=y.size();
	ret.resize(n+m-1);
	for(int i=0;i<n;i++)
	for(int j=0;j<m;j++)
	(ret[i+j]+=1ll*x[i]*y[j]%p)%=p;return ret;
}
void del(vector<int>&x,vector<int>y){
	int n=x.size(),m=y.size();
	x.resize(max(n,m));
	for(int i=0;i<m;i++)(x[i]-=y[i])%=p; 
}
void add(vector<int>&x,vector<int>y){
	int n=x.size(),m=y.size();
	x.resize(max(n,m));
	for(int i=0;i<m;i++)(x[i]+=y[i])%=p; 
}
int main(){
	int n;scanf("%d",&n);
	for(int i=0;i<2;i++)jc[i]=njc[i]=inv[i]=1;
	for(int i=2;i<=405;i++){
		jc[i]=1ll*jc[i-1]*i%p;
		inv[i]=1ll*inv[p%i]*(p-p/i)%p;
		njc[i]=1ll*njc[i-1]*inv[i]%p;
	}
	for(int i=0;i<n;i++)scanf("%d %d",&a[i],&b[i]); 
	int S=0,ans=0;
	for(int i=0;i<n;i++)S+=a[i];
	for(int i=0;i<n;i++){
		for(int k=0,P=1;k<b[i];k++,P=1ll*P*a[i]%p)E[i].push_back(1ll*P*njc[k]%p);
	}
	for(int i=0;i<n;i++){
		for(int k=S-a[i];k>=1;k--)if(all[k].size())
		del(all[a[i]+k],mul(E[i],all[k]));
		add(all[a[i]],E[i]);
	}
	for(int i=1;i<=S;i++){
		for(int j=0,u=inv[i];j<all[i].size();j++,u=1ll*u*inv[i]%p)
		(ans+=1ll*all[i][j]*u%p*jc[j]%p)%=p;
	}
	(ans+=p)%=p;
	cout<<1ll*ans*S%p;
	return 0;
}