Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
*A, = map(int, input().split())
a, b, c = A.count(1), A.count(2), A.count(3)
DP = [[[0.0 for k in range(n+1)] for j in range(n+1)] for i in range(n+1)]
for k in range(c+1):
    for j in range(b+c-k+1):
        for i in range(a+b+c-k-j+1):
            if i+j+k == 0:
                continue
            DP[i][j][k] = n
            if i > 0:
                DP[i][j][k] += DP[i-1][j][k]*i
            if j > 0:
                DP[i][j][k] += DP[i+1][j-1][k]*j
            if k > 0:
                DP[i][j][k] += DP[i][j+1][k-1]*k
            DP[i][j][k] /= (i+j+k)
print(DP[a][b][c])


Fixed submission:
n = int(input())
*A, = map(int, input().split())
a, b, c = A.count(1), A.count(2), A.count(3)
DP = [[[0.0 for k in range(n+1)] for j in range(n+1)] for i in range(n+1)]
for k in range(c+1):
    for j in range(b+c-k+1):
        for i in range(n-j-k+1):
            if i+j+k == 0:
                continue
            tmp = n
            if i > 0:
                tmp += DP[i-1][j][k]*i
            if j > 0:
                tmp += DP[i+1][j-1][k]*j
            if k > 0:
                tmp += DP[i][j+1][k-1]*k
            DP[i][j][k] = tmp/(i+j+k)
print(DP[a][b][c])

Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
*A, = map(int, input().split())
a, b, c = A.count(1), A.count(2), A.count(3)
DP = [[[0.0 for k in range(n+1)] for j in range(n+1)] for i in range(n+1)]
for k in range(c+1):
    for j in range(n-k+1):
        for i in range(n-k-j+1):
            if i+j+k == 0:
                continue
            DP[i][j][k] = n
            if i > 0:
                DP[i][j][k] += DP[i-1][j][k]*i
            if j > 0:
                DP[i][j][k] += DP[i+1][j-1][k]*j
            if k > 0:
                DP[i][j][k] += DP[i][j+1][k-1]*k
            DP[i][j][k] /= (i+j+k)
print(DP[a][b][c])


Fixed submission:
n = int(input())
*A, = map(int, input().split())
a, b, c = A.count(1), A.count(2), A.count(3)
DP = [[[0.0 for k in range(n+1)] for j in range(n+1)] for i in range(n+1)]
for k in range(c+1):
    for j in range(b+c-k+1):
        for i in range(n-j-k+1):
            if i+j+k == 0:
                continue
            tmp = n
            if i > 0:
                tmp += DP[i-1][j][k]*i
            if j > 0:
                tmp += DP[i+1][j-1][k]*j
            if k > 0:
                tmp += DP[i][j+1][k-1]*k
            DP[i][j][k] = tmp/(i+j+k)
print(DP[a][b][c])

Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
*A, = map(int, input().split())
a, b, c = A.count(1), A.count(2), A.count(3)
DP = [[[0.0 for k in range(n+1)] for j in range(n+1)] for i in range(n+1)]
for k in range(n+1):
    for j in range(n-k+1):
        for i in range(n-k-j+1):
            if i+j+k == 0:
                continue
            DP[i][j][k] = n
            if i > 0:
                DP[i][j][k] += DP[i-1][j][k]*i
            if j > 0:
                DP[i][j][k] += DP[i+1][j-1][k]*j
            if k > 0:
                DP[i][j][k] += DP[i][j+1][k-1]*k
            DP[i][j][k] /= (i+j+k)
print(DP[a][b][c])


Fixed submission:
n = int(input())
*A, = map(int, input().split())
a, b, c = A.count(1), A.count(2), A.count(3)
DP = [[[0.0 for k in range(n+1)] for j in range(n+1)] for i in range(n+1)]
for k in range(c+1):
    for j in range(b+c-k+1):
        for i in range(n-j-k+1):
            if i+j+k == 0:
                continue
            tmp = n
            if i > 0:
                tmp += DP[i-1][j][k]*i
            if j > 0:
                tmp += DP[i+1][j-1][k]*j
            if k > 0:
                tmp += DP[i][j+1][k-1]*k
            DP[i][j][k] = tmp/(i+j+k)
print(DP[a][b][c])

Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
*A, = map(int, input().split())
a, b, c = A.count(1), A.count(2), A.count(3)
DP = [[[None for k in range(n+1)] for j in range(n+1)] for i in range(n+1)]
DP[0][0][0] = 0.0
def dp(i, j, k):
    if i < 0 or j < 0 or k < 0:
        return 0.0
    if DP[i][j][k] != None:
        return DP[i][j][k]
    DP[i][j][k] = (n+dp(i-1, j, k)*i+dp(i+1, j-1, k)*j+dp(i, j+1, k-1)*k)/(i+j+k)
    return DP[i][j][k]
print(dp(a, b, c))


Fixed submission:
n = int(input())
*A, = map(int, input().split())
a, b, c = A.count(1), A.count(2), A.count(3)
DP = [[[0.0 for k in range(n+1)] for j in range(n+1)] for i in range(n+1)]
for k in range(c+1):
    for j in range(b+c-k+1):
        for i in range(n-j-k+1):
            if i+j+k == 0:
                continue
            tmp = n
            if i > 0:
                tmp += DP[i-1][j][k]*i
            if j > 0:
                tmp += DP[i+1][j-1][k]*j
            if k > 0:
                tmp += DP[i][j+1][k-1]*k
            DP[i][j][k] = tmp/(i+j+k)
print(DP[a][b][c])

Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
*A, = map(int, input().split())
a, b, c = A.count(1), A.count(2), A.count(3)
DP = [[[None for k in range(n+1)] for j in range(n+1)] for i in range(n+1)]
DP[0][0][0] = 0
def dp(i, j, k):
    if i < 0 or j < 0 or k < 0:
        return 0
    if DP[i][j][k] != None:
        return DP[i][j][k]
    DP[i][j][k] = (n+dp(i-1, j, k)*i+dp(i+1, j-1, k)*j+dp(i, j+1, k-1)*k)/(i+j+k)
    return DP[i][j][k]
print(dp(a, b, c))


Fixed submission:
n = int(input())
*A, = map(int, input().split())
a, b, c = A.count(1), A.count(2), A.count(3)
DP = [[[0.0 for k in range(n+1)] for j in range(n+1)] for i in range(n+1)]
for k in range(c+1):
    for j in range(b+c-k+1):
        for i in range(n-j-k+1):
            if i+j+k == 0:
                continue
            tmp = n
            if i > 0:
                tmp += DP[i-1][j][k]*i
            if j > 0:
                tmp += DP[i+1][j-1][k]*j
            if k > 0:
                tmp += DP[i][j+1][k-1]*k
            DP[i][j][k] = tmp/(i+j+k)
print(DP[a][b][c])

Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
*A, = map(int, input().split())
a, b, c = A.count(1), A.count(2), A.count(3)
DP = [[[0 for k in range(n+1)] for j in range(n+1)] for i in range(n+1)]
for k in range(n+1):
    for j in range(n+1):
        for i in range(n+1):
            if i+j+k == 0:
                continue
            tmp = n
            if i > 0:
                tmp += DP[i-1][j][k]*i
            if j > 0 and i < n:
                tmp += DP[i+1][j-1][k]*j
            if k > 0 and j < n:
                tmp += DP[i][j+1][k-1]*k
            DP[i][j][k] = tmp/(i+j+k)
print(DP[a][b][c])


Fixed submission:
n = int(input())
*A, = map(int, input().split())
a, b, c = A.count(1), A.count(2), A.count(3)
DP = [[[0.0 for k in range(n+1)] for j in range(n+1)] for i in range(n+1)]
for k in range(c+1):
    for j in range(b+c-k+1):
        for i in range(n-j-k+1):
            if i+j+k == 0:
                continue
            tmp = n
            if i > 0:
                tmp += DP[i-1][j][k]*i
            if j > 0:
                tmp += DP[i+1][j-1][k]*j
            if k > 0:
                tmp += DP[i][j+1][k-1]*k
            DP[i][j][k] = tmp/(i+j+k)
print(DP[a][b][c])

Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
*A, = map(int, input().split())
a, b, c = A.count(1), A.count(2), A.count(3)
DP = [[[0.0 for k in range(n+1)] for j in range(n+1)] for i in range(n+1)]
for k in range(n+1):
    for j in range(n+1):
        for i in range(n+1):
            if i+j+k == 0:
                continue
            tmp = n
            if i > 0:
                tmp += DP[i-1][j][k]*i
            if j > 0 and i < n:
                tmp += DP[i+1][j-1][k]*j
            if k > 0 and j < n:
                tmp += DP[i][j+1][k-1]*k
            DP[i][j][k] = tmp/(i+j+k)
print(DP[a][b][c])


Fixed submission:
n = int(input())
*A, = map(int, input().split())
a, b, c = A.count(1), A.count(2), A.count(3)
DP = [[[0.0 for k in range(n+1)] for j in range(n+1)] for i in range(n+1)]
for k in range(c+1):
    for j in range(b+c-k+1):
        for i in range(n-j-k+1):
            if i+j+k == 0:
                continue
            tmp = n
            if i > 0:
                tmp += DP[i-1][j][k]*i
            if j > 0:
                tmp += DP[i+1][j-1][k]*j
            if k > 0:
                tmp += DP[i][j+1][k-1]*k
            DP[i][j][k] = tmp/(i+j+k)
print(DP[a][b][c])

Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
*A, = map(int, input().split())
a, b, c = A.count(1), A.count(2), A.count(3)
DP = [[[0 for k in range(n+1)] for j in range(n+1)] for i in range(n+1)]
for k in range(c+1):
    for j in range(b+c-k+1):
        for i in range(n-j-k+1):
            if i+j+k == 0:
                continue
            tmp = n
            if i > 0:
                tmp += DP[i-1][j][k]*i
            if j > 0:
                tmp += DP[i+1][j-1][k]*j
            if k > 0:
                tmp += DP[i][j+1][k-1]*k
            DP[i][j][k] = tmp/(i+j+k)
print(DP[a][b][c])


Fixed submission:
n = int(input())
*A, = map(int, input().split())
a, b, c = A.count(1), A.count(2), A.count(3)
DP = [[[0.0 for k in range(n+1)] for j in range(n+1)] for i in range(n+1)]
for k in range(c+1):
    for j in range(b+c-k+1):
        for i in range(n-j-k+1):
            if i+j+k == 0:
                continue
            tmp = n
            if i > 0:
                tmp += DP[i-1][j][k]*i
            if j > 0:
                tmp += DP[i+1][j-1][k]*j
            if k > 0:
                tmp += DP[i][j+1][k-1]*k
            DP[i][j][k] = tmp/(i+j+k)
print(DP[a][b][c])

Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
*A, = map(int, input().split())

# メモ化再帰
DP = [[[-1 for k in range(n+1)] for j in range(n+1)] for i in range(n+1)]


def f(i, j, k):
    if DP[i][j][k] != -1:
        return DP[i][j][k]
    if i+j+k == 0:
        return 0
    tmp = 0.0
    if i > 0:
        tmp += i*f(i-1, j, k)
    if j > 0:
        tmp += j*f(i+1, j-1, k)
    if k > 0:
        tmp += k*f(i, j+1, k-1)
    tmp += n
    tmp /= i+j+k
    DP[i][j][k] = tmp
    return tmp


i, j, k = A.count(1), A.count(2), A.count(3)
print(f(i, j, k))


Fixed submission:
n = int(input())
*A, = map(int, input().split())
a, b, c = A.count(1), A.count(2), A.count(3)
DP = [[[0.0 for k in range(n+1)] for j in range(n+1)] for i in range(n+1)]
for k in range(c+1):
    for j in range(b+c-k+1):
        for i in range(n-j-k+1):
            if i+j+k == 0:
                continue
            tmp = n
            if i > 0:
                tmp += DP[i-1][j][k]*i
            if j > 0:
                tmp += DP[i+1][j-1][k]*j
            if k > 0:
                tmp += DP[i][j+1][k-1]*k
            DP[i][j][k] = tmp/(i+j+k)
print(DP[a][b][c])

Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
*A, = map(int, input().split())
a, b, c = 0, 0, 0
for x in A:
    if x == 1:
        a += 1
    if x == 2:
        b += 1
    if x == 3:
        c += 1
DP = [[[0 for k in range(n+1)] for j in range(n+1)] for i in range(n+1)]
for k in range(c+1):
    for j in range(b+c-k+1):
        for i in range(n-j-k+1):
            if i+j+k == 0:
                continue
            tmp = n
            if i > 0:
                tmp += DP[i-1][j][k]*i
            if j > 0:
                tmp += DP[i+1][j-1][k]*j
            if k > 0:
                tmp += DP[i][j+1][k-1]*k
            DP[i][j][k] = tmp/(i+j+k)
print(DP[a][b][c])


Fixed submission:
n = int(input())
*A, = map(int, input().split())
a, b, c = A.count(1), A.count(2), A.count(3)
DP = [[[0.0 for k in range(n+1)] for j in range(n+1)] for i in range(n+1)]
for k in range(c+1):
    for j in range(b+c-k+1):
        for i in range(n-j-k+1):
            if i+j+k == 0:
                continue
            tmp = n
            if i > 0:
                tmp += DP[i-1][j][k]*i
            if j > 0:
                tmp += DP[i+1][j-1][k]*j
            if k > 0:
                tmp += DP[i][j+1][k-1]*k
            DP[i][j][k] = tmp/(i+j+k)
print(DP[a][b][c])

Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
*A, = map(int, input().split())
a, b, c = A.count(1), A.count(2), A.count(3)
DP = [[[0 for k in range(n+1)] for j in range(n+1)] for i in range(n+1)]
for k in range(c+1):
    for j in range(b+c-k+1):
        for i in range(n-j-k+1):
            if i+j+k == 0:
                continue
            tmp = n
            if i > 0:
                tmp += DP[i-1][j][k]*i
            if j > 0:
                tmp += DP[i+1][j-1][k]*j
            if k > 0:
                tmp += DP[i][j+1][k-1]*k
            DP[i][j][k] = tmp/(i+j+k)
print(DP[a][b][c])


Fixed submission:
n = int(input())
*A, = map(int, input().split())
a, b, c = A.count(1), A.count(2), A.count(3)
DP = [[[0.0 for k in range(n+1)] for j in range(n+1)] for i in range(n+1)]
for k in range(c+1):
    for j in range(b+c-k+1):
        for i in range(n-j-k+1):
            if i+j+k == 0:
                continue
            tmp = n
            if i > 0:
                tmp += DP[i-1][j][k]*i
            if j > 0:
                tmp += DP[i+1][j-1][k]*j
            if k > 0:
                tmp += DP[i][j+1][k-1]*k
            DP[i][j][k] = tmp/(i+j+k)
print(DP[a][b][c])

Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
*A, = map(int, input().split())
a, b, c = A.count(1), A.count(2), A.count(3)
DP = [[[0 for k in range(n+1)] for j in range(n+1)] for i in range(n+1)]
for k in range(c+1):
    for j in range(b+c-k+1):
        for i in range(n-j-k+1):
            tmp = 0
            if i+j+k > 0:
                tmp += n/(i+j+k)
            if i > 0:
                tmp += DP[i-1][j][k]*i/(i+j+k)
            if j > 0:
                tmp += DP[i+1][j-1][k]*j/(i+j+k)
            if k > 0:
                tmp += DP[i][j+1][k-1]*k/(i+j+k)
            DP[i][j][k] = tmp
print(DP[a][b][c])


Fixed submission:
n = int(input())
*A, = map(int, input().split())
a, b, c = A.count(1), A.count(2), A.count(3)
DP = [[[0.0 for k in range(n+1)] for j in range(n+1)] for i in range(n+1)]
for k in range(c+1):
    for j in range(b+c-k+1):
        for i in range(n-j-k+1):
            if i+j+k == 0:
                continue
            tmp = n
            if i > 0:
                tmp += DP[i-1][j][k]*i
            if j > 0:
                tmp += DP[i+1][j-1][k]*j
            if k > 0:
                tmp += DP[i][j+1][k-1]*k
            DP[i][j][k] = tmp/(i+j+k)
print(DP[a][b][c])

Buggy python submission (verdict: Time Limit Exceeded):
def main():
    n = int(input())
    *A, = map(int, input().split())
    a, b, c = A.count(1), A.count(2), A.count(3)
    DP = [[[0 for k in range(n+1)] for j in range(n+1)] for i in range(n+1)]
    for k in range(c+1):
        for j in range(b+c-k+1):
            for i in range(n-j-k+1):
                if i+j+k > 0:
                    DP[i][j][k] += n/(i+j+k)
                if i > 0:
                    DP[i][j][k] += DP[i-1][j][k]*i/(i+j+k)
                if j > 0:
                    DP[i][j][k] += DP[i+1][j-1][k]*j/(i+j+k)
                if k > 0:
                    DP[i][j][k] += DP[i][j+1][k-1]*k/(i+j+k)
    print(DP[a][b][c])


main()


Fixed submission:
n = int(input())
*A, = map(int, input().split())
a, b, c = A.count(1), A.count(2), A.count(3)
DP = [[[0.0 for k in range(n+1)] for j in range(n+1)] for i in range(n+1)]
for k in range(c+1):
    for j in range(b+c-k+1):
        for i in range(n-j-k+1):
            if i+j+k == 0:
                continue
            tmp = n
            if i > 0:
                tmp += DP[i-1][j][k]*i
            if j > 0:
                tmp += DP[i+1][j-1][k]*j
            if k > 0:
                tmp += DP[i][j+1][k-1]*k
            DP[i][j][k] = tmp/(i+j+k)
print(DP[a][b][c])

Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
*A, = map(int, input().split())
a, b, c = A.count(1), A.count(2), A.count(3)
DP = [[[0 for k in range(n+1)] for j in range(n+1)] for i in range(n+1)]
for k in range(c+1):
    for j in range(b+c-k+1):
        for i in range(n-j-k+1):
            if i+j+k > 0:
                DP[i][j][k] += n/(i+j+k)
            if i > 0:
                DP[i][j][k] += DP[i-1][j][k]*i/(i+j+k)
            if j > 0:
                DP[i][j][k] += DP[i+1][j-1][k]*j/(i+j+k)
            if k > 0:
                DP[i][j][k] += DP[i][j+1][k-1]*k/(i+j+k)
print(DP[a][b][c])


Fixed submission:
n = int(input())
*A, = map(int, input().split())
a, b, c = A.count(1), A.count(2), A.count(3)
DP = [[[0.0 for k in range(n+1)] for j in range(n+1)] for i in range(n+1)]
for k in range(c+1):
    for j in range(b+c-k+1):
        for i in range(n-j-k+1):
            if i+j+k == 0:
                continue
            tmp = n
            if i > 0:
                tmp += DP[i-1][j][k]*i
            if j > 0:
                tmp += DP[i+1][j-1][k]*j
            if k > 0:
                tmp += DP[i][j+1][k-1]*k
            DP[i][j][k] = tmp/(i+j+k)
print(DP[a][b][c])

Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
*A, = map(int, input().split())
a, b, c = A.count(1), A.count(2), A.count(3)
DP = [[[0 for k in range(n+1)] for j in range(n+1)] for i in range(n+1)]
for k in range(c+1):
    for j in range(b+c+1):
        for i in range(n-j-k+1):
            if i+j+k > 0:
                DP[i][j][k] += n/(i+j+k)
            if i > 0:
                DP[i][j][k] += DP[i-1][j][k]*i/(i+j+k)
            if j > 0:
                DP[i][j][k] += DP[i+1][j-1][k]*j/(i+j+k)
            if k > 0:
                DP[i][j][k] += DP[i][j+1][k-1]*k/(i+j+k)
print(DP[a][b][c])


Fixed submission:
n = int(input())
*A, = map(int, input().split())
a, b, c = A.count(1), A.count(2), A.count(3)
DP = [[[0.0 for k in range(n+1)] for j in range(n+1)] for i in range(n+1)]
for k in range(c+1):
    for j in range(b+c-k+1):
        for i in range(n-j-k+1):
            if i+j+k == 0:
                continue
            tmp = n
            if i > 0:
                tmp += DP[i-1][j][k]*i
            if j > 0:
                tmp += DP[i+1][j-1][k]*j
            if k > 0:
                tmp += DP[i][j+1][k-1]*k
            DP[i][j][k] = tmp/(i+j+k)
print(DP[a][b][c])

Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
DP = [[[0 for k in range(n+1)] for j in range(n+1)] for i in range(n+1)]
for k in range(n+1):
    for j in range(n+1):
        for i in range(n+1):
            if i+j+k <= n:
                if i+j+k > 0:
                    DP[i][j][k] += n/(i+j+k)
                if i > 0:
                    DP[i][j][k] += DP[i-1][j][k]*i/(i+j+k)
                if j > 0:
                    DP[i][j][k] += DP[i+1][j-1][k]*j/(i+j+k)
                if k > 0:
                    DP[i][j][k] += DP[i][j+1][k-1]*k/(i+j+k)
*A, = map(int, input().split())
i, j, k = A.count(1), A.count(2), A.count(3)
print(DP[i][j][k])


Fixed submission:
n = int(input())
*A, = map(int, input().split())
a, b, c = A.count(1), A.count(2), A.count(3)
DP = [[[0.0 for k in range(n+1)] for j in range(n+1)] for i in range(n+1)]
for k in range(c+1):
    for j in range(b+c-k+1):
        for i in range(n-j-k+1):
            if i+j+k == 0:
                continue
            tmp = n
            if i > 0:
                tmp += DP[i-1][j][k]*i
            if j > 0:
                tmp += DP[i+1][j-1][k]*j
            if k > 0:
                tmp += DP[i][j+1][k-1]*k
            DP[i][j][k] = tmp/(i+j+k)
print(DP[a][b][c])

Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
DP = [[[0 for k in range(n+1)] for j in range(n+1)] for i in range(n+1)]
for k in range(n+1):
    for j in range(n+1):
        for i in range(n+1):
            if i+j+k <= n:
                if i+j+k > 0:
                    DP[i][j][k] += n/(i+j+k)
                if i > 0:
                    DP[i][j][k] += DP[i-1][j][k]*i/(i+j+k)
                if j > 0:
                    DP[i][j][k] += DP[i+1][j-1][k]*j/(i+j+k)
                if k > 0:
                    DP[i][j][k] += DP[i][j+1][k-1]*k/(i+j+k)
*A, = map(int, input().split())
i, j, k = A.count(1), A.count(2), A.count(3)
print(DP[i][j][k])


Fixed submission:
n = int(input())
*A, = map(int, input().split())
a, b, c = A.count(1), A.count(2), A.count(3)
DP = [[[0.0 for k in range(n+1)] for j in range(n+1)] for i in range(n+1)]
for k in range(c+1):
    for j in range(b+c-k+1):
        for i in range(n-j-k+1):
            if i+j+k == 0:
                continue
            tmp = n
            if i > 0:
                tmp += DP[i-1][j][k]*i
            if j > 0:
                tmp += DP[i+1][j-1][k]*j
            if k > 0:
                tmp += DP[i][j+1][k-1]*k
            DP[i][j][k] = tmp/(i+j+k)
print(DP[a][b][c])

Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
DP = [[[-1 for k in range(n+1)] for j in range(n+1)] for i in range(n+1)]


def f(i, j, k):
    if DP[i][j][k] != -1:
        return DP[i][j][k]
    if i+j+k == 0:
        return 0
    tmp = 0
    if i > 0:
        tmp += i*f(i-1, j, k)
    if j > 0:
        tmp += j*f(i+1, j-1, k)
    if k > 0:
        tmp += k*f(i, j+1, k-1)
    tmp += n
    tmp /= i+j+k
    DP[i][j][k] = tmp
    return tmp


*A, = map(int, input().split())
i, j, k = A.count(1), A.count(2), A.count(3)
print(f(i, j, k))


Fixed submission:
n = int(input())
*A, = map(int, input().split())
a, b, c = A.count(1), A.count(2), A.count(3)
DP = [[[0.0 for k in range(n+1)] for j in range(n+1)] for i in range(n+1)]
for k in range(c+1):
    for j in range(b+c-k+1):
        for i in range(n-j-k+1):
            if i+j+k == 0:
                continue
            tmp = n
            if i > 0:
                tmp += DP[i-1][j][k]*i
            if j > 0:
                tmp += DP[i+1][j-1][k]*j
            if k > 0:
                tmp += DP[i][j+1][k-1]*k
            DP[i][j][k] = tmp/(i+j+k)
print(DP[a][b][c])

Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
DP = [[[-1 for k in range(n+1)] for j in range(n+1)] for i in range(n+1)]


def f(i, j, k):
    if DP[i][j][k] != -1:
        return DP[i][j][k]
    if i+j+k == 0:
        return 0
    tmp = 0
    if i > 0:
        tmp += i*f(i-1, j, k)
    if j > 0:
        tmp += j*f(i+1, j-1, k)
    if k > 0:
        tmp += k*f(i, j+1, k-1)
    tmp += n
    tmp /= i+j+k
    DP[i][j][k] = tmp
    return tmp


*A, = map(int, input().split())
i, j, k = A.count(1), A.count(2), A.count(3)
print(f(i, j, k))


Fixed submission:
n = int(input())
*A, = map(int, input().split())
a, b, c = A.count(1), A.count(2), A.count(3)
DP = [[[0.0 for k in range(n+1)] for j in range(n+1)] for i in range(n+1)]
for k in range(c+1):
    for j in range(b+c-k+1):
        for i in range(n-j-k+1):
            if i+j+k == 0:
                continue
            tmp = n
            if i > 0:
                tmp += DP[i-1][j][k]*i
            if j > 0:
                tmp += DP[i+1][j-1][k]*j
            if k > 0:
                tmp += DP[i][j+1][k-1]*k
            DP[i][j][k] = tmp/(i+j+k)
print(DP[a][b][c])