Buggy python submission (verdict: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
double memo[305][305][305];
int n;

double dp(int i, int j, int k)
{
    if (i==0 && j==0 && k ==0) return 0.0;
    if (memo[i][j][k]!=-1) return memo[i][j][k];

    double res = 0;
    if (i>0) res += dp(i-1,j+1,k)*i;
    if (j>0) res += dp(i,j-1,k+1)*j;
    if (k>0) res += dp(i,j,k-1)*k;
    res += n;
    res *= 1.0/(i+j+k);
    return memo[i][j][k] = res;
}
signed main() {
    cin >>n;
    int cnt[5];
    for(int i = 0; i < n; i++)
    {
        int a;cin >>a;
        cnt[a]++;
    }
    for(int i = 0; i < 303; i++)
    {
        for(int j = 0; j < 303; j++)
        {
            for(int k = 0; k < 303; k++)
            {
                memo[i][j][k] = -1.0;
            }
            
        }
        
    }
    
    cout << fixed;
    cout << std::setprecision(10) << dp(cnt[3],cnt[2],cnt[1])<<endl;
    
    return 0;
}

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

dp = [[[0 for _ in range(301)]for __ in range(301)]for ___ in range(301)]

for i in range(n+1):
    for j in range(n+1):
        for k in range(n+1):
            cnt = i + j + k
            if cnt == 0 or cnt > n :continue
            if i == 0 and j ==0 and k ==1:
                dp[i][j][k] = 1
                continue
            if k > 0:
                dp[i][j][k] += (1+dp[i][j][k-1] * cnt/(cnt-1))*k/cnt
            if j > 0:
                dp[i][j][k] += (1+dp[i][j-1][k+1])*j/cnt
            if i > 0:
                dp[i][j][k] += (1+dp[i-1][j+1][k])*i/cnt 
            
print(dp[a.count(3)][a.count(2)][a.count(1)])

Buggy python submission (verdict: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
double memo[305][305][305];
int n;

double dp(int i, int j, int k)
{
    if (i==0 && j==0 && k ==0) return 0.0;
    if (memo[i][j][k]!=-1) return memo[i][j][k];

    double res = 0;
    if (i>0) res += dp(i-1,j+1,k)*i;
    if (j>0) res += dp(i,j-1,k+1)*j;
    if (k>0) res += dp(i,j,k-1)*k;
    res += n;
    res *= 1.0/(i+j+k);
    return res;
}
signed main() {
    cin >>n;
    int cnt[5];
    for(int i = 0; i < n; i++)
    {
        int a;cin >>a;
        cnt[a]++;
    }
    for(int i = 0; i < 303; i++)
    {
        for(int j = 0; j < 303; j++)
        {
            for(int k = 0; k < 303; k++)
            {
                memo[i][j][k] = -1.;
            }
            
        }
        
    }
    
    cout << fixed;
    cout << std::setprecision(10) << dp(cnt[3],cnt[2],cnt[1])<<endl;
    
    return 0;
}

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

dp = [[[0 for _ in range(301)]for __ in range(301)]for ___ in range(301)]

for i in range(n+1):
    for j in range(n+1):
        for k in range(n+1):
            cnt = i + j + k
            if cnt == 0 or cnt > n :continue
            if i == 0 and j ==0 and k ==1:
                dp[i][j][k] = 1
                continue
            if k > 0:
                dp[i][j][k] += (1+dp[i][j][k-1] * cnt/(cnt-1))*k/cnt
            if j > 0:
                dp[i][j][k] += (1+dp[i][j-1][k+1])*j/cnt
            if i > 0:
                dp[i][j][k] += (1+dp[i-1][j+1][k])*i/cnt 
            
print(dp[a.count(3)][a.count(2)][a.count(1)])

Buggy python submission (verdict: Time Limit Exceeded):
n=int(input())
a=list(map(int,input().split()))
cnt = [0]*5
for i in range(n):
    cnt[a[i]] += 1
maxn = 301
memo = [[[-1]*maxn for i in range(maxn)]for j in range(maxn)]
def dp(i,j,k):
    if i==0 and j==0 and k==0:
        return 0.0
    
    if memo[i][j][k] != -1:
        return memo[i][j][k]

    res = 0
    if i>0:
        res += i*dp(i-1,j+1,k)
    if j>0:
        res += j*dp(i,j-1,k+1)
    if k>0:
        res += k*dp(i,j,k-1)
    res += n
    res /= (i+j+k)
    memo[i][j][k]=res
    return res

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

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

dp = [[[0 for _ in range(301)]for __ in range(301)]for ___ in range(301)]

for i in range(n+1):
    for j in range(n+1):
        for k in range(n+1):
            cnt = i + j + k
            if cnt == 0 or cnt > n :continue
            if i == 0 and j ==0 and k ==1:
                dp[i][j][k] = 1
                continue
            if k > 0:
                dp[i][j][k] += (1+dp[i][j][k-1] * cnt/(cnt-1))*k/cnt
            if j > 0:
                dp[i][j][k] += (1+dp[i][j-1][k+1])*j/cnt
            if i > 0:
                dp[i][j][k] += (1+dp[i-1][j+1][k])*i/cnt 
            
print(dp[a.count(3)][a.count(2)][a.count(1)])