Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
*A, = map(int, input().split())
C = [0]*4
for a in A:
    C[a] += 1

c1, c2, c3 = C[1:]
dp = [[[0]*(c1+c2+c3+1) for i in range(c2+c3+2)] for j in range(c3+2)]
for c in range(c3+1):
    M1 = c2+c3-c
    T = dp[c-1]
    S = dp[c]
    for b in range(M1+1):
        M2 = N-b-c
        X = S[b-1]; Y = S[b]
        Z = T[b+1]
        for a in range(M2==N, M2+1):
            r = N
            if a:
                r += a*Y[a-1]
            if b:
                r += b*X[a+1]
            if c:
                r += c*Z[a]
            Y[a] = r / (a+b+c)
print("%.10f" % dp[c3][c2][c1])

Fixed submission:
N = int(input())
*A, = map(int, input().split())
C = [0]*4
for a in A:
    C[a] += 1

c1, c2, c3 = C[1:]
dp = [[[0]*(c1+c2+c3+1) for i in range(c2+c3+2)] for j in range(c3+2)]
for c in range(c3+1):
    M1 = c2+c3-c
    T = dp[c-1]
    S = dp[c]
    for b in range(M1+1):
        M2 = N-b-c
        X = S[b-1]; Y = S[b]
        Z = T[b+1]
        for a in range(M2==N, M2+1):
            r = N
            if a:
                r += a*Y[a-1]
            if b:
                r += b*X[a+1]
            if c:
                r += c*Z[a]
            Y[a] = r / (a+b+c)
print("%.10f" % dp[c3][c2][c1])

Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
*A, = map(int, input().split())
C = [0]*4
for a in A:
    C[a] += 1

c1, c2, c3 = C[1:]
dp = [[[0]*301 for i in range(301)] for j in range(301)]
for c in range(c3+1):
    M1 = c2+c3-c
    for b in range(M1+1):
        M2 = N-b-c
        for a in range(M2==N, M2+1):
            r = N
            if a:
                r += a*dp[a-1][b][c]
            if b:
                r += b*dp[a+1][b-1][c]
            if c:
                r += c*dp[a][b+1][c-1]
            dp[a][b][c] = r / (a+b+c)
print("%.10f" % dp[c1][c2][c3])

Fixed submission:
N = int(input())
*A, = map(int, input().split())
C = [0]*4
for a in A:
    C[a] += 1

c1, c2, c3 = C[1:]
dp = [[[0]*(c1+c2+c3+1) for i in range(c2+c3+2)] for j in range(c3+2)]
for c in range(c3+1):
    M1 = c2+c3-c
    T = dp[c-1]
    S = dp[c]
    for b in range(M1+1):
        M2 = N-b-c
        X = S[b-1]; Y = S[b]
        Z = T[b+1]
        for a in range(M2==N, M2+1):
            r = N
            if a:
                r += a*Y[a-1]
            if b:
                r += b*X[a+1]
            if c:
                r += c*Z[a]
            Y[a] = r / (a+b+c)
print("%.10f" % dp[c3][c2][c1])

Buggy python submission (verdict: Time Limit Exceeded):
import sys
sys.setrecursionlimit(10**6)

N = int(input())
*A, = map(int, input().split())
C = [0]*4
for a in A:
    C[a] += 1

c1, c2, c3 = C[1:]
memo = {}
memo[0, 0, 0] = 0.
V = [0]*(c1+c2+c3+1)
for i in range(c1+c2+c3):
    V[i+1] = V[i] + N / (i+1)
def dfs(a, b, c):
    key = (a, b, c)
    if b == c == 0:
        return V[a]
    if key in memo:
        return memo[key]
    r = 0
    if a:
        r += a*(dfs(a-1, b, c)+1)
    if b:
        r += b*(dfs(a+1, b-1, c)+1)
    if c:
        r += c*(dfs(a, b+1, c-1)+1)
    p = (a + b + c)
    memo[key] = r = (r + N - p) / p
    return r
print("%.10f" % dfs(c1, c2, c3))

Fixed submission:
N = int(input())
*A, = map(int, input().split())
C = [0]*4
for a in A:
    C[a] += 1

c1, c2, c3 = C[1:]
dp = [[[0]*(c1+c2+c3+1) for i in range(c2+c3+2)] for j in range(c3+2)]
for c in range(c3+1):
    M1 = c2+c3-c
    T = dp[c-1]
    S = dp[c]
    for b in range(M1+1):
        M2 = N-b-c
        X = S[b-1]; Y = S[b]
        Z = T[b+1]
        for a in range(M2==N, M2+1):
            r = N
            if a:
                r += a*Y[a-1]
            if b:
                r += b*X[a+1]
            if c:
                r += c*Z[a]
            Y[a] = r / (a+b+c)
print("%.10f" % dp[c3][c2][c1])

Buggy python submission (verdict: Time Limit Exceeded):
import sys
sys.setrecursionlimit(10**6)

N = int(input())
*A, = map(int, input().split())
C = [0]*4
for a in A:
    C[a] += 1

c1, c2, c3 = C[1:]
memo = {}
memo[0, 0, 0] = 0.
def dfs(a, b, c):
    key = (a, b, c)
    if key in memo:
        return memo[key]
    r = 0
    if a:
        r += a*(dfs(a-1, b, c)+1)
    if b:
        r += b*(dfs(a+1, b-1, c)+1)
    if c:
        r += c*(dfs(a, b+1, c-1)+1)
    p = (a + b + c)
    memo[key] = r = (r + N - p) / p
    return r
print("%.10f" % dfs(c1, c2, c3))

Fixed submission:
N = int(input())
*A, = map(int, input().split())
C = [0]*4
for a in A:
    C[a] += 1

c1, c2, c3 = C[1:]
dp = [[[0]*(c1+c2+c3+1) for i in range(c2+c3+2)] for j in range(c3+2)]
for c in range(c3+1):
    M1 = c2+c3-c
    T = dp[c-1]
    S = dp[c]
    for b in range(M1+1):
        M2 = N-b-c
        X = S[b-1]; Y = S[b]
        Z = T[b+1]
        for a in range(M2==N, M2+1):
            r = N
            if a:
                r += a*Y[a-1]
            if b:
                r += b*X[a+1]
            if c:
                r += c*Z[a]
            Y[a] = r / (a+b+c)
print("%.10f" % dp[c3][c2][c1])

Buggy python submission (verdict: Time Limit Exceeded):
import sys
sys.setrecursionlimit(10**6)

N = int(input())
*A, = map(int, input().split())
C = [0]*4
for a in A:
    C[a] += 1

c1, c2, c3 = C[1:]
memo = [[[-1]*(301) for i in range(301)] for j in range(301)]
memo[0][0][0] = 0.
def dfs(a, b, c):
    if memo[a][b][c] != -1:
        return memo[a][b][c]
    r = 0
    if a:
        r += a*(dfs(a-1, b, c)+1)
    if b:
        r += b*(dfs(a+1, b-1, c)+1)
    if c:
        r += c*(dfs(a, b+1, c-1)+1)
    p = (a + b + c)
    memo[a][b][c] = r = (r + N - p) / p
    return r

print("%.10f" % dfs(c1, c2, c3))

Fixed submission:
N = int(input())
*A, = map(int, input().split())
C = [0]*4
for a in A:
    C[a] += 1

c1, c2, c3 = C[1:]
dp = [[[0]*(c1+c2+c3+1) for i in range(c2+c3+2)] for j in range(c3+2)]
for c in range(c3+1):
    M1 = c2+c3-c
    T = dp[c-1]
    S = dp[c]
    for b in range(M1+1):
        M2 = N-b-c
        X = S[b-1]; Y = S[b]
        Z = T[b+1]
        for a in range(M2==N, M2+1):
            r = N
            if a:
                r += a*Y[a-1]
            if b:
                r += b*X[a+1]
            if c:
                r += c*Z[a]
            Y[a] = r / (a+b+c)
print("%.10f" % dp[c3][c2][c1])