Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
a = list(map(int,input().split()))
I = 0
J = 0
K = 0
for i in range(N):
    if a[i] == 3:
        I += 1
    elif a[i] == 2:
        J += 1
    elif a[i] == 1:
        K += 1

dp = [[[-1.0 for i in range(N+1)] for i in range(N+1)] for i in range(N+1)]
dp[0][0][0] = 0

def cal(i,j,k):
    if dp[i][j][k] != -1:
        return dp[i][j][k]
    else:
        res = 0
        if i != 0:
            res += i*cal(i-1,j+1,k)
        if j != 0:
            res += j*cal(i,j-1,k+1)
        if k != 0:
            res += k*cal(i,j,k-1)
        res += N
        res /= i+j+k
        dp[i][j][k] = res
        return res

print(cal(I,J,K))

Fixed submission:
N = int(input())
a = list(map(int,input().split()))
I = 0
J = 0
K = 0
for i in range(N):
    if a[i] == 1:
        I += 1
    if a[i] == 2:
        J += 1
    if a[i] == 3:
        K += 1

dp = [[[0.0]*(N+2) for _ in range(N+2)] for _ in range(N+2)]
for k in range(K+1):
    for j in range(J+K+1):
        if j+k > N:
            break
        for i in range(N+1):
            S = i+j+k
            if S > N:
                break
            if S == 0:
                continue
            dp[i][j][k] = (i*dp[i-1][j][k]+j*dp[i+1][j-1][k]+k*dp[i][j+1][k-1]+N)/S
print(dp[I][J][K])

Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
a = list(map(int,input().split()))
I = 0
J = 0
K = 0
for i in range(N):
    if a[i] == 1:
        I += 1
    if a[i] == 2:
        J += 1
    if a[i] == 3:
        K += 1

dp = [[[0]*(N+2) for _ in range(N+2)] for _ in range(N+2)]
for k in range(K+1):
    for j in range(J+K+1):
        if j+k > N:
            break
        for i in range(N+1):
            S = i+j+k
            if S > N:
                break
            if S == 0:
                continue
            dp[i][j][k] = (i*dp[i-1][j][k]+j*dp[i+1][j-1][k]+k*dp[i][j+1][k-1]+N)/S
print(dp[I][J][K])

Fixed submission:
N = int(input())
a = list(map(int,input().split()))
I = 0
J = 0
K = 0
for i in range(N):
    if a[i] == 1:
        I += 1
    if a[i] == 2:
        J += 1
    if a[i] == 3:
        K += 1

dp = [[[0.0]*(N+2) for _ in range(N+2)] for _ in range(N+2)]
for k in range(K+1):
    for j in range(J+K+1):
        if j+k > N:
            break
        for i in range(N+1):
            S = i+j+k
            if S > N:
                break
            if S == 0:
                continue
            dp[i][j][k] = (i*dp[i-1][j][k]+j*dp[i+1][j-1][k]+k*dp[i][j+1][k-1]+N)/S
print(dp[I][J][K])

Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
a = list(map(int,input().split()))
I = 0
J = 0
K = 0
for i in range(N):
    if a[i] == 1:
        I += 1
    if a[i] == 2:
        J += 1
    if a[i] == 3:
        K += 1

dp = [[[0]*(N+1) for _ in range(N+1)] for _ in range(N+1)]
for k in range(K+1):
    for j in range(J+K+1):
        if j+k > N:
            break
        for i in range(N+1):
            if i+j+k > N:
                break
            if i > 0:
                dp[i][j][k] += i*dp[i-1][j][k]
            if j > 0:
                dp[i][j][k] += j*dp[i+1][j-1][k]
            if k > 0:
                dp[i][j][k] += k*dp[i][j+1][k-1]
            if i+j+k > 0:
                dp[i][j][k] += N
                dp[i][j][k] /= i+j+k
print(dp[I][J][K])

Fixed submission:
N = int(input())
a = list(map(int,input().split()))
I = 0
J = 0
K = 0
for i in range(N):
    if a[i] == 1:
        I += 1
    if a[i] == 2:
        J += 1
    if a[i] == 3:
        K += 1

dp = [[[0.0]*(N+2) for _ in range(N+2)] for _ in range(N+2)]
for k in range(K+1):
    for j in range(J+K+1):
        if j+k > N:
            break
        for i in range(N+1):
            S = i+j+k
            if S > N:
                break
            if S == 0:
                continue
            dp[i][j][k] = (i*dp[i-1][j][k]+j*dp[i+1][j-1][k]+k*dp[i][j+1][k-1]+N)/S
print(dp[I][J][K])

Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
a = list(map(int,input().split()))
I = 0
J = 0
K = 0
for i in range(N):
    if a[i] == 3:
        I += 1
    elif a[i] == 2:
        J += 1
    elif a[i] == 1:
        K += 1

dp = [[[-1 for i in range(N+1)] for i in range(N+1)] for i in range(N+1)]
dp[0][0][0] = 0

def cal(i,j,k):
    if dp[i][j][k] != -1:
        return dp[i][j][k]
    else:
        res = 0
        if i != 0:
            res += i*cal(i-1,j+1,k)
        if j != 0:
            res += j*cal(i,j-1,k+1)
        if k != 0:
            res += k*cal(i,j,k-1)
        res += N
        res /= i+j+k
        dp[i][j][k] = res
        return res

print(cal(I,J,K))

Fixed submission:
N = int(input())
a = list(map(int,input().split()))
I = 0
J = 0
K = 0
for i in range(N):
    if a[i] == 1:
        I += 1
    if a[i] == 2:
        J += 1
    if a[i] == 3:
        K += 1

dp = [[[0.0]*(N+2) for _ in range(N+2)] for _ in range(N+2)]
for k in range(K+1):
    for j in range(J+K+1):
        if j+k > N:
            break
        for i in range(N+1):
            S = i+j+k
            if S > N:
                break
            if S == 0:
                continue
            dp[i][j][k] = (i*dp[i-1][j][k]+j*dp[i+1][j-1][k]+k*dp[i][j+1][k-1]+N)/S
print(dp[I][J][K])

Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
a = list(map(int,input().split()))
I = 0
J = 0
K = 0
for i in range(N):
    if a[i] == 3:
        I += 1
    elif a[i] == 2:
        J += 1
    elif a[i] == 1:
        K += 1

DP = [[[-1 for i in range(N+1)] for i in range(N+1)] for i in range(N+1)]
DP[0][0][0]=0

def cal(i,j,k):
    if DP[i][j][k] != -1:
        return DP[i][j][k]
    else:
        res = 0
        if i != 0:
            res += i*cal(i-1,j+1,k)
        if j != 0:
            res += j*cal(i,j-1,k+1)
        if k != 0:
            res += k*cal(i,j,k-1)
        res += N
        res /= i+j+k
        DP[i][j][k] = res
        return res

print(cal(I,J,K))

Fixed submission:
N = int(input())
a = list(map(int,input().split()))
I = 0
J = 0
K = 0
for i in range(N):
    if a[i] == 1:
        I += 1
    if a[i] == 2:
        J += 1
    if a[i] == 3:
        K += 1

dp = [[[0.0]*(N+2) for _ in range(N+2)] for _ in range(N+2)]
for k in range(K+1):
    for j in range(J+K+1):
        if j+k > N:
            break
        for i in range(N+1):
            S = i+j+k
            if S > N:
                break
            if S == 0:
                continue
            dp[i][j][k] = (i*dp[i-1][j][k]+j*dp[i+1][j-1][k]+k*dp[i][j+1][k-1]+N)/S
print(dp[I][J][K])