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

def calc(one,two,three):
  if dp[one][two][three]!=-1:
    return dp[one][two][three]
  zero=n-one-two-three
  ret=0
  if zero!=n:
    ret=1/(1-zero/n)
  if one!=0:
    ret+=(calc(one-1,two,three)*one/n)/(1-zero/n)
  if two!=0:
    ret+=(calc(one+1,two-1,three)*two/n)/(1-zero/n)
  if three!=0:
    ret+=(calc(one,two+1,three-1)*three/n)/(1-zero/n)
  dp[one][two][three]=ret
  return ret

n=int(input())
arr=list(map(int,input().split()))
dp=[[[-1]*301 for _ in range(301)] for _ in range(301)]
cnt=[0,0,0,0]
for val in arr:
  cnt[val]+=1
calc(cnt[1],cnt[2],cnt[3])
print(dp[cnt[1]][cnt[2]][cnt[3]])

Fixed submission:
import collections

n=int(input())
arr=list(map(int,input().split()))
dp=[[[0]*(n+1) for _ in range(n+1)] for _ in range(n+1)]
cnt=collections.Counter(arr)
for total in range(1,n+1):
  for three in range(total+1):
    for two in range(total-three+1):
      one=total-two-three
      dp[one][two][three]+=n/total
      if one!=0:
        dp[one][two][three]+=dp[one-1][two][three]*one/total
      if two!=0:
        dp[one][two][three]+=dp[one+1][two-1][three]*two/total
      if three!=0:
        dp[one][two][three]+=dp[one][two+1][three-1]*three/total
print(dp[cnt[1]][cnt[2]][cnt[3]])

Buggy python submission (verdict: Time Limit Exceeded):
import collections
import sys
sys.setrecursionlimit(10**6)

def calc(one,two,three):
  if dp[one][two][three]!=-1:
    return dp[one][two][three]
  zero=n-one-two-three
  if zero==n:
    return 0
  ret=n
  if one!=0:
    ret+=calc(one-1,two,three)*one
  if two!=0:
    ret+=calc(one+1,two-1,three)*two
  if three!=0:
    ret+=calc(one,two+1,three-1)*three
  ret/=n-zero
  dp[one][two][three]=ret
  return ret

n=int(input())
arr=list(map(int,input().split()))
dp=[[[-1]*301 for _ in range(301)] for _ in range(301)]
cnt=collections.Counter(arr)
print(calc(cnt[1],cnt[2],cnt[3]))

Fixed submission:
import collections

n=int(input())
arr=list(map(int,input().split()))
dp=[[[0]*(n+1) for _ in range(n+1)] for _ in range(n+1)]
cnt=collections.Counter(arr)
for total in range(1,n+1):
  for three in range(total+1):
    for two in range(total-three+1):
      one=total-two-three
      dp[one][two][three]+=n/total
      if one!=0:
        dp[one][two][three]+=dp[one-1][two][three]*one/total
      if two!=0:
        dp[one][two][three]+=dp[one+1][two-1][three]*two/total
      if three!=0:
        dp[one][two][three]+=dp[one][two+1][three-1]*three/total
print(dp[cnt[1]][cnt[2]][cnt[3]])

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

def calc(one,two,three):
  if dp[one][two][three]!=-1:
    return dp[one][two][three]
  zero=n-one-two-three
  ret=0
  if zero!=n:
    ret=1/(1-zero/n)
  if one!=0:
    ret+=(calc(one-1,two,three)*one/n)/(1-zero/n)
  if two!=0:
    ret+=(calc(one+1,two-1,three)*two/n)/(1-zero/n)
  if three!=0:
    ret+=(calc(one,two+1,three-1)*three/n)/(1-zero/n)
  dp[one][two][three]=ret
  return ret

n=int(input())
arr=list(map(int,input().split()))
dp=[[[-1]*301 for _ in range(301)] for _ in range(301)]
cnt=[0,0,0,0]
for val in arr:
  cnt[val]+=1
calc(cnt[1],cnt[2],cnt[3])
print(dp[cnt[1]][cnt[2]][cnt[3]])

Fixed submission:
import collections

n=int(input())
arr=list(map(int,input().split()))
dp=[[[0]*(n+1) for _ in range(n+1)] for _ in range(n+1)]
cnt=collections.Counter(arr)
for total in range(1,n+1):
  for three in range(total+1):
    for two in range(total-three+1):
      one=total-two-three
      dp[one][two][three]+=n/total
      if one!=0:
        dp[one][two][three]+=dp[one-1][two][three]*one/total
      if two!=0:
        dp[one][two][three]+=dp[one+1][two-1][three]*two/total
      if three!=0:
        dp[one][two][three]+=dp[one][two+1][three-1]*three/total
print(dp[cnt[1]][cnt[2]][cnt[3]])

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

def calc(one,two,three):
  zero=n-one-two-three
  if (one,two,three) in memo:
    return memo[(one,two,three)]
  ret=0
  if zero!=n:
    ret=1/(1-zero/n)
  if one!=0:
    ret+=(calc(one-1,two,three)*one/n)/(1-zero/n)
  if two!=0:
    ret+=(calc(one+1,two-1,three)*two/n)/(1-zero/n)
  if three!=0:
    ret+=(calc(one,two+1,three-1)*three/n)/(1-zero/n)
  memo[(one,two,three)]=ret
  return ret

n=int(input())
arr=list(map(int,input().split()))
memo=collections.defaultdict(int)
cnt=[0,0,0,0]
for val in arr:
  cnt[val]+=1
calc(cnt[1],cnt[2],cnt[3])
print(memo[(cnt[1],cnt[2],cnt[3])])

Fixed submission:
import collections

n=int(input())
arr=list(map(int,input().split()))
dp=[[[0]*(n+1) for _ in range(n+1)] for _ in range(n+1)]
cnt=collections.Counter(arr)
for total in range(1,n+1):
  for three in range(total+1):
    for two in range(total-three+1):
      one=total-two-three
      dp[one][two][three]+=n/total
      if one!=0:
        dp[one][two][three]+=dp[one-1][two][three]*one/total
      if two!=0:
        dp[one][two][three]+=dp[one+1][two-1][three]*two/total
      if three!=0:
        dp[one][two][three]+=dp[one][two+1][three-1]*three/total
print(dp[cnt[1]][cnt[2]][cnt[3]])

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

def calc(zero,one,two,three):
  n=zero+one+two+three
  if (zero,one,two,three) in memo:
    return memo[(zero,one,two,three)]
  ret=0
  if zero!=n:
    ret=1/(1-zero/n)
  if one!=0:
    ret+=(calc(zero+1,one-1,two,three)*one/n)/(1-zero/n)
  if two!=0:
    ret+=(calc(zero,one+1,two-1,three)*two/n)/(1-zero/n)
  if three!=0:
    ret+=(calc(zero,one,two+1,three-1)*three/n)/(1-zero/n)
  memo[(zero,one,two,three)]=ret
  return ret

n=int(input())
arr=list(map(int,input().split()))
memo=collections.defaultdict(int)
cnt=[0,0,0,0]
for val in arr:
  cnt[val]+=1
calc(cnt[0],cnt[1],cnt[2],cnt[3])
print(memo[(cnt[0],cnt[1],cnt[2],cnt[3])])

Fixed submission:
import collections

n=int(input())
arr=list(map(int,input().split()))
dp=[[[0]*(n+1) for _ in range(n+1)] for _ in range(n+1)]
cnt=collections.Counter(arr)
for total in range(1,n+1):
  for three in range(total+1):
    for two in range(total-three+1):
      one=total-two-three
      dp[one][two][three]+=n/total
      if one!=0:
        dp[one][two][three]+=dp[one-1][two][three]*one/total
      if two!=0:
        dp[one][two][three]+=dp[one+1][two-1][three]*two/total
      if three!=0:
        dp[one][two][three]+=dp[one][two+1][three-1]*three/total
print(dp[cnt[1]][cnt[2]][cnt[3]])

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

def calc(zero,one,two,three):
  n=zero+one+two+three
  if (zero,one,two,three) in memo:
    return memo[(zero,one,two,three)]
  ret=0
  if zero!=n:
    ret=1/(1-zero/n)
  if one!=0:
    ret+=(calc(zero+1,one-1,two,three)*one/n)/(1-zero/n)
  if two!=0:
    ret+=(calc(zero,one+1,two-1,three)*two/n)/(1-zero/n)
  if three!=0:
    ret+=(calc(zero,one,two+1,three-1)*three/n)/(1-zero/n)
  memo[(zero,one,two,three)]=ret
  return ret

n=int(input())
arr=list(map(int,input().split()))
memo=collections.defaultdict(int)
cnt=[0,0,0,0]
for val in arr:
  cnt[val]+=1
calc(cnt[0],cnt[1],cnt[2],cnt[3])
print(memo[(cnt[0],cnt[1],cnt[2],cnt[3])])

Fixed submission:
import collections

n=int(input())
arr=list(map(int,input().split()))
dp=[[[0]*(n+1) for _ in range(n+1)] for _ in range(n+1)]
cnt=collections.Counter(arr)
for total in range(1,n+1):
  for three in range(total+1):
    for two in range(total-three+1):
      one=total-two-three
      dp[one][two][three]+=n/total
      if one!=0:
        dp[one][two][three]+=dp[one-1][two][three]*one/total
      if two!=0:
        dp[one][two][three]+=dp[one+1][two-1][three]*two/total
      if three!=0:
        dp[one][two][three]+=dp[one][two+1][three-1]*three/total
print(dp[cnt[1]][cnt[2]][cnt[3]])