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


def main():
    sys.setrecursionlimit(10**7)
    readline = sys.stdin.buffer.readline
    N = int(readline())
    A = list(map(int, readline().split()))
    C = Counter(A)

    l, m, n = C[3], C[2], C[1]
    dp = [[[0]*(l+m+n+1) for _ in range(l+m+1)] for _ in range(l+1)]
    seen = [[[False]*(l+m+n+1) for _ in range(l+m+1)] for _ in range(l+1)]
    seen[0][0][0] = True

    def exval(i, j, k):
        if seen[i][j][k]:
            return dp[i][j][k]
        s = N
        if i > 0:
            s += i*exval(i-1, j+1, k)
        if j > 0:
            s += j*exval(i, j-1, k+1)
        if k > 0:
            s += k*exval(i, j, k-1)
        dp[i][j][k] = s / (i+j+k)
        seen[i][j][k] = True
        return dp[i][j][k]


    print(exval(l, m, n))


if __name__ == "__main__":
    main()

Fixed submission:
from collections import Counter
import sys


def main():
    sys.setrecursionlimit(10**6)
    readline = sys.stdin.buffer.readline
    N = int(readline())
    A = list(map(int, readline().split()))
    C = Counter(A)

    l, m, n = C[3], C[2], C[1]
    dp = [[[-1]*(N+1) for _ in range(N+1)] for _ in range(N+1)]
    dp[0][0][0] = 0

    def exval(i, j, k):
        if dp[i][j][k]!=-1:
            return dp[i][j][k]
        s = N
        if i > 0:
            s += i*exval(i-1, j+1, k)
        if j > 0:
            s += j*exval(i, j-1, k+1)
        if k > 0:
            s += k*exval(i, j, k-1)
        dp[i][j][k] = s / (i+j+k)
        return dp[i][j][k]


    print(exval(l, m, n))
    
if __name__ == "__main__":
    main()

Buggy python submission (verdict: Time Limit Exceeded):
from collections import Counter
N = int(input())
A = list(map(int, input().split()))
C = Counter(A)

l, m, n = C[3], C[2], C[1]
dp = [[[0]*(l+m+n+1) for _ in range(l+m+1)] for _ in range(l+1)]
seen = [[[False]*(l+m+n+1) for _ in range(l+m+1)] for _ in range(l+1)]
seen[0][0][0] = True


def exval(i, j, k):
    if seen[i][j][k]:
        return dp[i][j][k]
    s = N/(i+j+k)
    if i > 0:
        s += i*exval(i-1, j+1, k)/(i+j+k)
    if j > 0:
        s += j*exval(i, j-1, k+1)/(i+j+k)
    if k > 0:
        s += k*exval(i, j, k-1)/(i+j+k)
    dp[i][j][k] = s
    seen[i][j][k] = True
    return s


print(exval(l, m, n))


Fixed submission:
from collections import Counter
import sys


def main():
    sys.setrecursionlimit(10**6)
    readline = sys.stdin.buffer.readline
    N = int(readline())
    A = list(map(int, readline().split()))
    C = Counter(A)

    l, m, n = C[3], C[2], C[1]
    dp = [[[-1]*(N+1) for _ in range(N+1)] for _ in range(N+1)]
    dp[0][0][0] = 0

    def exval(i, j, k):
        if dp[i][j][k]!=-1:
            return dp[i][j][k]
        s = N
        if i > 0:
            s += i*exval(i-1, j+1, k)
        if j > 0:
            s += j*exval(i, j-1, k+1)
        if k > 0:
            s += k*exval(i, j, k-1)
        dp[i][j][k] = s / (i+j+k)
        return dp[i][j][k]


    print(exval(l, m, n))
    
if __name__ == "__main__":
    main()

Buggy python submission (verdict: Runtime Error):
from collections import Counter
from functools import lru_cache
N = int(input())
A = list(map(int, input().split()))
C = Counter(A)

@lru_cache(maxsize=10*7)
def exval(l, m, n):
    if -1 in [l, m, n]:
        return 0
    if (l, m, n) == (0, 0, 0):
        return 0
    return (l*exval(l-1, m+1, n) + m*exval(l, m-1, n+1) +
            n*exval(l, m, n-1) + N) / (l+m+n)


l, m, n = C[3], C[2], C[1]
print(exval(l, m, n))


Fixed submission:
from collections import Counter
import sys


def main():
    sys.setrecursionlimit(10**6)
    readline = sys.stdin.buffer.readline
    N = int(readline())
    A = list(map(int, readline().split()))
    C = Counter(A)

    l, m, n = C[3], C[2], C[1]
    dp = [[[-1]*(N+1) for _ in range(N+1)] for _ in range(N+1)]
    dp[0][0][0] = 0

    def exval(i, j, k):
        if dp[i][j][k]!=-1:
            return dp[i][j][k]
        s = N
        if i > 0:
            s += i*exval(i-1, j+1, k)
        if j > 0:
            s += j*exval(i, j-1, k+1)
        if k > 0:
            s += k*exval(i, j, k-1)
        dp[i][j][k] = s / (i+j+k)
        return dp[i][j][k]


    print(exval(l, m, n))
    
if __name__ == "__main__":
    main()

Buggy python submission (verdict: Runtime Error):
from collections import Counter
from functools import lru_cache
N = int(input())
A = list(map(int, input().split()))
C = Counter(A)

@lru_cache(maxsize=10*7)
def exval(l, m, n):
    if -1 in [l, m, n]:
        return 0
    if (l, m, n) == (0, 0, 0):
        return 0
    return (l*exval(l-1, m+1, n) + m*exval(l, m-1, n+1) +
            n*exval(l, m, n-1) + N) / (l+m+n)


l, m, n = C[3], C[2], C[1]
print(exval(l, m, n))


Fixed submission:
from collections import Counter
import sys


def main():
    sys.setrecursionlimit(10**6)
    readline = sys.stdin.buffer.readline
    N = int(readline())
    A = list(map(int, readline().split()))
    C = Counter(A)

    l, m, n = C[3], C[2], C[1]
    dp = [[[-1]*(N+1) for _ in range(N+1)] for _ in range(N+1)]
    dp[0][0][0] = 0

    def exval(i, j, k):
        if dp[i][j][k]!=-1:
            return dp[i][j][k]
        s = N
        if i > 0:
            s += i*exval(i-1, j+1, k)
        if j > 0:
            s += j*exval(i, j-1, k+1)
        if k > 0:
            s += k*exval(i, j, k-1)
        dp[i][j][k] = s / (i+j+k)
        return dp[i][j][k]


    print(exval(l, m, n))
    
if __name__ == "__main__":
    main()