Buggy python submission (verdict: Time Limit Exceeded):
from collections import Counter
N = int(input())
A = list(map(int, input().split()))

dp = Counter()

dp[0] = 0
dp[1] = 1

geta = 1000
geta2 = geta**2
CA = Counter(A)
SA = CA[1]
SB = CA[2]
SC = CA[3]
for c in range(SC+1):
    for b in range(SB+SC+1):
        if b + c > N:
            break
        for a in range(N+1):
            if a + b + c > N:
                break
            S = a + b + c
            if S == 0:
                continue
            dp[a*geta2 + b*geta + c] = N/S + a/S*dp[(a-1)*geta2 + b*geta + c]\
            +b/S*dp[(a+1)*geta2 + (b-1)*geta + c]\
            +c/S*dp[a*geta2 + (b+1)*geta + c - 1]

print(dp[SA*geta2 + SB*geta + SC])

Fixed submission:
from collections import Counter
N = int(input())
A = list(map(int, input().split()))

dp = [[[0]*(N+2) for _ in range(N+2)] for _ in range(N+2)]
CA = Counter(A)
SA = CA[1]
SB = CA[2]
SC = CA[3]
for c in range(SC+1):
    for b in range(SB+SC+1):
        if b + c > N:
            break
        for a in range(N+1):
            if a + b + c > N:
                break
            S = a + b + c
            if S == 0:
                continue
            dp[a][b][c] = N/S + a/S*dp[a-1][b][c]\
            +b/S*dp[a+1][b-1][c]\
            +c/S*dp[a][b+1][c - 1]

print(dp[SA][SB][SC])

Buggy python submission (verdict: Time Limit Exceeded):
from collections import Counter
N = int(input())
A = list(map(int, input().split()))

dp = Counter()

dp[0] = 0
dp[1] = 1

geta = 1000
geta2 = geta**2
CA = Counter(A)
SA = CA[1]
SB = CA[2]
SC = CA[3]
for c in range(SC+1):
    for b in range(SB+SC+1):
        if b + c > N:
            break
        for a in range(N+1):
            if a + b + c > N:
                break
            S = a + b + c
            if S == 0:
                continue
            res = N/S
            res += a/S*dp[(a-1)*geta2 + b*geta + c]
            res += b/S*dp[(a+1)*geta2 + (b-1)*geta + c]
            res += c/S*dp[a*geta2 + (b+1)*geta + c - 1]
            dp[a*geta2 + b*geta + c] = res

print(dp[SA*geta2 + SB*geta + SC])

Fixed submission:
from collections import Counter
N = int(input())
A = list(map(int, input().split()))

dp = [[[0]*(N+2) for _ in range(N+2)] for _ in range(N+2)]
CA = Counter(A)
SA = CA[1]
SB = CA[2]
SC = CA[3]
for c in range(SC+1):
    for b in range(SB+SC+1):
        if b + c > N:
            break
        for a in range(N+1):
            if a + b + c > N:
                break
            S = a + b + c
            if S == 0:
                continue
            dp[a][b][c] = N/S + a/S*dp[a-1][b][c]\
            +b/S*dp[a+1][b-1][c]\
            +c/S*dp[a][b+1][c - 1]

print(dp[SA][SB][SC])

Buggy python submission (verdict: Time Limit Exceeded):
from collections import Counter
N = int(input())
A = list(map(int, input().split()))

dp = Counter()

dp[0] = 0
dp[1] = 1

geta = 1000
geta2 = geta**2
for c in range(N+1):
    for b in range(N+1):
        if b + c > N:
            break
        for a in range(N+1):
            if a + b + c > N:
                break
            S = a + b + c
            res = 0 if S == 0 else N/S
            if a != 0:
                res += a/S*dp[(a-1)*geta2 + b*geta + c]
            if b != 0:
                res += b/S*dp[(a+1)*geta2 + (b-1)*geta + c]
            if c != 0:
                res += c/S*dp[a*geta2 + (b+1)*geta + c - 1]
            dp[a*geta2 + b*geta + c] = res
CA = Counter(A)
print(dp[CA[1]*geta2 + CA[2]*geta + CA[3]])

Fixed submission:
from collections import Counter
N = int(input())
A = list(map(int, input().split()))

dp = [[[0]*(N+2) for _ in range(N+2)] for _ in range(N+2)]
CA = Counter(A)
SA = CA[1]
SB = CA[2]
SC = CA[3]
for c in range(SC+1):
    for b in range(SB+SC+1):
        if b + c > N:
            break
        for a in range(N+1):
            if a + b + c > N:
                break
            S = a + b + c
            if S == 0:
                continue
            dp[a][b][c] = N/S + a/S*dp[a-1][b][c]\
            +b/S*dp[a+1][b-1][c]\
            +c/S*dp[a][b+1][c - 1]

print(dp[SA][SB][SC])