Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
As = list(map(int, input().split()))
d = [0]*((N+1)**3)
def encode(i,j,k):
    return i+j*(N+1)+k*((N+1)**2)
B = 0
for a in As:
    B += (N+1)**(a-1)
d[0] = 0
for k in range(N+1):
    for j in range(N+1-k):
        for i in range(N+1-k-j):
            E = 0
            x = encode(i,j,k)
            s0 = i+j+k
            if i:
                E += (i*N)/(s0**2) + d[x-1]*(i/s0)
            if j:
                E += (j*N)/(s0**2) + d[x-N]*(j/s0)
            if k:
                E += (k*N)/(s0**2) + d[x-N**2-N]*(k/s0)
            d[x] = E
print(d[B])


Fixed submission:
N = int(input())
As = list(map(int, input().split()))
d = dict()
def encode(i,j,k):
    return i+j*(N+1)+k*((N+1)**2)
def decode(x):
    return [(x // ((N+1)**i))%(N+1) for  i in range(3)]
B = 0
for a in As:
    B += (N+1)**(a-1)
d[0] = 0
for k in range(N+1):
    for j in range(N+1-k):
        for i in range(N+1-k-j):
            E = 0
            x = encode(i,j,k)
            s0 = N-i-j-k
            if i:
                E += (i*N)/((N-s0)**2) + d[x-1]*(i/(N-s0))
            if j:
                E += (j*N)/((N-s0)**2) + d[x-N]*(j/(N-s0))
            if k:
                E += (k*N)/((N-s0)**2) + d[x-N**2-N]*(k/(N-s0))
            d[x] = E
print(d[B])

Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
As = list(map(int, input().split()))
d = [0]*((N+1)**3)
def encode(i,j,k):
    return i+j*(N+1)+k*((N+1)**2)
B = 0
for a in As:
    B += (N+1)**(a-1)
d[0] = 0
for k in range(N+1):
    for j in range(N+1-k):
        for i in range(N+1-k-j):
            E = 0
            x = encode(i,j,k)
            s0 = i+j+k
            if s0:
                E += (i*N)/(s0**2) + d[x-1]*(i/s0)
                E += (j*N)/(s0**2) + d[x-N]*(j/s0)
                E += (k*N)/(s0**2) + d[x-N**2-N]*(k/s0)
            d[x] = E
print(d[B])

Fixed submission:
N = int(input())
As = list(map(int, input().split()))
d = dict()
def encode(i,j,k):
    return i+j*(N+1)+k*((N+1)**2)
def decode(x):
    return [(x // ((N+1)**i))%(N+1) for  i in range(3)]
B = 0
for a in As:
    B += (N+1)**(a-1)
d[0] = 0
for k in range(N+1):
    for j in range(N+1-k):
        for i in range(N+1-k-j):
            E = 0
            x = encode(i,j,k)
            s0 = N-i-j-k
            if i:
                E += (i*N)/((N-s0)**2) + d[x-1]*(i/(N-s0))
            if j:
                E += (j*N)/((N-s0)**2) + d[x-N]*(j/(N-s0))
            if k:
                E += (k*N)/((N-s0)**2) + d[x-N**2-N]*(k/(N-s0))
            d[x] = E
print(d[B])

Buggy python submission (verdict: Time Limit Exceeded):
import sys
sys.setrecursionlimit(pow(10, 8))
N = int(input())
As = list(map(int, input().split()))
d = dict()
def encode(i,j,k):
    return i+j*(N+1)+k*((N+1)**2)
def decode(x):
    return [(x // ((N+1)**i))%(N+1) for  i in range(3)]
B = 0
for a in As:
    B += (N+1)**(a-1)
d[0] = 0
for x in range((N+1)**3):
    E = 0
    i,j,k = decode(x)
    s0 = N-i-j-k
    if i:
        E += (i*N)/((N-s0)**2) + d[x-1]*(i/(N-s0))
    if j:
        E += (j*N)/((N-s0)**2) + d[x-N]*(j/(N-s0))
    if k:
        E += (k*N)/((N-s0)**2) + d[x-N**2-N]*(k/(N-s0))
    d[x] = E
print(d[B])


Fixed submission:
N = int(input())
As = list(map(int, input().split()))
d = dict()
def encode(i,j,k):
    return i+j*(N+1)+k*((N+1)**2)
def decode(x):
    return [(x // ((N+1)**i))%(N+1) for  i in range(3)]
B = 0
for a in As:
    B += (N+1)**(a-1)
d[0] = 0
for k in range(N+1):
    for j in range(N+1-k):
        for i in range(N+1-k-j):
            E = 0
            x = encode(i,j,k)
            s0 = N-i-j-k
            if i:
                E += (i*N)/((N-s0)**2) + d[x-1]*(i/(N-s0))
            if j:
                E += (j*N)/((N-s0)**2) + d[x-N]*(j/(N-s0))
            if k:
                E += (k*N)/((N-s0)**2) + d[x-N**2-N]*(k/(N-s0))
            d[x] = E
print(d[B])

Buggy python submission (verdict: Time Limit Exceeded):
import sys
sys.setrecursionlimit(pow(10, 8))
N = int(input())
As = list(map(int, input().split()))
d = dict()
def decode(x):
    return [(x // ((N+1)**i))%(N+1) for  i in range(4)]
def f(x):
    if x in d:
        return d[x]
    E = 0
    _x = decode(x)
    for i in range(1, 4):
        if _x[i]:
            E += (_x[i]*N)/((N-_x[0])**2) + f(x - (N+1)**i + (N+1)**(i-1))*(_x[i]/(N-_x[0]))
    d[x] = E
    return E
B = 0
for a in As:
    B += (N+1)**a
print(f(B))


Fixed submission:
N = int(input())
As = list(map(int, input().split()))
d = dict()
def encode(i,j,k):
    return i+j*(N+1)+k*((N+1)**2)
def decode(x):
    return [(x // ((N+1)**i))%(N+1) for  i in range(3)]
B = 0
for a in As:
    B += (N+1)**(a-1)
d[0] = 0
for k in range(N+1):
    for j in range(N+1-k):
        for i in range(N+1-k-j):
            E = 0
            x = encode(i,j,k)
            s0 = N-i-j-k
            if i:
                E += (i*N)/((N-s0)**2) + d[x-1]*(i/(N-s0))
            if j:
                E += (j*N)/((N-s0)**2) + d[x-N]*(j/(N-s0))
            if k:
                E += (k*N)/((N-s0)**2) + d[x-N**2-N]*(k/(N-s0))
            d[x] = E
print(d[B])