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

U = N + 1
inf = 1e18

dp = [[[inf] * U for _ in range(U)] for _ in range(U)]
dp[0][0][0] = 0.0

for c in range(N + 1):
    for b in range(N + 1):
        for a in range(N + 1):
            if a + b + c == 0:
                continue
            prob = 0
            E = N
            if a > 0:
                E += a * dp[a - 1][b][c]
                prob += a
            if b > 0 and a < N:
                E += b * dp[a + 1][b - 1][c]
                prob += b
            if c > 0 and b < N:
                E += c * dp[a][b + 1][c - 1]
                prob += c
            dp[a][b][c] = E / prob

x = C[1]
y = C[2]
z = C[3]
print(dp[x][y][z])

Fixed submission:
from collections import Counter, defaultdict
N = int(input())
A = list(map(int, input().split()))
C = Counter(A)

U = N + 1
inf = 1e18

dp = [[[inf] * U for _ in range(U)] for _ in range(U)]
dp[0][0][0] = 0.0

for c in range(N + 1):
    for b in range(N + 1):
        if c + b > N:
            break
        for a in range(N + 1):
            if a + b + c == 0:
                continue
            if a + b + c > N:
                break
            prob = 0
            E = N
            if a > 0:
                E += a * dp[a - 1][b][c]
                prob += a
            if b > 0 and a < N:
                E += b * dp[a + 1][b - 1][c]
                prob += b
            if c > 0 and b < N:
                E += c * dp[a][b + 1][c - 1]
                prob += c
            dp[a][b][c] = E / prob

x = C[1]
y = C[2]
z = C[3]
print(dp[x][y][z])

Buggy python submission (verdict: Time Limit Exceeded):
import sys
from functools import lru_cache
sys.setrecursionlimit(10 ** 7)

N = int(input())
A = list(map(int, input().split()))

sushi = [0] * 4
for a in A:
    sushi[a] += 1
B, C, D = sushi[1:]


@lru_cache(maxsize=10 ** 6)
def rec(b, c, d):
    if (b, c, d) == (0, 0, 0):
        return 0
    if b < 0 or c < 0 or d < 0:
        return 0

    a = N - b - c - d
    x1 = rec(b - 1, c, d) * b / (N - a)
    x2 = rec(b + 1, c - 1, d) * c / (N - a)
    x3 = rec(b, c + 1, d - 1) * d / (N - a)
    return x1 + x2 + x3 + N / (N - a)


print(rec(B, C, D))

Fixed submission:
from collections import Counter, defaultdict
N = int(input())
A = list(map(int, input().split()))
C = Counter(A)

U = N + 1
inf = 1e18

dp = [[[inf] * U for _ in range(U)] for _ in range(U)]
dp[0][0][0] = 0.0

for c in range(N + 1):
    for b in range(N + 1):
        if c + b > N:
            break
        for a in range(N + 1):
            if a + b + c == 0:
                continue
            if a + b + c > N:
                break
            prob = 0
            E = N
            if a > 0:
                E += a * dp[a - 1][b][c]
                prob += a
            if b > 0 and a < N:
                E += b * dp[a + 1][b - 1][c]
                prob += b
            if c > 0 and b < N:
                E += c * dp[a][b + 1][c - 1]
                prob += c
            dp[a][b][c] = E / prob

x = C[1]
y = C[2]
z = C[3]
print(dp[x][y][z])

Buggy python submission (verdict: Time Limit Exceeded):
import sys
from functools import lru_cache
sys.setrecursionlimit(10 ** 7)

N = int(input())
A = list(map(int, input().split()))

sushi = [0] * 4
for a in A:
    sushi[a] += 1
B, C, D = sushi[1:]


@lru_cache(maxsize=10 ** 6)
def rec(b, c, d):
    if (b, c, d) == (0, 0, 0):
        return 0
    if b < 0 or c < 0 or d < 0:
        return 0

    a = N - b - c - d
    x1 = rec(b - 1, c, d) * b / (N - a)
    x2 = rec(b + 1, c - 1, d) * c / (N - a)
    x3 = rec(b, c + 1, d - 1) * d / (N - a)
    return x1 + x2 + x3 + N / (N - a)


print(rec(B, C, D))

Fixed submission:
from collections import Counter, defaultdict
N = int(input())
A = list(map(int, input().split()))
C = Counter(A)

U = N + 1
inf = 1e18

dp = [[[inf] * U for _ in range(U)] for _ in range(U)]
dp[0][0][0] = 0.0

for c in range(N + 1):
    for b in range(N + 1):
        if c + b > N:
            break
        for a in range(N + 1):
            if a + b + c == 0:
                continue
            if a + b + c > N:
                break
            prob = 0
            E = N
            if a > 0:
                E += a * dp[a - 1][b][c]
                prob += a
            if b > 0 and a < N:
                E += b * dp[a + 1][b - 1][c]
                prob += b
            if c > 0 and b < N:
                E += c * dp[a][b + 1][c - 1]
                prob += c
            dp[a][b][c] = E / prob

x = C[1]
y = C[2]
z = C[3]
print(dp[x][y][z])

Buggy python submission (verdict: Time Limit Exceeded):
import sys
from functools import lru_cache
sys.setrecursionlimit(10 ** 7)

N = int(input())
A = list(map(int, input().split()))

sushi = [0] * 4
for a in A:
    sushi[a] += 1
B, C, D = sushi[1:]


@lru_cache(maxsize=10 ** 6)
def rec(a, b, c, d):
    if (b, c, d) == (0, 0, 0):
        return 0
    if b < 0 or c < 0 or d < 0:
        return 0

    x1 = rec(a + 1, b - 1, c, d) * b / (N - a)
    x2 = rec(a, b + 1, c - 1, d) * c / (N - a)
    x3 = rec(a, b, c + 1, d - 1) * d / (N - a)
    return x1 + x2 + x3 + N / (N - a)


print(rec(0, B, C, D))

Fixed submission:
from collections import Counter, defaultdict
N = int(input())
A = list(map(int, input().split()))
C = Counter(A)

U = N + 1
inf = 1e18

dp = [[[inf] * U for _ in range(U)] for _ in range(U)]
dp[0][0][0] = 0.0

for c in range(N + 1):
    for b in range(N + 1):
        if c + b > N:
            break
        for a in range(N + 1):
            if a + b + c == 0:
                continue
            if a + b + c > N:
                break
            prob = 0
            E = N
            if a > 0:
                E += a * dp[a - 1][b][c]
                prob += a
            if b > 0 and a < N:
                E += b * dp[a + 1][b - 1][c]
                prob += b
            if c > 0 and b < N:
                E += c * dp[a][b + 1][c - 1]
                prob += c
            dp[a][b][c] = E / prob

x = C[1]
y = C[2]
z = C[3]
print(dp[x][y][z])

Buggy python submission (verdict: Time Limit Exceeded):
import collections
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(10 ** 7)

N = int(input())
a = list(map(int, input().split()))
c = collections.Counter(a)
one = c[1]
two = c[2]
three = c[3]

m = 301
memo = [[[-1] * m for _ in range(m)] for _ in range(m)]
memo[0][0][0] = 0.0


def sushi(x, y, z):
    if memo[x][y][z] >= 0:
        return memo[x][y][z]

    res = 0.
    if x > 0:
        res += sushi(x - 1, y, z) * x
    if y > 0:
        res += sushi(x + 1, y - 1, z) * y
    if z > 0:
        res += sushi(x, y + 1, z - 1) * z
    res = (res + N) / (x + y + z)
    memo[x][y][z] = res
    return res


ans = sushi(one, two, three)
print(ans)


Fixed submission:
from collections import Counter, defaultdict
N = int(input())
A = list(map(int, input().split()))
C = Counter(A)

U = N + 1
inf = 1e18

dp = [[[inf] * U for _ in range(U)] for _ in range(U)]
dp[0][0][0] = 0.0

for c in range(N + 1):
    for b in range(N + 1):
        if c + b > N:
            break
        for a in range(N + 1):
            if a + b + c == 0:
                continue
            if a + b + c > N:
                break
            prob = 0
            E = N
            if a > 0:
                E += a * dp[a - 1][b][c]
                prob += a
            if b > 0 and a < N:
                E += b * dp[a + 1][b - 1][c]
                prob += b
            if c > 0 and b < N:
                E += c * dp[a][b + 1][c - 1]
                prob += c
            dp[a][b][c] = E / prob

x = C[1]
y = C[2]
z = C[3]
print(dp[x][y][z])

Buggy python submission (verdict: Time Limit Exceeded):
import collections
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(10 ** 7)

N = int(input())
a = list(map(int, input().split()))
c = collections.Counter(a)
one = c[1]
two = c[2]
three = c[3]

m = 301
memo = [[[-1] * m for _ in range(m)] for _ in range(m)]
memo[0][0][0] = 0.0


def sushi(x, y, z):
    if memo[x][y][z] >= 0:
        return memo[x][y][z]

    res = 0.
    if x > 0:
        res += sushi(x - 1, y, z) * x
    if y > 0:
        res += sushi(x + 1, y - 1, z) * y
    if z > 0:
        res += sushi(x, y + 1, z - 1) * z
    res = (res + N) / (x + y + z)
    memo[x][y][z] = res
    return res


ans = sushi(one, two, three)
print(ans)


Fixed submission:
from collections import Counter, defaultdict
N = int(input())
A = list(map(int, input().split()))
C = Counter(A)

U = N + 1
inf = 1e18

dp = [[[inf] * U for _ in range(U)] for _ in range(U)]
dp[0][0][0] = 0.0

for c in range(N + 1):
    for b in range(N + 1):
        if c + b > N:
            break
        for a in range(N + 1):
            if a + b + c == 0:
                continue
            if a + b + c > N:
                break
            prob = 0
            E = N
            if a > 0:
                E += a * dp[a - 1][b][c]
                prob += a
            if b > 0 and a < N:
                E += b * dp[a + 1][b - 1][c]
                prob += b
            if c > 0 and b < N:
                E += c * dp[a][b + 1][c - 1]
                prob += c
            dp[a][b][c] = E / prob

x = C[1]
y = C[2]
z = C[3]
print(dp[x][y][z])