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 (Python):
s=input()
N=len(s)
table=[]
mod=998244353

if s[0]=="0":
  table.append((2,0))
elif s[0]=="1":
  table.append((1,1))
elif s[0]=="2":
  table.append((0,2))
for i in range(1,N):
  r,b=table[-1]
  if s[i]=="0":
    r,b=table[-1]
    table.append((r+2,b))
  elif s[i]=="1":
    r,b=table[-1]
    table.append((r+1,b+1))
  elif s[i]=="2":
    r,b=table[-1]
    table.append((r,b+2))
for i in range(N):
  table.append(table[-1])
dp=[[0]*(2*N+1) for i in range(2*N+1)]
dp[0][0]=1
for i in range(2*N):
  for k in range(2*N):
    if k+1<=table[i][0]:
      dp[i+1][k+1]=(dp[i+1][k+1]+dp[i][k])%mod
    if i+1-k<=table[i][1]:
      dp[i+1][k]=(dp[i+1][k]+dp[i][k])%mod
ans=0
for k in range(2*N+1):
  ans=(ans+dp[2*N][k])%mod
print(ans)

Solution (Python):
m = 998244353
S = map(int, raw_input())
N = len(S)
pr = [0]*(N*2)
pb = [0]*(N*2)
pr[0] = 2-S[0]
pb[0] = S[0]
for i in xrange(1, N):
  pr[i] = pr[i-1]+(2-S[i])
  pb[i] = pb[i-1]+S[i]
for i in xrange(N, N*2):
  pr[i] = pr[i-1]
  pb[i] = pb[i-1]
dp = []
for i in xrange(N*2+1):
  dp.append([0]*(N*2+1))
dp[0][0]=1
for i in xrange(1, N*2+1):
  si = 0
  for b in xrange(i+1):
    r = i-b
    if pr[i-1]<r or pb[i-1]<b:
      continue
    if r>0:
      dp[r][b] = (dp[r][b]+dp[r-1][b]) % m
    if b>0:
      dp[r][b] = (dp[r][b]+dp[r][b-1]) % m
    si = (si+dp[r][b]) % m
print si

Solution (Python):


S = input()
N = len(S)
MOD = 998244353

red = []
blue = []

for i in range(N):
    if S[i] == "0": 
        red.append(i)
        red.append(i)
    if S[i] == "1": 
        red.append(i)
        blue.append(i)
    if S[i] == "2": 
        blue.append(i)
        blue.append(i)

DP = [ [0]*4020 for _ in range(4020)] 
                                           
                                                        
DP[0][0] = 1                          

rsize = len(red)
bsize = len(blue)















for i in range(rsize+1):
    for j in range(bsize+1):

        DP[i][j] %= MOD

        m = i+j

        if j < bsize and blue[j] <= m:
            DP[i][j+1] += DP[i][j]

        if i < rsize and red[i] <= m:
            DP[i+1][j] += DP[i][j]


print(DP[rsize][bsize] % MOD)

Solution (Python):
MOD = 998244353
s = input()
n = len(s)
R = s.count("0") * 2 + s.count("1")
B = 2*n - R
rb = [[R, B] for _ in range(2*n)]
if s[0] == "0":
	rb[0] = [2, 0]
elif s[0] == "1":
	rb[0] = [1, 1]
else:
	rb[0] = [0, 2]
for i in range(1, n):
	if s[i] == "0":
		rb[i] = [rb[i-1][0] + 2, rb[i-1][1] + 0]
	elif s[i] == "1":
		rb[i] = [rb[i-1][0] + 1, rb[i-1][1] + 1]
	else:
		rb[i] = [rb[i-1][0] + 0, rb[i-1][1] + 2]

dp = [[0 for _ in range(R+1)] for _ in range(2*n)]
if rb[0][0] >= 1:
	dp[0][1] = 1
if rb[0][1] >= 1:
	dp[0][0] = 1
for i in range(1, 2*n):
	for j in range(R+1):
		if j > 0 and rb[i][0] >= j:
			dp[i][j] += dp[i-1][j-1]
			dp[i][j] %= MOD
		if j != i+1 and rb[i][1] >= i+1 - j:
			dp[i][j] += dp[i-1][j]
			dp[i][j] %= MOD

print(dp[2*n-1][R])

Solution (Python):
S = input()
N = len(S)
MOD = 998244353

dp = [[0]*(N+1) for i in range(N+1)] 
if S[0] == '0':
    dp[0][1] = 1
elif S[0] == '1':
    dp[1][0] = 1
    dp[0][1] = 1
else:
    dp[1][0] = 1

for k in range(1,N):
    c = int(S[k])
    for r in range(k+1):
        b = k-r
        if c == 1:
            dp[r][b+1] += dp[r][b]
            dp[r][b+1] %= MOD
            dp[r+1][b] += dp[r][b]
            dp[r+1][b] %= MOD
        elif c == 0:
            dp[r][b+1] += dp[r][b]
            dp[r][b+1] %= MOD
            if r > 0:
                dp[r-1][b+2] += dp[r][b]
                dp[r-1][b+2] %= MOD
        else:
            if b > 0:
                dp[r+2][b-1] += dp[r][b]
                dp[r+2][b-1] %= MOD
            dp[r+1][b] += dp[r][b]
            dp[r+1][b] %= MOD

M = N+5
fac = [1,1] + [0]*M
finv = [1,1] + [0]*M
inv = [0,1] + [0]*M
for i in range(2,M+2):
    fac[i] = fac[i-1] * i % MOD
    inv[i] = -inv[MOD%i] * (MOD // i) % MOD
    finv[i] = finv[i-1] * inv[i] % MOD

def ncr(n,r):
    if n < r: return 0
    if n < 0 or r < 0: return 0
    return fac[n] * (finv[r] * finv[n-r] % MOD) % MOD

ans = 0
for k in range(N+1):
    ans += ncr(N,k) * dp[k][N-k]

print(ans % MOD)

Solution (Python):
s = input()
N = len(s)
P = 998244353
B = 0
R = 0

Y = [[0] * (2*N+1) for _ in range(2*N+1)] 

for i in range(N):
    if s[i] == "0":
        R += 2
    elif s[i] == "1":
        B += 1
        R += 1
    else:
        B += 2
        
    
    if R < i+1:
        for j in range(i+1-R):
            Y[R+1][j] = 1
    
    if B < i+1:
        for j in range(i+1-B):
            Y[j][B+1] = 1

X = [[0] * (B+1) for _ in range(R+1)]

for i in range(R+1):
    for j in range(B+1):
        if i == 0 and j == 0:
            X[i][j] = 1
            continue
        
        if Y[i][j]:
            continue
            
        if j > 0:
            X[i][j] += X[i][j-1]
        
        if i > 0:
            X[i][j] += X[i-1][j]
            
        X[i][j] %= P

print(X[-1][-1])