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>
using namespace std;
typedef long long ll;
#define all(x) (x).begin(),(x).end()
const int mod=998244353,MAX=4005,INF=1<<30;
ll dp[MAX][MAX];

ll inv[MAX],fac[MAX],finv[MAX];

void make(){
    
    fac[0]=fac[1]=1;
    finv[0]=finv[1]=1;
    inv[1]=1;
    
    for(int i=2;i<MAX;i++){
        inv[i]=mod-inv[mod%i]*(mod/i)%mod;
        fac[i]=fac[i-1]*(ll)i%mod;
        finv[i]=finv[i-1]*inv[i]%mod;
    }
    
}

ll comb(ll a,ll b){
    if(a<b||b<0) return 0;
    return fac[a]*(finv[b]*finv[a-b]%mod)%mod;
}

int main(){
    
    std::ifstream in("text.txt");
    std::cin.rdbuf(in.rdbuf());
    cin.tie(0);
    ios::sync_with_stdio(false);
    
    make();
    
    string S;cin>>S;
    int N=S.size();
    int cana=0,canb=0;
    dp[0][0]=1;
    if(S[0]=='0'){
        dp[1][1]=1;
        cana=2;
    }else if(S[0]=='1'){
        dp[1][1]=1;
        dp[1][0]=1;
        cana=1;
        canb=1;
    }else{
        dp[1][0]=1;
        canb=2;
    }
    
    for(int i=1;i<N;i++){
        if(S[i]=='0') cana++;
        else if(S[i]=='1'){
            cana++;
            canb++;
        }else canb++;
        
        for(int j=0;j<=i+1;j++){
            if(canb>=i+1-j) dp[i+1][j]+=dp[i][j];
            if(cana>=j&&j) dp[i+1][j]+=dp[i][j-1];
            
            dp[i+1][j]%=mod;
        }
        
        if(S[i]=='0') cana++;
        if(S[i]=='2') canb++;
    }
    
    ll ans=0;
    
    for(int j=0;j<=N;j++){
        ans+=dp[N][j]*comb(N,cana-j);
        ans%=mod;
    }
    
    cout<<ans<<endl;
    
}