Buggy python submission (verdict: Runtime Error):
"""
https://atcoder.jp/contests/dp/tasks/dp_j
"""
from sys import stdin
from collections import defaultdict
input = stdin.readline

n = int(input())
c = [0] * 4
l = list(map(int, input().split()))

for i in l:
    c[i] += 1

# dp = defaultdict(int)
# dp[(0, 0, 0)] = 0
dp = [[[0] * (302) for i in range(302)] for j in range(302)]
# dp[0][0][0] = 0
for n3 in range(0, c[3] + 1):
    for n2 in range(0, c[2] + c[3] - n3 + 1):
        for n1 in range(0, n - c[0] - n2 - n3 + 1):
            if n1 or n2 or n3:
                dp[n1 + 1][n2 + 1][
                    n3 + 1] = (n + n1 * dp[n1][n2 + 1][n3 + 1] +
                               n2 * dp[n1 + 2][n2][n3 + 1] +
                               n3 * dp[n1 + 1][n2 + 2][n3]) / (n1 + n2 + n3)

print(dp[c[1] + 1][c[2] + 1][c[3] + 1])

Fixed submission:
"""
https://atcoder.jp/contests/dp/tasks/dp_j
"""
from sys import stdin
input = stdin.readline

n = int(input())
c = [0] * 4
l = list(map(int, input().split()))

for i in l:
    c[i] += 1

dp = [[[0.0] * (n + 1) for i in range(n + 1)] for j in range(n + 1)]
dp[0][0][0] = 0
for n3 in range(0, c[3] + 1):
    for n2 in range(0, c[2] + c[3] - n3 + 1):
        for n1 in range(0, n-c[0]- n3 - n2 + 1):
            if n1 == n2 == n3 == 0:
                continue
            temp = n
            if n1 != 0:
                # temp += n1 * dp[(n1 - 1, n2, n3)]
                temp += n1*dp[n1 - 1][n2][n3]
            if n2 != 0:
                # temp += n2 * dp[(n1 + 1, n2 - 1, n3)]
                temp += n2*dp[n1 + 1][n2 - 1][n3]
            if n3 != 0:
                # temp += n3 * dp[(n1, n2 + 1, n3 - 1)]
                temp += n3*dp[n1][n2 + 1][n3 - 1]
            dp[n1][n2][n3] = temp / (n1 + n2 + n3)

print(dp[c[1]][c[2]][c[3]])

Buggy python submission (verdict: Time Limit Exceeded):
"""
https://atcoder.jp/contests/dp/tasks/dp_j
"""
from sys import stdin
from collections import defaultdict
input = stdin.readline

n = int(input())
c = [0] * 4
l = list(map(int, input().split()))

for i in l:
    c[i] += 1

dp = defaultdict(int)
dp[(0, 0, 0)] = 0
for n3 in range(0, c[3] + 1):
    for n2 in range(0, c[2] + c[3] - n3 + 1):
        for n1 in range(0, n - c[0] - n2 - n3 + 1):
            s = n1 + n2 + n3
            if s != 0:
                temp = n
                if n1 != 0:
                    temp += n1 * dp[(n1 - 1, n2, n3)]
                if n2 != 0:
                    temp += n2 * dp[(n1 + 1, n2 - 1, n3)]
                if n3 != 0:
                    temp += n3 * dp[(n1, n2 + 1, n3 - 1)]
                temp /= s
                dp[(n1, n2, n3)] = temp

print(dp[(c[1], c[2], c[3])])


Fixed submission:
"""
https://atcoder.jp/contests/dp/tasks/dp_j
"""
from sys import stdin
input = stdin.readline

n = int(input())
c = [0] * 4
l = list(map(int, input().split()))

for i in l:
    c[i] += 1

dp = [[[0.0] * (n + 1) for i in range(n + 1)] for j in range(n + 1)]
dp[0][0][0] = 0
for n3 in range(0, c[3] + 1):
    for n2 in range(0, c[2] + c[3] - n3 + 1):
        for n1 in range(0, n-c[0]- n3 - n2 + 1):
            if n1 == n2 == n3 == 0:
                continue
            temp = n
            if n1 != 0:
                # temp += n1 * dp[(n1 - 1, n2, n3)]
                temp += n1*dp[n1 - 1][n2][n3]
            if n2 != 0:
                # temp += n2 * dp[(n1 + 1, n2 - 1, n3)]
                temp += n2*dp[n1 + 1][n2 - 1][n3]
            if n3 != 0:
                # temp += n3 * dp[(n1, n2 + 1, n3 - 1)]
                temp += n3*dp[n1][n2 + 1][n3 - 1]
            dp[n1][n2][n3] = temp / (n1 + n2 + n3)

print(dp[c[1]][c[2]][c[3]])

Buggy python submission (verdict: Time Limit Exceeded):
from sys import stdin, setrecursionlimit
input = stdin.readline

setrecursionlimit(200000)
n = int(input())
c = [0] * 4
l = list(map(int, input().split()))

for i in l:
    c[i] += 1

mem = {}


def dp(n3, n2, n1, n0):
    if n0 == n:
        return 0
    if not mem.get((n3, n2, n1, n0), None):
        ans = n
        if n3 > 0:
            ans += n3 * dp(n3 - 1, n2 + 1, n1, n0)
        if n2 > 0:
            ans += n2 * dp(n3, n2 - 1, n1 + 1, n0)
        if n1 > 0:
            ans += n1 * dp(n3, n2, n1 - 1, n0 + 1)
        ans /= n - n0
        mem[(n3, n2, n1, n0)] = ans
    return mem[(n3, n2, n1, n0)]


print(dp(*c[::-1]))

Fixed submission:
"""
https://atcoder.jp/contests/dp/tasks/dp_j
"""
from sys import stdin
input = stdin.readline

n = int(input())
c = [0] * 4
l = list(map(int, input().split()))

for i in l:
    c[i] += 1

dp = [[[0.0] * (n + 1) for i in range(n + 1)] for j in range(n + 1)]
dp[0][0][0] = 0
for n3 in range(0, c[3] + 1):
    for n2 in range(0, c[2] + c[3] - n3 + 1):
        for n1 in range(0, n-c[0]- n3 - n2 + 1):
            if n1 == n2 == n3 == 0:
                continue
            temp = n
            if n1 != 0:
                # temp += n1 * dp[(n1 - 1, n2, n3)]
                temp += n1*dp[n1 - 1][n2][n3]
            if n2 != 0:
                # temp += n2 * dp[(n1 + 1, n2 - 1, n3)]
                temp += n2*dp[n1 + 1][n2 - 1][n3]
            if n3 != 0:
                # temp += n3 * dp[(n1, n2 + 1, n3 - 1)]
                temp += n3*dp[n1][n2 + 1][n3 - 1]
            dp[n1][n2][n3] = temp / (n1 + n2 + n3)

print(dp[c[1]][c[2]][c[3]])

Buggy python submission (verdict: Time Limit Exceeded):
from sys import stdin, setrecursionlimit
from functools import lru_cache
input = stdin.readline

setrecursionlimit(200000)
n = int(input())
c = [0] * 4
l = list(map(int, input().split()))

for i in l:
    c[i] += 1


# dp = []
@lru_cache(None)
def dp(n3, n2, n1, n0):
    if n0 == n:
        return 0
    else:
        ans = n
        if n3 > 0:
            ans += n3 * dp(n3 - 1, n2 + 1, n1, n0)
        if n2 > 0:
            ans += n2 * dp(n3, n2 - 1, n1 + 1, n0)
        if n1 > 0:
            ans += n1 * dp(n3, n2, n1 - 1, n0 + 1)
        ans /= n - n0
        return ans


print(dp(*c[::-1]))

Fixed submission:
"""
https://atcoder.jp/contests/dp/tasks/dp_j
"""
from sys import stdin
input = stdin.readline

n = int(input())
c = [0] * 4
l = list(map(int, input().split()))

for i in l:
    c[i] += 1

dp = [[[0.0] * (n + 1) for i in range(n + 1)] for j in range(n + 1)]
dp[0][0][0] = 0
for n3 in range(0, c[3] + 1):
    for n2 in range(0, c[2] + c[3] - n3 + 1):
        for n1 in range(0, n-c[0]- n3 - n2 + 1):
            if n1 == n2 == n3 == 0:
                continue
            temp = n
            if n1 != 0:
                # temp += n1 * dp[(n1 - 1, n2, n3)]
                temp += n1*dp[n1 - 1][n2][n3]
            if n2 != 0:
                # temp += n2 * dp[(n1 + 1, n2 - 1, n3)]
                temp += n2*dp[n1 + 1][n2 - 1][n3]
            if n3 != 0:
                # temp += n3 * dp[(n1, n2 + 1, n3 - 1)]
                temp += n3*dp[n1][n2 + 1][n3 - 1]
            dp[n1][n2][n3] = temp / (n1 + n2 + n3)

print(dp[c[1]][c[2]][c[3]])