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 Java8 (OpenJDK 1.8.0) (judge verdict: Wrong Answer)


import java.math.BigInteger;
import java.util.Scanner;

public class Main {
	Scanner sc = new Scanner(System.in) ;
	public static void main(String[]args) {
		Main m = new Main();
		m.run();
	}
	
	long MOD = 998244353;
	
	void run() {
		String s = sc.next();
		int n = s.length();
		
		int b[] = new int[n];
		int r[] = new int[n];
		for(int i = 0; i < n; i++) {
			b[i] = s.charAt(i) - '0';
			r[i] = 2 - b[i];
		}
		
		
		int bb[] = new int[2*n+1];
		int rr[] = new int[2*n+1];
		
		for(int i = 0 ; i < n ; i++) {
			bb[i + 1] = bb[i] + b[i];
			rr[i + 1] = rr[i] + r[i];
		}
		for(int i = 0 ; i < n ; i++) {
			bb[i+n+1] = bb[i+n];
			rr[i+n+1] = rr[i+n];
		}
		
		
		long dp[][] = new long[2*n+1][2*n+1];
		dp[0][0] = 1;
		
		for(int i = 0 ; i < 2*n ; i++) {
			for(int rc = 0; rc <= i ; rc++) {
				int bc = i-rc;
				
				if(rr[i+1] >= rc+1) {
					dp[rc+1][bc] += dp[rc][bc];
				}
				if(bb[i+1] >= bc+1) {
					dp[rc][bc+1] += dp[rc][bc];
				}
			}
		}
		System.out.println(dp[rr[n]][bb[n]] % MOD);
		
	}
	
	BigInteger[][] ncr = new BigInteger[2001][2001];

	BigInteger nCr(int n, int r) {
		if(r > n || r < 0) {
			return BigInteger.ZERO;
		}
		if(n == r || r == 0) {
			return BigInteger.ONE;
		}
		if(r == 1 || r == n-1) {
			return BigInteger.valueOf(n);
		}
		if(ncr[n][r] != null) {
			return ncr[n][r];
		}
		
		ncr[n][r] = (nCr(n-1,r).add(nCr(n-1,r-1)));
		return ncr[n][r] ;
	}
}