Problem:
Score : 900 points

Problem Statement

There are N Snukes lining up in a row.
You are given a string S of length N. The i-th Snuke from the front has two red balls if the i-th character in S is 0; one red ball and one blue ball if the i-th character in S is 1; two blue balls if the i-th character in S is 2.

Takahashi has a sequence that is initially empty. Find the number of the possible sequences he may have after repeating the following procedure 2N times, modulo 998244353:

Each Snuke who has one or more balls simultaneously chooses one of his balls and hand it to the Snuke in front of him, or hand it to Takahashi if he is the first Snuke in the row.

Takahashi receives the ball and put it to the end of his sequence.

Constraints

1 \leq |S| \leq 2000

S consists of 0,1 and 2.

Note that the integer N is not directly given in input; it is given indirectly as the length of the string S.

Input

Input is given from Standard Input in the following format:

S

Output

Print the number of the possible sequences Takahashi may have after repeating the procedure 2N times, modulo 998244353.

Sample Input 1

02

Sample Output 1

3

There are three sequences that Takahashi may have: rrbb, rbrb and rbbr, where r and b stand for red and blue balls, respectively.

Sample Input 2

1210

Sample Output 2

55

Sample Input 3

12001021211100201020

Sample Output 3

543589959

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

#include <bits/stdc++.h>
#define pb push_back
#define int long long
#define mp make_pair
#define inf 998244353
#define LINF 1000000000000000007LL
#define ll long long
using namespace std;
int dp[4010][4010];
int ka[214514],inv[214514];
void setkaijo(int n){
	ka[0]=1;
	for(int i=1;i<=n;i++){
		ka[i] = ka[i-1] * i;
		ka[i] %= inf;
	}
}
int modpow(int x,int k){
	// x ^ k mod inf
	int p2[55],res=1;
	p2[0]=1;p2[1]=x%inf;
	for(int i=2;i<=50;i++){
		p2[i]=p2[i-1]*p2[i-1];
		p2[i] %= inf;
	}
	for(int i=1;i<=50;i++){
		if(k%2==1){
			res *= p2[i];
			res %= inf;
		}
		k /= 2;
	}
	return res;
}
void setinv(int n){
	for(int i=0;i<=n;i++){
		inv[i] = modpow(ka[i],(ll)inf-2);
	}
}
int comb(int n,int k){
	if( k<0 || n<0 || k>n)return 0;
	if(k==0 || k==n)return 1;
	int res = (ka[n] * inv[k]) % inf;
	res = (res*inv[n-k]) % inf;
	return res;
}
signed main(){
	string st;
	cin>>st;
	int n = st.size();
	setkaijo(10000);
	setinv(10000);
	dp[0][0] = 1;
	int rc = 0, bc = 0;
	for(int i=1;i<=n;i++){
		char cur = st[i-1];
		if( cur == '2'){
			rc += 2;
		}
		else if( cur == '1' ){
			rc++;
			bc++;
		}
		else{
			bc += 2;
		}
		for(int j=0;j<=2000;j++){
			if( j <= n && j < rc ){
				dp[i][j+1] += dp[i-1][j];
				dp[i][j+1] %= inf;
			}
			if( (i-1-j) >= 0 && (i-1-j) < bc){
				dp[i][j] += dp[i-1][j];
				dp[i][j] %= inf;
			}
		}
	}
	//for(int i=0;i<=n;i++){for(int j=0;j<=i;j++){cout<<"dp"<<i<<j<<" "<<dp[i][j]<<endl;}}
	int ans = 0;
	for(int i=0;i<=n;i++){
		ans += dp[n][i] * comb(n,rc-i);
		ans %= inf;
	}
	cout<<ans<<endl;
	return 0;
}