Buggy python submission (verdict: Time Limit Exceeded):
import sys
from collections import Counter

sys.setrecursionlimit(10 ** 6)
input = sys.stdin.readline
int1 = lambda x: int(x) - 1
p2D = lambda x: print(*x, sep="\n")

def main():
    n = int(input())
    aa = list(map(int, input().split()))
    cnt_aa = Counter(aa)
    nc3 = cnt_aa[3]
    nc2 = cnt_aa[2]
    nc1 = cnt_aa[1]
    dp = [[[0] * (n + 1) for _ in range(nc3 + nc2 + 1)] for _ in range(nc3 + 1)]
    for c3 in range(nc3 + 1):
        for c2 in range(nc2 + nc3 - c3 + 1):
            if c3 + c2 > n: break
            for c1 in range(n + 1):
                s = c1 + c2 + c3
                if s > n: break
                if c1 > 0: dp[c3][c2][c1] += (dp[c3][c2][c1 - 1] + n / s) * c1 / s
                if c2 > 0: dp[c3][c2][c1] += (dp[c3][c2 - 1][c1 + 1] + n / s) * c2 / s
                if c3 > 0: dp[c3][c2][c1] += (dp[c3 - 1][c2 + 1][c1] + n / s) * c3 / s
    print(dp[nc3][nc2][nc1])

main()


Fixed submission:
import sys
from collections import Counter

sys.setrecursionlimit(10 ** 6)
input = sys.stdin.readline
int1 = lambda x: int(x) - 1
p2D = lambda x: print(*x, sep="\n")

def main():
    n = int(input())
    aa = list(map(int, input().split()))
    cnt_aa = Counter(aa)
    nc3 = cnt_aa[3]
    nc2 = cnt_aa[2]
    nc1 = cnt_aa[1]
    dp = [[[0] * (n + 1) for _ in range(nc3 + nc2 + 1)] for _ in range(nc3 + 1)]
    for c3 in range(nc3 + 1):
        for c2 in range(nc2 + nc3 - c3 + 1):
            if c3 + c2 > n: break
            for c1 in range(n + 1):
                s = c1 + c2 + c3
                if s > n: break
                if c1 > 0: dp[c3][c2][c1] += (dp[c3][c2][c1 - 1] + n / s) * c1 / s
                if c2 > 0: dp[c3][c2][c1] += (dp[c3][c2 - 1][c1 + 1] + n / s) * c2 / s
                if c3 > 0: dp[c3][c2][c1] += (dp[c3 - 1][c2 + 1][c1] + n / s) * c3 / s
    print(dp[nc3][nc2][nc1])

main()

Buggy python submission (verdict: Time Limit Exceeded):
def ev(d0, d1, d2, d3):
    if d0 == n:
        return 0
    tp = (d1, d2, d3)
    if tp in memo:
        return memo[tp]
    re = n
    if d1 > 0:
        re += ev(d0 + 1, d1 - 1, d2, d3) * d1
    if d2 > 0:
        re += ev(d0, d1 + 1, d2 - 1, d3) * d2
    if d3 > 0:
        re += ev(d0, d1, d2 + 1, d3 - 1) * d3
    re /=n - d0
    memo[tp] = re
    return re


memo = {}
n = int(input())
a = list(map(int, input().split()))
ds = [0] + [a.count(i) for i in range(1, 4)]
print(ev(0, ds[1], ds[2], ds[3]))


Fixed submission:
import sys
from collections import Counter

sys.setrecursionlimit(10 ** 6)
input = sys.stdin.readline
int1 = lambda x: int(x) - 1
p2D = lambda x: print(*x, sep="\n")

def main():
    n = int(input())
    aa = list(map(int, input().split()))
    cnt_aa = Counter(aa)
    nc3 = cnt_aa[3]
    nc2 = cnt_aa[2]
    nc1 = cnt_aa[1]
    dp = [[[0] * (n + 1) for _ in range(nc3 + nc2 + 1)] for _ in range(nc3 + 1)]
    for c3 in range(nc3 + 1):
        for c2 in range(nc2 + nc3 - c3 + 1):
            if c3 + c2 > n: break
            for c1 in range(n + 1):
                s = c1 + c2 + c3
                if s > n: break
                if c1 > 0: dp[c3][c2][c1] += (dp[c3][c2][c1 - 1] + n / s) * c1 / s
                if c2 > 0: dp[c3][c2][c1] += (dp[c3][c2 - 1][c1 + 1] + n / s) * c2 / s
                if c3 > 0: dp[c3][c2][c1] += (dp[c3 - 1][c2 + 1][c1] + n / s) * c3 / s
    print(dp[nc3][nc2][nc1])

main()

Buggy python submission (verdict: Time Limit Exceeded):
def ev(d0, d1, d2, d3):
    if d0 == n:
        return 0
    if memo[d1][d2][d3] != -1:
        return memo[d1][d2][d3]
    re = 1
    if d1 > 0:
        re += ev(d0 + 1, d1 - 1, d2, d3) * d1 / n
    if d2 > 0:
        re += ev(d0, d1 + 1, d2 - 1, d3) * d2 / n
    if d3 > 0:
        re += ev(d0, d1, d2 + 1, d3 - 1) * d3 / n
    re *= n / (n - d0)
    memo[d1][d2][d3] = re
    return re


n = int(input())
a = list(map(int, input().split()))
ds = [0] + [a.count(i) for i in range(1, 4)]
memo = [[[-1] * (ds[3] + 1) for i in range(ds[2] + ds[3] + 1)] for ii in range(n + 1)]
print(ev(0, ds[1], ds[2], ds[3]))


Fixed submission:
import sys
from collections import Counter

sys.setrecursionlimit(10 ** 6)
input = sys.stdin.readline
int1 = lambda x: int(x) - 1
p2D = lambda x: print(*x, sep="\n")

def main():
    n = int(input())
    aa = list(map(int, input().split()))
    cnt_aa = Counter(aa)
    nc3 = cnt_aa[3]
    nc2 = cnt_aa[2]
    nc1 = cnt_aa[1]
    dp = [[[0] * (n + 1) for _ in range(nc3 + nc2 + 1)] for _ in range(nc3 + 1)]
    for c3 in range(nc3 + 1):
        for c2 in range(nc2 + nc3 - c3 + 1):
            if c3 + c2 > n: break
            for c1 in range(n + 1):
                s = c1 + c2 + c3
                if s > n: break
                if c1 > 0: dp[c3][c2][c1] += (dp[c3][c2][c1 - 1] + n / s) * c1 / s
                if c2 > 0: dp[c3][c2][c1] += (dp[c3][c2 - 1][c1 + 1] + n / s) * c2 / s
                if c3 > 0: dp[c3][c2][c1] += (dp[c3 - 1][c2 + 1][c1] + n / s) * c3 / s
    print(dp[nc3][nc2][nc1])

main()

Buggy python submission (verdict: Time Limit Exceeded):
def ev(d0, d1, d2, d3):
    if d0 == n:
        return 0
    tp = (d1, d2, d3)
    if tp in memo:
        return memo[tp]
    re = 1
    if d1 > 0:
        re += ev(d0 + 1, d1 - 1, d2, d3) * d1 / n
    if d2 > 0:
        re += ev(d0, d1 + 1, d2 - 1, d3) * d2 / n
    if d3 > 0:
        re += ev(d0, d1, d2 + 1, d3 - 1) * d3 / n
    re *=n / (n - d0)
    memo[tp] = re
    return re


memo = {}
n = int(input())
a = list(map(int, input().split()))
ds = [0, 0, 0, 0]
for i in range(1, 4):
    ic = a.count(i)
    ds[i] = ic
print(ev(0, ds[1], ds[2], ds[3]))


Fixed submission:
import sys
from collections import Counter

sys.setrecursionlimit(10 ** 6)
input = sys.stdin.readline
int1 = lambda x: int(x) - 1
p2D = lambda x: print(*x, sep="\n")

def main():
    n = int(input())
    aa = list(map(int, input().split()))
    cnt_aa = Counter(aa)
    nc3 = cnt_aa[3]
    nc2 = cnt_aa[2]
    nc1 = cnt_aa[1]
    dp = [[[0] * (n + 1) for _ in range(nc3 + nc2 + 1)] for _ in range(nc3 + 1)]
    for c3 in range(nc3 + 1):
        for c2 in range(nc2 + nc3 - c3 + 1):
            if c3 + c2 > n: break
            for c1 in range(n + 1):
                s = c1 + c2 + c3
                if s > n: break
                if c1 > 0: dp[c3][c2][c1] += (dp[c3][c2][c1 - 1] + n / s) * c1 / s
                if c2 > 0: dp[c3][c2][c1] += (dp[c3][c2 - 1][c1 + 1] + n / s) * c2 / s
                if c3 > 0: dp[c3][c2][c1] += (dp[c3 - 1][c2 + 1][c1] + n / s) * c3 / s
    print(dp[nc3][nc2][nc1])

main()

Buggy python submission (verdict: Time Limit Exceeded):
def ev(d0, d1, d2, d3):
    if d0 == n:
        return 0
    tp = (d1, d2, d3)
    if tp in memo:
        return memo[tp]
    re = [1]
    if d1 > 0:
        re += [ev(d0 + 1, d1 - 1, d2, d3) * d1 / n]
    if d2 > 0:
        re += [ev(d0, d1 + 1, d2 - 1, d3) * d2 / n]
    if d3 > 0:
        re += [ev(d0, d1, d2 + 1, d3 - 1) * d3 / n]
    re = sum(re) * n / (n - d0)
    memo[tp] = re
    return re


memo = {}
n = int(input())
a = list(map(int, input().split()))
ds = [0, 0, 0, 0]
for i in range(1, 4):
    ic = a.count(i)
    ds[i] = ic
print(ev(0, ds[1], ds[2], ds[3]))


Fixed submission:
import sys
from collections import Counter

sys.setrecursionlimit(10 ** 6)
input = sys.stdin.readline
int1 = lambda x: int(x) - 1
p2D = lambda x: print(*x, sep="\n")

def main():
    n = int(input())
    aa = list(map(int, input().split()))
    cnt_aa = Counter(aa)
    nc3 = cnt_aa[3]
    nc2 = cnt_aa[2]
    nc1 = cnt_aa[1]
    dp = [[[0] * (n + 1) for _ in range(nc3 + nc2 + 1)] for _ in range(nc3 + 1)]
    for c3 in range(nc3 + 1):
        for c2 in range(nc2 + nc3 - c3 + 1):
            if c3 + c2 > n: break
            for c1 in range(n + 1):
                s = c1 + c2 + c3
                if s > n: break
                if c1 > 0: dp[c3][c2][c1] += (dp[c3][c2][c1 - 1] + n / s) * c1 / s
                if c2 > 0: dp[c3][c2][c1] += (dp[c3][c2 - 1][c1 + 1] + n / s) * c2 / s
                if c3 > 0: dp[c3][c2][c1] += (dp[c3 - 1][c2 + 1][c1] + n / s) * c3 / s
    print(dp[nc3][nc2][nc1])

main()