Buggy python submission (verdict: Time Limit Exceeded):
def f(d):
    try:
        return stored[(d[0],d[1],d[2])]
    except:
        if d[1]<0 or d[2]<0 or d[0]<0:
            return 0
        pass
    ans = n/(d[0]+d[1]+d[2]) + (d[2]/(d[0]+d[1]+d[2]))*f([d[0],d[1]+1,d[2]-1]) + (d[1]/(d[0]+d[1]+d[2]))*f([d[0]+1,d[1]-1,d[2]]) + (d[0]/(d[0]+d[1]+d[2]))*f([d[0]-1,d[1],d[2]])
    stored[(d[0],d[1],d[2])] = ans
    return ans
 
 
n = int(input())
a = list(map(int,input().split()))
d = [0 for i in range(3)]
stored = {(0,0,0):0}
for i in a:
    d[i-1] +=1
print(f(d))

Fixed submission:
n = int(input())
a = list(map(int,input().split()))
d = [0 for i in range(3)]
dp = [[[0 for i in range(n+1)] for j in range(n+1)] for k in range(n+1)] 
for i in a:
    d[i-1] +=1
for k in range(n+1):
    for j in range(n+1):
        for i in range(n+1):
            if i+j+k==0 or i+j+k>n:
                continue
            ans = n/(i+j+k)
            x = i/(i+j+k)
            if x:
                ans += x*dp[k][j][i-1]
            x = j/(i+j+k)
            if x:
                ans += x*dp[k][j-1][i+1]
            x = k/(i+j+k)
            if x:
                ans += x*dp[k-1][j+1][i]
            dp[k][j][i] = ans
print(dp[d[2]][d[1]][d[0]])

Buggy python submission (verdict: Runtime Error):
from sys import stdin, setrecursionlimit as srl
from threading import stack_size, Thread
srl(int(1e9)+7)
stack_size(int(1e8))
 
def f(d):
    ans = 0
    try:
        return stored[(d[0],d[1],d[2])]
    except:
        pass
    ans = ans + n/(d[0]+d[1]+d[2]) + (d[2]/(d[0]+d[1]+d[2]))*x*f([d[0],d[1]+1,d[2]-1]) + (d[1]/(d[0]+d[1]+d[2]))*x*f([d[0]+1,d[1]-1,d[2]]) + (d[0]/(d[0]+d[1]+d[2]))*f([d[0]-1,d[1],d[2]])
    stored[(d[0],d[1],d[2])] = ans
    return ans
 
 
n = int(input())
a = list(map(int,input().split()))
d = [0 for i in range(3)]
stored = {(0,0,0):0}
 
for i in a:
    d[i-1] +=1
 
print(f(d))

Fixed submission:
n = int(input())
a = list(map(int,input().split()))
d = [0 for i in range(3)]
dp = [[[0 for i in range(n+1)] for j in range(n+1)] for k in range(n+1)] 
for i in a:
    d[i-1] +=1
for k in range(n+1):
    for j in range(n+1):
        for i in range(n+1):
            if i+j+k==0 or i+j+k>n:
                continue
            ans = n/(i+j+k)
            x = i/(i+j+k)
            if x:
                ans += x*dp[k][j][i-1]
            x = j/(i+j+k)
            if x:
                ans += x*dp[k][j-1][i+1]
            x = k/(i+j+k)
            if x:
                ans += x*dp[k-1][j+1][i]
            dp[k][j][i] = ans
print(dp[d[2]][d[1]][d[0]])

Buggy python submission (verdict: Time Limit Exceeded):
from sys import stdin, setrecursionlimit as srl
from threading import stack_size, Thread
srl(int(1e9)+7)
stack_size(int(1e8))
 
def f(d):
    ans = 0
    if sum(d)==0:
        return 0
    try:
        return stored[(d[0],d[1],d[2])]
    except:
        pass
    ans += n/(d[0]+d[1]+d[2])
    x = d[0]/(d[0]+d[1]+d[2])
    ans += x*f([d[0]-1,d[1],d[2]])
    x = d[1]/(d[0]+d[1]+d[2])
    ans += x*f([d[0]+1,d[1]-1,d[2]])
    x = d[2]/(d[0]+d[1]+d[2])
    ans += x*f([d[0],d[1]+1,d[2]-1])
    stored[(d[0],d[1],d[2])] = ans
    return ans
 
 
n = int(input())
a = list(map(int,input().split()))
d = [0 for i in range(3)]
stored = {}
 
for i in a:
    d[i-1] +=1
 
print(f(d))

Fixed submission:
n = int(input())
a = list(map(int,input().split()))
d = [0 for i in range(3)]
dp = [[[0 for i in range(n+1)] for j in range(n+1)] for k in range(n+1)] 
for i in a:
    d[i-1] +=1
for k in range(n+1):
    for j in range(n+1):
        for i in range(n+1):
            if i+j+k==0 or i+j+k>n:
                continue
            ans = n/(i+j+k)
            x = i/(i+j+k)
            if x:
                ans += x*dp[k][j][i-1]
            x = j/(i+j+k)
            if x:
                ans += x*dp[k][j-1][i+1]
            x = k/(i+j+k)
            if x:
                ans += x*dp[k-1][j+1][i]
            dp[k][j][i] = ans
print(dp[d[2]][d[1]][d[0]])

Buggy python submission (verdict: Time Limit Exceeded):
from sys import stdin, setrecursionlimit as srl
from threading import stack_size, Thread
srl(int(1e9)+7)
stack_size(int(1e8))

def f(d):
    ans = 0
    if sum(d)==0:
        return 0
    try:
        return stored[(d[0],d[1],d[2])]
    except:
        pass
    ans += n/(d[0]+d[1]+d[2])
    if d[0]:
        x = d[0]/(d[0]+d[1]+d[2])
        ans += x*f([d[0]-1,d[1],d[2]])
    if d[1]:
        x = d[1]/(d[0]+d[1]+d[2])
        ans += x*f([d[0]+1,d[1]-1,d[2]])
    if d[2]:
        x = d[2]/(d[0]+d[1]+d[2])
        ans += x*f([d[0],d[1]+1,d[2]-1])
    stored[(d[0],d[1],d[2])] = ans
    return ans
 
 
n = int(input())
a = list(map(int,input().split()))
d = [0 for i in range(3)]
stored = {}
 
for i in a:
    d[i-1] +=1

print(f(d))

Fixed submission:
n = int(input())
a = list(map(int,input().split()))
d = [0 for i in range(3)]
dp = [[[0 for i in range(n+1)] for j in range(n+1)] for k in range(n+1)] 
for i in a:
    d[i-1] +=1
for k in range(n+1):
    for j in range(n+1):
        for i in range(n+1):
            if i+j+k==0 or i+j+k>n:
                continue
            ans = n/(i+j+k)
            x = i/(i+j+k)
            if x:
                ans += x*dp[k][j][i-1]
            x = j/(i+j+k)
            if x:
                ans += x*dp[k][j-1][i+1]
            x = k/(i+j+k)
            if x:
                ans += x*dp[k-1][j+1][i]
            dp[k][j][i] = ans
print(dp[d[2]][d[1]][d[0]])

Buggy python submission (verdict: Time Limit Exceeded):
def f(d):
    ans = 0
    if d[1]==0 and d[2]==0 and d[3]==0:
        return ans
    try:
        return stored[(d[1],d[2],d[3])]
    except:
        pass
    ans += (d[0]+d[1]+d[2]+d[3])/(d[1]+d[2]+d[3])
    if d[1]:
        x = d[1]/(d[1]+d[2]+d[3])
        ans += x*f([d[0]+1,d[1]-1,d[2],d[3]])
    if d[2]:
        x = d[2]/(d[1]+d[2]+d[3])
        ans += x*f([d[0],d[1]+1,d[2]-1,d[3]])
    if d[3]:
        x = d[3]/(d[1]+d[2]+d[3])
        ans += x*f([d[0],d[1],d[2]+1,d[3]-1])
    stored[(d[1],d[2],d[3])] = ans
    return ans


n = int(input())
a = list(map(int,input().split()))
d = [0 for i in range(4)]
stored = {}

for i in a:
    d[i] +=1

ans = 0
print(f(d))

Fixed submission:
n = int(input())
a = list(map(int,input().split()))
d = [0 for i in range(3)]
dp = [[[0 for i in range(n+1)] for j in range(n+1)] for k in range(n+1)] 
for i in a:
    d[i-1] +=1
for k in range(n+1):
    for j in range(n+1):
        for i in range(n+1):
            if i+j+k==0 or i+j+k>n:
                continue
            ans = n/(i+j+k)
            x = i/(i+j+k)
            if x:
                ans += x*dp[k][j][i-1]
            x = j/(i+j+k)
            if x:
                ans += x*dp[k][j-1][i+1]
            x = k/(i+j+k)
            if x:
                ans += x*dp[k-1][j+1][i]
            dp[k][j][i] = ans
print(dp[d[2]][d[1]][d[0]])