Buggy python submission (verdict: Time Limit Exceeded):
import sys
sys.setrecursionlimit(10**7)
input = sys.stdin.readline
def main():
    n=int(input())
    a=list(map(int,input().split()))
    # 0個の皿がx0枚、1個の皿がx1枚、2個の皿がx2枚、3個の皿がx3枚
    # x0=n-x1-x2-x3
    ca={1:a.count(1),2:a.count(2),3:a.count(3)}
    memo=[[[-1]*(n+2) for _ in range(n+2)] for _ in range(n+2)]
    memo[0][0][0]=0
    for x3 in range(ca[3]+1):
        for x2 in range(ca[2]+ca[3]+1):
            for x1 in range(ca[1]+ca[2]+ca[3]+1):
                if (x1,x2,x3)==(0,0,0):continue
                tmp=0
                if x1>0:tmp+=memo[x1-1][x2][x3]*x1
                if x2>0:tmp+=memo[x1+1][x2-1][x3]*x2
                if x3>0:tmp+=memo[x1][x2+1][x3-1]*x3
                tmp+=n
                tmp/=x1+x2+x3
                memo[x1][x2][x3]=tmp
    print(memo[ca[1]][ca[2]][ca[3]])
if __name__=='__main__':
    main()

Fixed submission:
import sys
sys.setrecursionlimit(10**7)
input = sys.stdin.readline
def main():
    n=int(input())
    a=list(map(int,input().split()))
    # 0個の皿がx0枚、1個の皿がx1枚、2個の皿がx2枚、3個の皿がx3枚
    # x0=n-x1-x2-x3
    ca={1:a.count(1),2:a.count(2),3:a.count(3)}
    memo=[[[-1]*(n+2) for _ in range(n+2)] for _ in range(n+2)]
    memo[0][0][0]=0
    for x3 in range(ca[3]+1):
        for x2 in range(ca[2]+ca[3]+1-x3):
            for x1 in range(ca[1]+ca[2]+ca[3]+1-x3-x2):
                if (x1,x2,x3)==(0,0,0):continue
                tmp=0
                if x1>0:tmp+=memo[x1-1][x2][x3]*x1
                if x2>0:tmp+=memo[x1+1][x2-1][x3]*x2
                if x3>0:tmp+=memo[x1][x2+1][x3-1]*x3
                tmp+=n
                tmp/=x1+x2+x3
                memo[x1][x2][x3]=tmp
    print(memo[ca[1]][ca[2]][ca[3]])
if __name__=='__main__':
    main()

Buggy python submission (verdict: Time Limit Exceeded):
n=int(input())
a=list(map(int,input().split()))
dp=[[0]*4 for _ in range(n+1)]
# 0個の皿がx0枚、1個の皿がx1枚、2個の皿がx2枚、3個の皿がx3枚
# x0=n-x1-x2-x3
from collections import Counter
a.append(1)
a.append(2)
a.append(3)
ca=dict(Counter(a))
import sys
sys.setrecursionlimit(10**7)
memo={}
memo[(0,0,0)]=0
def func(x1,x2,x3):
    if (x1,x2,x3) in memo:return memo[(x1,x2,x3)]
    ret=0
    s=x1+x2+x3
    if x1>0:ret+=((n/s)+func(x1-1,x2,x3))*(x1/s)
    if x2>0:ret+=((n/s)+func(x1+1,x2-1,x3))*(x2/s)
    if x3>0:ret+=((n/s)+func(x1,x2+1,x3-1))*(x3/s)
    #print(x1,x2,x3,ret)
    memo[(x1,x2,x3)]=ret
    return ret
print(func(ca[1]-1,ca[2]-1,ca[3]-1))


Fixed submission:
import sys
sys.setrecursionlimit(10**7)
input = sys.stdin.readline
def main():
    n=int(input())
    a=list(map(int,input().split()))
    # 0個の皿がx0枚、1個の皿がx1枚、2個の皿がx2枚、3個の皿がx3枚
    # x0=n-x1-x2-x3
    ca={1:a.count(1),2:a.count(2),3:a.count(3)}
    memo=[[[-1]*(n+2) for _ in range(n+2)] for _ in range(n+2)]
    memo[0][0][0]=0
    for x3 in range(ca[3]+1):
        for x2 in range(ca[2]+ca[3]+1-x3):
            for x1 in range(ca[1]+ca[2]+ca[3]+1-x3-x2):
                if (x1,x2,x3)==(0,0,0):continue
                tmp=0
                if x1>0:tmp+=memo[x1-1][x2][x3]*x1
                if x2>0:tmp+=memo[x1+1][x2-1][x3]*x2
                if x3>0:tmp+=memo[x1][x2+1][x3-1]*x3
                tmp+=n
                tmp/=x1+x2+x3
                memo[x1][x2][x3]=tmp
    print(memo[ca[1]][ca[2]][ca[3]])
if __name__=='__main__':
    main()

Buggy python submission (verdict: Time Limit Exceeded):
n=int(input())
a=list(map(int,input().split()))
dp=[[0]*4 for _ in range(n+1)]
# 0個の皿がx0枚、1個の皿がx1枚、2個の皿がx2枚、3個の皿がx3枚
# x0=n-x1-x2-x3
from collections import Counter
a.append(1)
a.append(2)
a.append(3)
ca=dict(Counter(a))
import sys
sys.setrecursionlimit(10**7)
memo={}
memo[(0,0,0)]=0
def func(x1,x2,x3):
    if (x1,x2,x3) in memo:return memo[(x1,x2,x3)]
    ret=0
    s=x1+x2+x3
    if x1>0:ret+=((n/s)+func(x1-1,x2,x3))*(x1/s)
    if x2>0:ret+=((n/s)+func(x1+1,x2-1,x3))*(x2/s)
    if x3>0:ret+=((n/s)+func(x1,x2+1,x3-1))*(x3/s)
    #print(x1,x2,x3,ret)
    memo[(x1,x2,x3)]=ret
    return ret
print(func(ca[1]-1,ca[2]-1,ca[3]-1))


Fixed submission:
import sys
sys.setrecursionlimit(10**7)
input = sys.stdin.readline
def main():
    n=int(input())
    a=list(map(int,input().split()))
    # 0個の皿がx0枚、1個の皿がx1枚、2個の皿がx2枚、3個の皿がx3枚
    # x0=n-x1-x2-x3
    ca={1:a.count(1),2:a.count(2),3:a.count(3)}
    memo=[[[-1]*(n+2) for _ in range(n+2)] for _ in range(n+2)]
    memo[0][0][0]=0
    for x3 in range(ca[3]+1):
        for x2 in range(ca[2]+ca[3]+1-x3):
            for x1 in range(ca[1]+ca[2]+ca[3]+1-x3-x2):
                if (x1,x2,x3)==(0,0,0):continue
                tmp=0
                if x1>0:tmp+=memo[x1-1][x2][x3]*x1
                if x2>0:tmp+=memo[x1+1][x2-1][x3]*x2
                if x3>0:tmp+=memo[x1][x2+1][x3-1]*x3
                tmp+=n
                tmp/=x1+x2+x3
                memo[x1][x2][x3]=tmp
    print(memo[ca[1]][ca[2]][ca[3]])
if __name__=='__main__':
    main()

Buggy python submission (verdict: Time Limit Exceeded):
# 再帰回数の上限を引き上げる
import sys
sys.setrecursionlimit(10**7)
n=int(input())
a=list(map(int,input().split()))
a0,a1,a2,a3=0,0,0,0
for ai in a:
      if ai==0:
            a0+=1
      elif ai==1:
            a1+=1
      elif ai==2:
            a2+=1
      elif ai==3:
            a3+=1
aa=a0+a1+a2+a3
memo=[[[-1]*(aa+1) for _ in range(aa+1)] for _ in range(aa+1)]
#print(memo)
memo[1][0][0]=n
def dp(i,j,k):
      if memo[i][j][k]>0:
            return memo[i][j][k]
      else:
            m=i+j+k
            c=n
            if i>0:
                  c+=i*dp(i-1,j,k)
            if j>0:
                  c+=j*dp(i+1,j-1,k)
            if k>0:
                  c+=k*dp(i,j+1,k-1)
            memo[i][j][k]=c/m
            return c/m
print(dp(a1,a2,a3))


Fixed submission:
import sys
sys.setrecursionlimit(10**7)
input = sys.stdin.readline
def main():
    n=int(input())
    a=list(map(int,input().split()))
    # 0個の皿がx0枚、1個の皿がx1枚、2個の皿がx2枚、3個の皿がx3枚
    # x0=n-x1-x2-x3
    ca={1:a.count(1),2:a.count(2),3:a.count(3)}
    memo=[[[-1]*(n+2) for _ in range(n+2)] for _ in range(n+2)]
    memo[0][0][0]=0
    for x3 in range(ca[3]+1):
        for x2 in range(ca[2]+ca[3]+1-x3):
            for x1 in range(ca[1]+ca[2]+ca[3]+1-x3-x2):
                if (x1,x2,x3)==(0,0,0):continue
                tmp=0
                if x1>0:tmp+=memo[x1-1][x2][x3]*x1
                if x2>0:tmp+=memo[x1+1][x2-1][x3]*x2
                if x3>0:tmp+=memo[x1][x2+1][x3-1]*x3
                tmp+=n
                tmp/=x1+x2+x3
                memo[x1][x2][x3]=tmp
    print(memo[ca[1]][ca[2]][ca[3]])
if __name__=='__main__':
    main()

Buggy python submission (verdict: Time Limit Exceeded):
# 再帰回数の上限を引き上げる
import sys
sys.setrecursionlimit(10**7)
n=int(input())
a=list(map(int,input().split()))
a0,a1,a2,a3=0,0,0,0
for ai in a:
      if ai==0:
            a0+=1
      elif ai==1:
            a1+=1
      elif ai==2:
            a2+=1
      elif ai==3:
            a3+=1
aa=a0+a1+a2+a3
memo=[[[-1]*(aa+1) for _ in range(aa+1)] for _ in range(aa+1)]
#print(memo)
memo[1][0][0]=n
def dp(i,j,k):
      if memo[i][j][k]>0:
            return memo[i][j][k]
      else:
            m=i+j+k
            c=n
            if i>0:
                  c+=i*dp(i-1,j,k)
            if j>0:
                  c+=j*dp(i+1,j-1,k)
            if k>0:
                  c+=k*dp(i,j+1,k-1)
            memo[i][j][k]=c/m
            return c/m
print(dp(a1,a2,a3))


Fixed submission:
import sys
sys.setrecursionlimit(10**7)
input = sys.stdin.readline
def main():
    n=int(input())
    a=list(map(int,input().split()))
    # 0個の皿がx0枚、1個の皿がx1枚、2個の皿がx2枚、3個の皿がx3枚
    # x0=n-x1-x2-x3
    ca={1:a.count(1),2:a.count(2),3:a.count(3)}
    memo=[[[-1]*(n+2) for _ in range(n+2)] for _ in range(n+2)]
    memo[0][0][0]=0
    for x3 in range(ca[3]+1):
        for x2 in range(ca[2]+ca[3]+1-x3):
            for x1 in range(ca[1]+ca[2]+ca[3]+1-x3-x2):
                if (x1,x2,x3)==(0,0,0):continue
                tmp=0
                if x1>0:tmp+=memo[x1-1][x2][x3]*x1
                if x2>0:tmp+=memo[x1+1][x2-1][x3]*x2
                if x3>0:tmp+=memo[x1][x2+1][x3-1]*x3
                tmp+=n
                tmp/=x1+x2+x3
                memo[x1][x2][x3]=tmp
    print(memo[ca[1]][ca[2]][ca[3]])
if __name__=='__main__':
    main()