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

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

SC=[0,0,0]#1個,2個,3個

for a in A:
    SC[a-1]+=1
SC=tuple(SC)

#N=300
#SC=(N-2,1,1,0)

DAYSDICT=dict()

def days(SC):
    if DAYSDICT.get(SC)!=None:
        return DAYSDICT[SC]

    #if SC[0]<0 or SC[1]<0 or SC[2]<0:
    #    return 0

    k=sum(SC)

    if k==0:
        return 0

    ANS=N
    if SC[0]>0:
        ANS+=days((SC[0]-1,SC[1],SC[2]))*SC[0]
    if SC[1]>0:
        ANS+=days((SC[0]+1,SC[1]-1,SC[2]))*SC[1]
    if SC[2]>0:
        ANS+=days((SC[0],SC[1]+1,SC[2]-1))*SC[2]
        
    DAYSDICT[SC]=ANS/k
    return DAYSDICT[SC]

for k in range(SC[2]+1):
    for j in range(SC[1]+SC[2]+1-k):
        for i in range(SC[0]+SC[1]+SC[2]+1-j-k):
            days((i,j,k))

print(DAYSDICT[SC])

Fixed submission:
import sys
input = sys.stdin.readline

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

SC=[0,0,0]#1個,2個,3個

for a in A:
    SC[a-1]+=1
one,two,three=SC

DAYSDICT=[[[0.0]*301 for j in range(301)] for k in range(301)]
DAYSDICT[0][0][0]=0
def days(one,two,three):
    ANS=N
    if one>0:
        ANS+=DAYSDICT[one-1][two][three]*one
    if two>0:
        ANS+=DAYSDICT[one+1][two-1][three]*two
    if three>0:
        ANS+=DAYSDICT[one][two+1][three-1]*three
        
    DAYSDICT[one][two][three]=ANS/(one+two+three)


for k in range(three+1):
    for j in range(two+three+1-k):
        for i in range(one+two+three+1-j-k):
            if i==j==k==0:
                continue
            days(i,j,k)

print(DAYSDICT[one][two][three])

Buggy python submission (verdict: Time Limit Exceeded):
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**9)

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

SC=[0,0,0]#1個,2個,3個

for a in A:
    SC[a-1]+=1

one,two,three=SC

DAYSLIST=[[[None for k in range(N+1)] for i in range(N+1)] for j in range(N+1)]

def days(one,two,three):
    if one<0 or two<0 or three<0:
        return 0
    if DAYSLIST[one][two][three]!=None:
        return DAYSLIST[one][two][three]
    k=one+two+three

    if k==0:
        return 0

    DAYSLIST[one][two][three]=(days(one-1,two,three)*one+days(one+1,two-1,three)*two+days(one,two+1,three-1)*three+N)/k
    return DAYSLIST[one][two][three]

print(days(one,two,three))

Fixed submission:
import sys
input = sys.stdin.readline

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

SC=[0,0,0]#1個,2個,3個

for a in A:
    SC[a-1]+=1
one,two,three=SC

DAYSDICT=[[[0.0]*301 for j in range(301)] for k in range(301)]
DAYSDICT[0][0][0]=0
def days(one,two,three):
    ANS=N
    if one>0:
        ANS+=DAYSDICT[one-1][two][three]*one
    if two>0:
        ANS+=DAYSDICT[one+1][two-1][three]*two
    if three>0:
        ANS+=DAYSDICT[one][two+1][three-1]*three
        
    DAYSDICT[one][two][three]=ANS/(one+two+three)


for k in range(three+1):
    for j in range(two+three+1-k):
        for i in range(one+two+three+1-j-k):
            if i==j==k==0:
                continue
            days(i,j,k)

print(DAYSDICT[one][two][three])

Buggy python submission (verdict: Time Limit Exceeded):
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**9)

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

SC=[0,0,0]#1個,2個,3個

for a in A:
    SC[a-1]+=1
SC=tuple(SC)

#N=300
#SC=(N-2,1,1,0)

DAYSDICT=dict()

def days(SC):
    if DAYSDICT.get(SC)!=None:
        return DAYSDICT[SC]

    if SC[0]<0 or SC[1]<0 or SC[2]<0:
        return 0

    k=sum(SC)

    if k==0:
        return 0

    DAYSDICT[SC]=(days((SC[0]-1,SC[1],SC[2]))*SC[0]+days((SC[0]+1,SC[1]-1,SC[2]))*SC[1]+days((SC[0],SC[1]+1,SC[2]-1))*SC[2]+N)/k
    return DAYSDICT[SC]

print(days(SC))

Fixed submission:
import sys
input = sys.stdin.readline

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

SC=[0,0,0]#1個,2個,3個

for a in A:
    SC[a-1]+=1
one,two,three=SC

DAYSDICT=[[[0.0]*301 for j in range(301)] for k in range(301)]
DAYSDICT[0][0][0]=0
def days(one,two,three):
    ANS=N
    if one>0:
        ANS+=DAYSDICT[one-1][two][three]*one
    if two>0:
        ANS+=DAYSDICT[one+1][two-1][three]*two
    if three>0:
        ANS+=DAYSDICT[one][two+1][three-1]*three
        
    DAYSDICT[one][two][three]=ANS/(one+two+three)


for k in range(three+1):
    for j in range(two+three+1-k):
        for i in range(one+two+three+1-j-k):
            if i==j==k==0:
                continue
            days(i,j,k)

print(DAYSDICT[one][two][three])

Buggy python submission (verdict: Time Limit Exceeded):
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**9)

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

SC=[0,0,0,0]#0個,1個,2個,3個

for a in A:
    SC[a]+=1
SC=tuple(SC)

DAYSDICT=dict()

def days(SC):
    if DAYSDICT.get(SC)!=None:
        return DAYSDICT[SC]

    if SC[1]<0 or SC[2]<0 or SC[3]<0:
        return 0

    k=SC[1]+SC[2]+SC[3]

    if k==0:
        return 0

    DAYSDICT[SC]=N/k+days((SC[0]+1,SC[1]-1,SC[2],SC[3]))*SC[1]/k+days((SC[0],SC[1]+1,SC[2]-1,SC[3]))*SC[2]/k+days((SC[0],SC[1],SC[2]+1,SC[3]-1))*SC[3]/k
    return DAYSDICT[SC]

print(days(SC))

Fixed submission:
import sys
input = sys.stdin.readline

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

SC=[0,0,0]#1個,2個,3個

for a in A:
    SC[a-1]+=1
one,two,three=SC

DAYSDICT=[[[0.0]*301 for j in range(301)] for k in range(301)]
DAYSDICT[0][0][0]=0
def days(one,two,three):
    ANS=N
    if one>0:
        ANS+=DAYSDICT[one-1][two][three]*one
    if two>0:
        ANS+=DAYSDICT[one+1][two-1][three]*two
    if three>0:
        ANS+=DAYSDICT[one][two+1][three-1]*three
        
    DAYSDICT[one][two][three]=ANS/(one+two+three)


for k in range(three+1):
    for j in range(two+three+1-k):
        for i in range(one+two+three+1-j-k):
            if i==j==k==0:
                continue
            days(i,j,k)

print(DAYSDICT[one][two][three])

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

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

SC=[0,0,0,0]#0個,1個,2個,3個

for a in A:
    SC[a]+=1
SC=tuple(SC)

DAYSDICT=dict()

def days(SC):
    if DAYSDICT.get(SC)!=None:
        return DAYSDICT[SC]

    if SC[1]<0 or SC[2]<0 or SC[3]<0:
        return 0

    k=SC[1]+SC[2]+SC[3]

    if k==0:
        return 0

    DAYSDICT[SC]=(SC[0]+k)/k+days((SC[0]+1,SC[1]-1,SC[2],SC[3]))*SC[1]/k+days((SC[0],SC[1]+1,SC[2]-1,SC[3]))*SC[2]/k+days((SC[0],SC[1],SC[2]+1,SC[3]-1))*SC[3]/k
    return DAYSDICT[SC]


print(days(SC))


Fixed submission:
import sys
input = sys.stdin.readline

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

SC=[0,0,0]#1個,2個,3個

for a in A:
    SC[a-1]+=1
one,two,three=SC

DAYSDICT=[[[0.0]*301 for j in range(301)] for k in range(301)]
DAYSDICT[0][0][0]=0
def days(one,two,three):
    ANS=N
    if one>0:
        ANS+=DAYSDICT[one-1][two][three]*one
    if two>0:
        ANS+=DAYSDICT[one+1][two-1][three]*two
    if three>0:
        ANS+=DAYSDICT[one][two+1][three-1]*three
        
    DAYSDICT[one][two][three]=ANS/(one+two+three)


for k in range(three+1):
    for j in range(two+three+1-k):
        for i in range(one+two+three+1-j-k):
            if i==j==k==0:
                continue
            days(i,j,k)

print(DAYSDICT[one][two][three])

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

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

SC=[0,0,0,0]#0個,1個,2個,3個

for a in A:
    SC[a]+=1
SC=tuple(SC)

DAYSDICT=dict()

def days(SC):
    if DAYSDICT.get(SC)!=None:
        return DAYSDICT[SC]

    if SC[1]<0 or SC[2]<0 or SC[3]<0:
        return 0

    k=SC[1]+SC[2]+SC[3]

    if k==0:
        return 0

    DAYSDICT[SC]=(SC[0]+k)/k+days((SC[0]+1,SC[1]-1,SC[2],SC[3]))*SC[1]/k+days((SC[0],SC[1]+1,SC[2]-1,SC[3]))*SC[2]/k+days((SC[0],SC[1],SC[2]+1,SC[3]-1))*SC[3]/k
    return DAYSDICT[SC]


print(days(SC))

Fixed submission:
import sys
input = sys.stdin.readline

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

SC=[0,0,0]#1個,2個,3個

for a in A:
    SC[a-1]+=1
one,two,three=SC

DAYSDICT=[[[0.0]*301 for j in range(301)] for k in range(301)]
DAYSDICT[0][0][0]=0
def days(one,two,three):
    ANS=N
    if one>0:
        ANS+=DAYSDICT[one-1][two][three]*one
    if two>0:
        ANS+=DAYSDICT[one+1][two-1][three]*two
    if three>0:
        ANS+=DAYSDICT[one][two+1][three-1]*three
        
    DAYSDICT[one][two][three]=ANS/(one+two+three)


for k in range(three+1):
    for j in range(two+three+1-k):
        for i in range(one+two+three+1-j-k):
            if i==j==k==0:
                continue
            days(i,j,k)

print(DAYSDICT[one][two][three])

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

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

SC=[0,0,0,0]#0個,1個,2個,3個

for a in A:
    SC[a]+=1
SC=tuple(SC)

DP=dict()
DP[SC]=[1,0]#遷移確率0,経過日数

def plus(A,B):
    xa,ya=A
    xb,yb=B

    return [xa+xb,(xa*ya+xb*yb)/(xa+xb)]
#print(DP)

for test in range(sum(A)):
    
    NEXT=dict()
    
    for a,b,c,d in DP:
        x,y=DP[(a,b,c,d)]
        

        #print(a,b,c,d,x,y)
        #print(NEXT.get((a+1,b-1,c,d),[0,0]),[x*b/(b+c+d),y+N/(b+c+d)])

        if b>0:
            NEXT[(a+1,b-1,c,d)]=plus(NEXT.get((a+1,b-1,c,d),[0,0]),[x*b/(b+c+d),y+N/(b+c+d)])

        if c>0:
            NEXT[(a,b+1,c-1,d)]=plus(NEXT.get((a,b+1,c-1,d),[0,0]),[x*c/(b+c+d),y+N/(b+c+d)])

        if d>0:
            NEXT[(a,b,c+1,d-1)]=plus(NEXT.get((a,b,c+1,d-1),[0,0]),[x*d/(b+c+d),y+N/(b+c+d)])

        #print(NEXT)

    DP=NEXT

print(list(DP.values())[0][1])


Fixed submission:
import sys
input = sys.stdin.readline

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

SC=[0,0,0]#1個,2個,3個

for a in A:
    SC[a-1]+=1
one,two,three=SC

DAYSDICT=[[[0.0]*301 for j in range(301)] for k in range(301)]
DAYSDICT[0][0][0]=0
def days(one,two,three):
    ANS=N
    if one>0:
        ANS+=DAYSDICT[one-1][two][three]*one
    if two>0:
        ANS+=DAYSDICT[one+1][two-1][three]*two
    if three>0:
        ANS+=DAYSDICT[one][two+1][three-1]*three
        
    DAYSDICT[one][two][three]=ANS/(one+two+three)


for k in range(three+1):
    for j in range(two+three+1-k):
        for i in range(one+two+three+1-j-k):
            if i==j==k==0:
                continue
            days(i,j,k)

print(DAYSDICT[one][two][three])

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

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

SC=[0,0,0,0]#0個,1個,2個,3個

for a in A:
    SC[a]+=1
SC=tuple(SC)

DP=dict()
DP[SC]=[1,0]#遷移確率0,経過日数

def plus(A,B):
    xa,ya=A
    xb,yb=B

    return [xa+xb,(xa*ya+xb*yb)/(xa+xb)]
#print(DP)

for test in range(sum(A)):
    
    NEXT=dict()
    
    for a,b,c,d in DP:
        x,y=DP[(a,b,c,d)]
        

        #print(a,b,c,d,x,y)
        #print(NEXT.get((a+1,b-1,c,d),[0,0]),[x*b/(b+c+d),y+N/(b+c+d)])

        if b>0:
            NEXT[(a+1,b-1,c,d)]=plus(NEXT.get((a+1,b-1,c,d),[0,0]),[x*b/(b+c+d),y+N/(b+c+d)])

        if c>0:
            NEXT[(a,b+1,c-1,d)]=plus(NEXT.get((a,b+1,c-1,d),[0,0]),[x*c/(b+c+d),y+N/(b+c+d)])

        if d>0:
            NEXT[(a,b,c+1,d-1)]=plus(NEXT.get((a,b,c+1,d-1),[0,0]),[x*d/(b+c+d),y+N/(b+c+d)])

        #print(NEXT)

    DP=NEXT

print(list(DP.values())[0][1])


Fixed submission:
import sys
input = sys.stdin.readline

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

SC=[0,0,0]#1個,2個,3個

for a in A:
    SC[a-1]+=1
one,two,three=SC

DAYSDICT=[[[0.0]*301 for j in range(301)] for k in range(301)]
DAYSDICT[0][0][0]=0
def days(one,two,three):
    ANS=N
    if one>0:
        ANS+=DAYSDICT[one-1][two][three]*one
    if two>0:
        ANS+=DAYSDICT[one+1][two-1][three]*two
    if three>0:
        ANS+=DAYSDICT[one][two+1][three-1]*three
        
    DAYSDICT[one][two][three]=ANS/(one+two+three)


for k in range(three+1):
    for j in range(two+three+1-k):
        for i in range(one+two+three+1-j-k):
            if i==j==k==0:
                continue
            days(i,j,k)

print(DAYSDICT[one][two][three])

Buggy python submission (verdict: Time Limit Exceeded):
import sys
import copy

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

SC=[0,0,0,0]#0個,1個,2個,3個

for a in A:
    SC[a]+=1
SC=tuple(SC)

DP=dict()
DP[SC]=[1,0]#遷移確率0,経過日数

def plus(A,B):
    xa,ya=A
    xb,yb=B

    x=xa+xb
    y=(xa*ya+xb*yb)/(xa+xb)
    return [x,y]
#print(DP)
while True:
    NEXT=dict()
    
    for a,b,c,d in DP:
        x,y=DP[(a,b,c,d)]
        

        #print(a,b,c,d,x,y)
        #print(NEXT.get((a+1,b-1,c,d),[0,0]),[x*b/(b+c+d),y+N/(b+c+d)])

        if b>0:
            NEXT[(a+1,b-1,c,d)]=plus(NEXT.get((a+1,b-1,c,d),[0,0]),[x*b/(b+c+d),y+N/(b+c+d)])

        if c>0:
            NEXT[(a,b+1,c-1,d)]=plus(NEXT.get((a,b+1,c-1,d),[0,0]),[x*c/(b+c+d),y+N/(b+c+d)])

        if d>0:
            NEXT[(a,b,c+1,d-1)]=plus(NEXT.get((a,b,c+1,d-1),[0,0]),[x*d/(b+c+d),y+N/(b+c+d)])

        #print(NEXT)

    DP=copy.deepcopy(NEXT)

    #print(DP)

    if len(DP)==1:
        a,b,c,d=list(DP.keys())[0]
        if b==c==d==0:
            print(DP[(a,b,c,d)][1])
            sys.exit()


Fixed submission:
import sys
input = sys.stdin.readline

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

SC=[0,0,0]#1個,2個,3個

for a in A:
    SC[a-1]+=1
one,two,three=SC

DAYSDICT=[[[0.0]*301 for j in range(301)] for k in range(301)]
DAYSDICT[0][0][0]=0
def days(one,two,three):
    ANS=N
    if one>0:
        ANS+=DAYSDICT[one-1][two][three]*one
    if two>0:
        ANS+=DAYSDICT[one+1][two-1][three]*two
    if three>0:
        ANS+=DAYSDICT[one][two+1][three-1]*three
        
    DAYSDICT[one][two][three]=ANS/(one+two+three)


for k in range(three+1):
    for j in range(two+three+1-k):
        for i in range(one+two+three+1-j-k):
            if i==j==k==0:
                continue
            days(i,j,k)

print(DAYSDICT[one][two][three])

Buggy python submission (verdict: Time Limit Exceeded):
import sys
import copy

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

SC=[0,0,0,0]#0個,1個,2個,3個

for a in A:
    SC[a]+=1
SC=tuple(SC)

DP=dict()
DP[SC]=[1,0]#遷移確率0,経過日数

def plus(A,B):
    xa,ya=A
    xb,yb=B

    x=xa+xb
    y=(xa*ya+xb*yb)/(xa+xb)
    return [x,y]
#print(DP)
while True:
    NEXT=dict()
    
    for a,b,c,d in DP:
        x,y=DP[(a,b,c,d)]
        

        #print(a,b,c,d,x,y)
        #print(NEXT.get((a+1,b-1,c,d),[0,0]),[x*b/(b+c+d),y+N/(b+c+d)])

        if b>0:
            NEXT[(a+1,b-1,c,d)]=plus(NEXT.get((a+1,b-1,c,d),[0,0]),[x*b/(b+c+d),y+N/(b+c+d)])

        if c>0:
            NEXT[(a,b+1,c-1,d)]=plus(NEXT.get((a,b+1,c-1,d),[0,0]),[x*c/(b+c+d),y+N/(b+c+d)])

        if d>0:
            NEXT[(a,b,c+1,d-1)]=plus(NEXT.get((a,b,c+1,d-1),[0,0]),[x*d/(b+c+d),y+N/(b+c+d)])

        #print(NEXT)

    DP=copy.deepcopy(NEXT)

    #print(DP)

    if len(DP)==1:
        a,b,c,d=list(DP.keys())[0]
        if b==c==d==0:
            print(DP[(a,b,c,d)][1])
            sys.exit()


Fixed submission:
import sys
input = sys.stdin.readline

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

SC=[0,0,0]#1個,2個,3個

for a in A:
    SC[a-1]+=1
one,two,three=SC

DAYSDICT=[[[0.0]*301 for j in range(301)] for k in range(301)]
DAYSDICT[0][0][0]=0
def days(one,two,three):
    ANS=N
    if one>0:
        ANS+=DAYSDICT[one-1][two][three]*one
    if two>0:
        ANS+=DAYSDICT[one+1][two-1][three]*two
    if three>0:
        ANS+=DAYSDICT[one][two+1][three-1]*three
        
    DAYSDICT[one][two][three]=ANS/(one+two+three)


for k in range(three+1):
    for j in range(two+three+1-k):
        for i in range(one+two+three+1-j-k):
            if i==j==k==0:
                continue
            days(i,j,k)

print(DAYSDICT[one][two][three])