Problem: Consider a road consisting of several rows. Each row is divided into several rectangular tiles, and all tiles in the same row are equal. The first row contains exactly one rectangular tile. Look at the picture below which shows how the tiles are arranged.

The road is constructed as follows: 

  * the first row consists of 1 tile; 
  * then a_1 rows follow; each of these rows contains 1 tile greater than the previous row; 
  * then b_1 rows follow; each of these rows contains 1 tile less than the previous row; 
  * then a_2 rows follow; each of these rows contains 1 tile greater than the previous row; 
  * then b_2 rows follow; each of these rows contains 1 tile less than the previous row; 
  * ... 
  * then a_n rows follow; each of these rows contains 1 tile greater than the previous row; 
  * then b_n rows follow; each of these rows contains 1 tile less than the previous row. 

<image> An example of the road with n = 2, a_1 = 4, b_1 = 2, a_2 = 2, b_2 = 3. Rows are arranged from left to right. 

You start from the only tile in the first row and want to reach the last row (any tile of it). From your current tile, you can move to any tile in the next row which touches your current tile.

Calculate the number of different paths from the first row to the last row. Since it can be large, print it modulo 998244353.

Input

The first line contains one integer n (1 ≤ n ≤ 1000).

Then n lines follow. The i-th of them contains two integers a_i and b_i (1 ≤ a_i, b_i ≤ 10^5; |a_i - b_i| ≤ 5).

Additional constraint on the input: the sequence of a_i and b_i never results in a row with non-positive number of tiles.

Output

Print one integer — the number of paths from the first row to the last row, taken modulo 998244353.

Examples

Input


2
4 2
2 3


Output


850


Input


3
4 1
2 3
3 1


Output


10150


Input


8
328 323
867 868
715 718
721 722
439 435
868 870
834 834
797 796


Output


759099319

Solution (in CPP):

Solution:
#include<cstdio>
#include<algorithm>
using namespace std;
typedef long long ll;
const int N = 1e6 + 50;
const ll mod = 998244353;

int n, ans, cnt = 1;
int jc[N], jc_inv[N];
ll f[N], g[N];

ll qpow(ll a, ll b, int mod){
    ll t = 1;
    while(b){
        if(b & 1)  t = (t *a) % mod;
        b >>= 1;
        a = (a * a) % mod;
    }
    return t;
}

void NTT(int n, ll *a, int opt){
    int i, j = 0, k;
    for(i = 0; i < n; i++){
        if(i > j)  swap(a[i], a[j]);
        for(int l = n >> 1; (j ^= l) < l; l >>= 1);
    }
    for(i = 1; i < n; i <<= 1){
        ll wn = qpow(3, (mod - 1) / (i << 1), mod);
        int m = i << 1;
        for(j = 0; j < n; j += m){
            ll w = 1;
            for(k = 0; k < i; k++, w = (w * wn) % mod){
                ll z = (a[j + i + k] * w) % mod;
                a[i + j + k] =( a[j+ k] - z + mod) % mod;
                a[j + k] = (a[j + k] + z) % mod;
            }
        }
    } 
    if(opt == -1)  reverse(a + 1, a + n);
}

// 注意多次使用的话要初始化，将数组值清零，注意n和m的意义 
// a数组储存最后结果，n,m分别是a,b的项数，数组下标从0开始 
int work(ll *a, int n, ll *b, int m){
	int fn = 1;
    while(fn <= n + m)  fn <<= 1;
    NTT(fn, a, 1);  NTT(fn, b, 1);
    // 这里a数组储存结果，所以是a[i] = (a[i] * b[i]) % mod
    // 如果是算其他形式多项式相乘，如(2 - a[x] * b[x]) * b[x]的多项式相乘
    // 那么要改为a[i] = ((2 - a[i] * b[i]) % mod * b[i] % mod + mod) % mod 
    for(int i = 0; i <= fn; i++)  a[i] = (a[i] * b[i]) % mod;
    // 对储存结果的数组NTT
    NTT(fn, a, -1);
    ll t = qpow(fn, mod - 2, mod);
    for(int i = 0; i < fn; i++)  a[i] = (a[i] * t) % mod;
	return fn;
}

int main(){
    scanf("%d", &n);
    jc[0] = 1;
    for(int i = 1; i < N; ++i)  jc[i] = 1LL * jc[i - 1] * i % mod;
    jc_inv[N - 1] = qpow(jc[N - 1], mod - 2, mod);
    for(int i = N - 2; ~i; --i)  jc_inv[i] = 1LL * jc_inv[i + 1] * (i + 1) % mod;
    f[1] = 1;
    while(n--){
        int a, b, tot = -1;
        scanf("%d%d", &a, &b);
        int max_j = a - b + cnt,  max_k = cnt;
        int p = 1e9;
        for(int i = 1 - max_k; i <= max_j - 1; ++i){
            if(a - i >= 0 && b + i >= 0){
                if(p == 1e9)  p = i;
                g[++tot] = 1LL * jc_inv[a - i] * jc_inv[b + i] % mod;
            	//printf("hh %lld\n", g[tot]);
			}
        }
        int len = work(f, cnt, g, tot);
        //for(int i = 0; i <= cnt + tot; ++i)  printf("hh  %lld\n", f[i]);
        for(int i = 0; i < len; ++i)  f[i] = f[i] * jc[a + b] % mod;
        int tt = -1;
        for(int i = 0; i < len; ++i){
            if(p >= 0 && p <= max_j)  f[++tt] = f[i];
            ++p;  
        }
        for(int i = tt + 1; i < len; ++i)  f[i] = 0;
        f[0] = 0,  cnt = tt;
        //for(int i = 0; i <= cnt; ++i) printf("hh %lld\n", f[i]);
        for(int i = 0; i < len; ++i)  g[i] = 0;
    }
    for(int i = 1; i <= cnt; ++i)  ans = (ans + f[i]) % mod;
    printf("%d", ans);  
    return 0;
}