Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
a = list(map(int, input().split()))

m = d = t = 0
for i in range(len(a)):
  if a[i] == 1:
    m += 1
  elif a[i] == 2:
    d += 1
  else:
    t += 1

D = [[[0] * (N + 1) for i in range(d + t + 1)] for j in range(t + 1)]
DP = [[[0] * (N + 1) for i in range(d + t + 1)] for j in range(t + 1)]
DP[t][d][m] = 1
su = 0
D[t][d][m] = 1
for i in range(t, -1, -1):
  for j in range(d + t - i, -1, -1):
    for k in range(N - i - j, -1, -1):
      x = DP[i][j][k]
      y = D[i][j][k]
      if x != 0:
        if i + j + k != 0:
          su += y * N /(i + j + k)
        if i >= 1:
          DP[i - 1][j + 1][k] += x
          D[i - 1][j + 1][k] += y * i / (i + j + k)
        if j >= 1: 
          DP[i][j - 1][k + 1] += x
          D[i][j - 1][k + 1] += y * j / (i + j + k)
        if k >= 1:
          DP[i][j][k - 1] += x
          D[i][j][k - 1] += y * k / (i + j + k)

print(su)


Fixed submission:
N = int(input())
a = list(map(int, input().split()))

m = d = t = 0
for i in range(len(a)):
  if a[i] == 1:
    m += 1
  elif a[i] == 2:
    d += 1
  else:
    t += 1

D = [[[0] * (N + 1) for i in range(d + t + 1)] for j in range(t + 1)]
su = 0
D[t][d][m] = 1
for i in range(t, -1, -1):
  for j in range(d + t - i, -1, -1):
    for k in range(N - i - j, -1, -1):
      y = D[i][j][k]
      if i + j + k != 0:
        su += y * N /(i + j + k)
      if i >= 1:
        D[i - 1][j + 1][k] += y * i / (i + j + k)
      if j >= 1: 
        D[i][j - 1][k + 1] += y * j / (i + j + k)
      if k >= 1:
        D[i][j][k - 1] += y * k / (i + j + k)

print(su)

Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
a = list(map(int, input().split()))

m = d = t = 0
for i in range(len(a)):
  if a[i] == 1:
    m += 1
  elif a[i] == 2:
    d += 1
  else:
    t += 1

D = [[[0] * (N + 1) for i in range(d + t + 1)] for j in range(t + 1)]
DP = [[[0] * (N + 1) for i in range(d + t + 1)] for j in range(t + 1)]
DP[t][d][m] = 1
su = 0
D[t][d][m] = 1
for i in range(t, -1, -1):
  for j in range(d + t, -1, -1):
    for k in range(N, -1, -1):
      x = DP[i][j][k]
      y = D[i][j][k]
      if x != 0:
        if i + j + k != 0:
          su += y * N /(i + j + k)
        if i >= 1:
          DP[i - 1][j + 1][k] += x
          D[i - 1][j + 1][k] += y * i / (i + j + k)
        if j >= 1: 
          DP[i][j - 1][k + 1] += x
          D[i][j - 1][k + 1] += y * j / (i + j + k)
        if k >= 1:
          DP[i][j][k - 1] += x
          D[i][j][k - 1] += y * k / (i + j + k)

print(su)


Fixed submission:
N = int(input())
a = list(map(int, input().split()))

m = d = t = 0
for i in range(len(a)):
  if a[i] == 1:
    m += 1
  elif a[i] == 2:
    d += 1
  else:
    t += 1

D = [[[0] * (N + 1) for i in range(d + t + 1)] for j in range(t + 1)]
su = 0
D[t][d][m] = 1
for i in range(t, -1, -1):
  for j in range(d + t - i, -1, -1):
    for k in range(N - i - j, -1, -1):
      y = D[i][j][k]
      if i + j + k != 0:
        su += y * N /(i + j + k)
      if i >= 1:
        D[i - 1][j + 1][k] += y * i / (i + j + k)
      if j >= 1: 
        D[i][j - 1][k + 1] += y * j / (i + j + k)
      if k >= 1:
        D[i][j][k - 1] += y * k / (i + j + k)

print(su)

Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
a = list(map(int, input().split()))

m = d = t = 0
for i in range(len(a)):
  if a[i] == 1:
    m += 1
  elif a[i] == 2:
    d += 1
  else:
    t += 1

M = t * 2 + d + m
D = [[[0] * (M + 1) for i in range(d + t + 1)] for j in range(t + 1)]
DP = [[[0] * (M + 1) for i in range(d + t + 1)] for j in range(t + 1)]
DP[t][d][m] = 1
su = 0
D[t][d][m] = 1
for i in range(t, -1, -1):
  for j in range(d + t, -1, -1):
    for k in range(M, -1, -1):
      x = DP[i][j][k]
      y = D[i][j][k]
      if x != 0:
        if i + j + k != 0:
          su += y * N /(i + j + k)
        if i >= 1:
          DP[i - 1][j + 1][k] += x
          D[i - 1][j + 1][k] += y * i / (i + j + k)
        if j >= 1: 
          DP[i][j - 1][k + 1] += x
          D[i][j - 1][k + 1] += y * j / (i + j + k)
        if k >= 1:
          DP[i][j][k - 1] += x
          D[i][j][k - 1] += y * k / (i + j + k)

print(su)


Fixed submission:
N = int(input())
a = list(map(int, input().split()))

m = d = t = 0
for i in range(len(a)):
  if a[i] == 1:
    m += 1
  elif a[i] == 2:
    d += 1
  else:
    t += 1

D = [[[0] * (N + 1) for i in range(d + t + 1)] for j in range(t + 1)]
su = 0
D[t][d][m] = 1
for i in range(t, -1, -1):
  for j in range(d + t - i, -1, -1):
    for k in range(N - i - j, -1, -1):
      y = D[i][j][k]
      if i + j + k != 0:
        su += y * N /(i + j + k)
      if i >= 1:
        D[i - 1][j + 1][k] += y * i / (i + j + k)
      if j >= 1: 
        D[i][j - 1][k + 1] += y * j / (i + j + k)
      if k >= 1:
        D[i][j][k - 1] += y * k / (i + j + k)

print(su)