Buggy python submission (verdict: Time Limit Exceeded):
from collections import Counter

def solve():
    N = int(input())
    As = list(map(int, input().split()))

    cnt = Counter(As)

    dp = [[[0]*(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):
            if j+k > N:
                break
            for i in range(N+1):
                if i+j+k > N:
                    break
                expe = 0.0
                if i > 0:
                    expe += i/N * dp[i-1][j][k]
                if j > 0:
                    expe += j/N * dp[i+1][j-1][k]
                if k > 0:
                    expe += k/N * dp[i][j+1][k-1]
                if i+j+k:
                    expe = (expe+1)*N/(i+j+k)
                dp[i][j][k] = expe

    ans = dp[cnt[1]][cnt[2]][cnt[3]]
    print(ans)


solve()


Fixed submission:
from collections import Counter

def solve():
    N = int(input())
    As = list(map(int, input().split()))

    cnt = Counter(As)

    dp = [[[0.0]*(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):
            if j+k > N:
                break
            for i in range(N+1):
                if i+j+k > N:
                    break
                expe = 0.0
                if i > 0:
                    expe += i/N * dp[i-1][j][k]
                if j > 0:
                    expe += j/N * dp[i+1][j-1][k]
                if k > 0:
                    expe += k/N * dp[i][j+1][k-1]
                if i+j+k:
                    expe = (expe+1)*N/(i+j+k)
                dp[i][j][k] = expe

    ans = dp[cnt[1]][cnt[2]][cnt[3]]
    print(ans)


solve()

Buggy python submission (verdict: Time Limit Exceeded):
from collections import Counter
import sys
sys.setrecursionlimit(10**9)

def solve():
    N = int(input())
    As = list(map(int, input().split()))

    cnt = Counter(As)

    N2, N3 = (N+1)*(N+1), (N+1)*(N+1)*(N+1)

    def rec(i, j, k):
        z = i*N2 + j*N + k
        expe = memo[z]
        if expe == -1:
            expe = 0.0
            if i > 0:
                expe += i/N * rec(i-1, j, k)
            if j > 0:
                expe += j/N * rec(i+1, j-1, k)
            if k > 0:
                expe += k/N * rec(i, j+1, k-1)
            if i+j+k:
                expe = (expe+1)*N/(i+j+k)
            memo[z] = expe
        return expe

    memo = [-1] * N3

    ans = rec(cnt[1], cnt[2], cnt[3])
    print(ans)


solve()


Fixed submission:
from collections import Counter

def solve():
    N = int(input())
    As = list(map(int, input().split()))

    cnt = Counter(As)

    dp = [[[0.0]*(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):
            if j+k > N:
                break
            for i in range(N+1):
                if i+j+k > N:
                    break
                expe = 0.0
                if i > 0:
                    expe += i/N * dp[i-1][j][k]
                if j > 0:
                    expe += j/N * dp[i+1][j-1][k]
                if k > 0:
                    expe += k/N * dp[i][j+1][k-1]
                if i+j+k:
                    expe = (expe+1)*N/(i+j+k)
                dp[i][j][k] = expe

    ans = dp[cnt[1]][cnt[2]][cnt[3]]
    print(ans)


solve()

Buggy python submission (verdict: Time Limit Exceeded):
from collections import Counter
import sys
sys.setrecursionlimit(10**9)

def solve():
    N = int(input())
    As = list(map(int, input().split()))

    cnt = Counter(As)

    N2, N3 = (N+1)*(N+1), (N+1)*(N+1)*(N+1)

    def rec(i, j, k):
        z = i*N2 + j*N + k
        expe = memo[z]
        if expe == -1:
            expe = 0.0
            if i > 0:
                expe += i/N * rec(i-1, j, k)
            if j > 0:
                expe += j/N * rec(i+1, j-1, k)
            if k > 0:
                expe += k/N * rec(i, j+1, k-1)
            if i+j+k:
                expe = (expe+1)*N/(i+j+k)
            memo[z] = expe
        return expe

    memo = [-1] * N3

    ans = rec(cnt[1], cnt[2], cnt[3])
    print(ans)


solve()


Fixed submission:
from collections import Counter

def solve():
    N = int(input())
    As = list(map(int, input().split()))

    cnt = Counter(As)

    dp = [[[0.0]*(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):
            if j+k > N:
                break
            for i in range(N+1):
                if i+j+k > N:
                    break
                expe = 0.0
                if i > 0:
                    expe += i/N * dp[i-1][j][k]
                if j > 0:
                    expe += j/N * dp[i+1][j-1][k]
                if k > 0:
                    expe += k/N * dp[i][j+1][k-1]
                if i+j+k:
                    expe = (expe+1)*N/(i+j+k)
                dp[i][j][k] = expe

    ans = dp[cnt[1]][cnt[2]][cnt[3]]
    print(ans)


solve()