Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
a = [int(x) for x in input().split()]

I = a.count(1)
J = a.count(2)
K = a.count(3)

dp = [[[0 for _ in range(K+1)] for __ in range(J+K+1)] for ___ in range(I+J+K+1)]

for k in range(K+1):
    for j in range(J+K+1-k):
        for i in range(I+J+K+1-k-j):
            if i == 0 and j == 0 and k == 0:
                continue
            if k-1 > -1 and j+1 < J+K+1:
                dp[i][j][k] += (n/(i+j+k))*(k/n)*dp[i][j+1][k-1]
            if j-1 > -1 and i+1 < I+J+K+1:
                dp[i][j][k] += (n/(i+j+k))*(j/n)*dp[i+1][j-1][k]
            if i-1 > -1:
                dp[i][j][k] += (n/(i+j+k))*(i/n)*dp[i-1][j][k]
            dp[i][j][k] += n/(i+j+k)

print(dp[I][J][K])


Fixed submission:
n = int(input())
a = [int(x) for x in input().split()]

I = a.count(3)
J = a.count(2)
K = a.count(1)

dp = [[[0 for _ in range(I+J+K+1)] for __ in range(I+J+1)]
      for ___ in range(I+1)]

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

print(dp[I][J][K])

Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
a = [int(x) for x in input().split()]

I = a.count(1)
J = a.count(2)
K = a.count(3)

dp = [[[0 for _ in range(K+1)] for __ in range(J+K+1)] for ___ in range(I+J+K+1)]

for k in range(K+1):
    for j in range(J+K+1):
        for i in range(I+J+K+1):
            if i == 0 and j == 0 and k == 0:
                continue
            if k-1 > -1 and j+1 < J+K+1:
                dp[i][j][k] += (n/(i+j+k))*(k/n)*dp[i][j+1][k-1]
            if j-1 > -1 and i+1 < I+J+K+1:
                dp[i][j][k] += (n/(i+j+k))*(j/n)*dp[i+1][j-1][k]
            if i-1 > -1:
                dp[i][j][k] += (n/(i+j+k))*(i/n)*dp[i-1][j][k]
            dp[i][j][k] += n/(i+j+k)

print(dp[I][J][K])


Fixed submission:
n = int(input())
a = [int(x) for x in input().split()]

I = a.count(3)
J = a.count(2)
K = a.count(1)

dp = [[[0 for _ in range(I+J+K+1)] for __ in range(I+J+1)]
      for ___ in range(I+1)]

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

print(dp[I][J][K])

Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
a = [int(x) for x in input().split()]

I = a.count(1)
J = a.count(2)
K = a.count(3)

dp = [[[0 for _ in range(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):
        for i in range(I+J+K+1):
            if i == 0 and j == 0 and k == 0:
                continue
            if k-1 > -1 and j+1 < n+1:
                dp[i][j][k] += (n/(i+j+k))*(k/n)*dp[i][j+1][k-1]
            if j-1 > -1 and i+1 < n+1:
                dp[i][j][k] += (n/(i+j+k))*(j/n)*dp[i+1][j-1][k]
            if i-1 > -1:
                dp[i][j][k] += (n/(i+j+k))*(i/n)*dp[i-1][j][k]
            dp[i][j][k] += n/(i+j+k)

print(dp[I][J][K])


Fixed submission:
n = int(input())
a = [int(x) for x in input().split()]

I = a.count(3)
J = a.count(2)
K = a.count(1)

dp = [[[0 for _ in range(I+J+K+1)] for __ in range(I+J+1)]
      for ___ in range(I+1)]

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

print(dp[I][J][K])