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


N = int(input())
memo = [[[-1]*(N+1) for _ in range(N+1)] for _ in range(N+1)]

def dp(a, b, c):
    if a == b == c == 0: return 0
    if memo[a][b][c] != -1:
        return memo[a][b][c]
    
    res = N
    if a > 0: res += a*dp(a-1, b, c)
    if b > 0: res += b*dp(a+1, b-1, c)
    if c > 0: res += c*dp(a, b+1, c-1)
    res /= a+b+c
    
    memo[a][b][c] = res
    return res

def main():
    A = list(map(int, input().split()))
    
    count = Counter(A)
    for i in range(1, 4):
        if not i in count: count[i] = 0
    x, y, z = count[1], count[2], count[3]

    
    ans = dp(x, y, z)
    
    print(ans)
        

    
if __name__ == "__main__":
    main()



Fixed submission:
import sys
from collections import Counter
input = sys.stdin.readline


N = int(input())
memo = [[[-1]*(N+1) for _ in range(N+1)] for _ in range(N+1)]

def dp(a, b, c):
    if a == b == c == 0: return 0
    if memo[a][b][c] != -1:
        return memo[a][b][c]
    
    res = N
    if a > 0: res += a*dp(a-1, b, c)
    if b > 0: res += b*dp(a+1, b-1, c)
    if c > 0: res += c*dp(a, b+1, c-1)
    res /= a+b+c
    
    memo[a][b][c] = res
    return res

def main():
    A = list(map(int, input().split()))
    
    count = Counter(A)
    for i in range(1, 4):
        if not i in count: count[i] = 0
    x, y, z = count[1], count[2], count[3]

    
    ans = dp(x, y, z)
    
    print(ans)
        

    
if __name__ == "__main__":
    main()

Buggy python submission (verdict: Runtime Error):
#include <bits/stdc++.h>
#define _GLIBCXX_DEBUG
#define rep(i, n) for (int i = 0; i < n; i++)
#define repr(i, n) for (int i = n; i >= 0; i--)
#define reps(i, m, n) for (int i = m; i < n; i++)
#define all(v) v.begin(), v.end()
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;

map<ll, double> memo;

double dp(int a, int b, int c, int n){
    if ((a == 0) && (b == 0) && (c == 0)) return 0.0;
    if ((a < 0) || (b < 0) || (c < 0)) return 0.0;
    if ((a > n) || (b > n) || (c > n)) return 0.0;
    ll judge = a*1000000+b*1000+c;
    if (memo[judge] != 0) return memo[judge];
    double res = n;
    res += a*dp(a-1, b, c, n);
    res += b*dp(a+1, b-1, c, n);
    res += c*dp(a, b+1, c-1, n);
    res /= a+b+c;
    memo[judge] = res;
    return res;
}

int main(void){
    int n;
    scanf("%d", &n);
    int a = 0;
    int b = 0;
    int c = 0;
    int sub;
    rep(i, n){
        scanf("%d", &sub);
        if (sub == 1) a++;
        if (sub == 2) b++;
        if (sub == 3) c++;
    }
    
    double ans;
    ans = dp(a, b, c, n);
    
    printf("%.11lf", ans);
    
    return 0;
}




Fixed submission:
import sys
from collections import Counter
input = sys.stdin.readline


N = int(input())
memo = [[[-1]*(N+1) for _ in range(N+1)] for _ in range(N+1)]

def dp(a, b, c):
    if a == b == c == 0: return 0
    if memo[a][b][c] != -1:
        return memo[a][b][c]
    
    res = N
    if a > 0: res += a*dp(a-1, b, c)
    if b > 0: res += b*dp(a+1, b-1, c)
    if c > 0: res += c*dp(a, b+1, c-1)
    res /= a+b+c
    
    memo[a][b][c] = res
    return res

def main():
    A = list(map(int, input().split()))
    
    count = Counter(A)
    for i in range(1, 4):
        if not i in count: count[i] = 0
    x, y, z = count[1], count[2], count[3]

    
    ans = dp(x, y, z)
    
    print(ans)
        

    
if __name__ == "__main__":
    main()

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


def main():
    N = int(input())
    A = list(map(int, input().split()))
    
    count = Counter(A)
    for i in range(1, 4):
        if not i in count: count[i] = 0
    x, y, z = count[1], count[2], count[3]
    
    memo = [[[-1]*(N+1) for _ in range(N+1)] for _ in range(N+1)]
    
    def dp(a, b, c):
        
        if a == b == c == 0:
            return 0
        
        if a < 0 or b < 0 or c < 0:
            return 0
        
        if a > N or b > N or c > N:
            return 0
        
        if memo[a][b][c] != -1:
            return memo[a][b][c]
        
        res = N
        res += a*dp(a-1, b, c)
        res += b*dp(a+1, b-1, c)
        res += c*dp(a, b+1, c-1)
        res /= a+b+c
        
        memo[a][b][c] = res
        return res
    
    ans = dp(x, y, z)
    
    print(ans)
        
        
    
    
if __name__ == "__main__":
    main()


Fixed submission:
import sys
from collections import Counter
input = sys.stdin.readline


N = int(input())
memo = [[[-1]*(N+1) for _ in range(N+1)] for _ in range(N+1)]

def dp(a, b, c):
    if a == b == c == 0: return 0
    if memo[a][b][c] != -1:
        return memo[a][b][c]
    
    res = N
    if a > 0: res += a*dp(a-1, b, c)
    if b > 0: res += b*dp(a+1, b-1, c)
    if c > 0: res += c*dp(a, b+1, c-1)
    res /= a+b+c
    
    memo[a][b][c] = res
    return res

def main():
    A = list(map(int, input().split()))
    
    count = Counter(A)
    for i in range(1, 4):
        if not i in count: count[i] = 0
    x, y, z = count[1], count[2], count[3]

    
    ans = dp(x, y, z)
    
    print(ans)
        

    
if __name__ == "__main__":
    main()

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


def main():
    N = int(input())
    A = list(map(int, input().split()))
    
    count = Counter(A)
    for i in range(1, 4):
        if not i in count: count[i] = 0
    x, y, z = count[1], count[2], count[3]
    
    memo = [[[-1]*(N+1) for _ in range(N+1)] for _ in range(N+1)]
    
    def dp(a, b, c):
        
        if a == b == c == 0:
            return 0
        
        if a < 0 or b < 0 or c < 0:
            return 0
        
        if a > N or b > N or c > N:
            return 0
        
        if memo[a][b][c] != -1:
            return memo[a][b][c]
        
        res = N
        res += a*dp(a-1, b, c)
        res += b*dp(a+1, b-1, c)
        res += c*dp(a, b+1, c-1)
        res /= a+b+c
        
        memo[a][b][c] = res
        return res
    
    ans = dp(x, y, z)
    
    print(ans)
        
        
    
    
if __name__ == "__main__":
    main()


Fixed submission:
import sys
from collections import Counter
input = sys.stdin.readline


N = int(input())
memo = [[[-1]*(N+1) for _ in range(N+1)] for _ in range(N+1)]

def dp(a, b, c):
    if a == b == c == 0: return 0
    if memo[a][b][c] != -1:
        return memo[a][b][c]
    
    res = N
    if a > 0: res += a*dp(a-1, b, c)
    if b > 0: res += b*dp(a+1, b-1, c)
    if c > 0: res += c*dp(a, b+1, c-1)
    res /= a+b+c
    
    memo[a][b][c] = res
    return res

def main():
    A = list(map(int, input().split()))
    
    count = Counter(A)
    for i in range(1, 4):
        if not i in count: count[i] = 0
    x, y, z = count[1], count[2], count[3]

    
    ans = dp(x, y, z)
    
    print(ans)
        

    
if __name__ == "__main__":
    main()

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


def main():
    N = int(input())
    A = list(map(int, input().split()))
    
    count = Counter(A)
    for i in range(1, 4):
        if not i in count: count[i] = 0
    x, y, z = count[1], count[2], count[3]
    
    memo = dict()
    
    def dp(a, b, c):
        if (a, b, c) in memo:
            return memo[(a, b, c)]
        
        if a == b == c == 0:
            return 0
        
        if a < 0 or b < 0 or c < 0:
            return 0
        
        if a + b + c > N:
            return 0
        
        res = N
        res += a*dp(a-1, b, c)
        res += b*dp(a+1, b-1, c)
        res += c*dp(a, b+1, c-1)
        res /= a+b+c
        
        memo[(a, b, c)] = res
        return res
    
    ans = dp(x, y, z)
    
    print(ans)
        
        
    
    
if __name__ == "__main__":
    main()


Fixed submission:
import sys
from collections import Counter
input = sys.stdin.readline


N = int(input())
memo = [[[-1]*(N+1) for _ in range(N+1)] for _ in range(N+1)]

def dp(a, b, c):
    if a == b == c == 0: return 0
    if memo[a][b][c] != -1:
        return memo[a][b][c]
    
    res = N
    if a > 0: res += a*dp(a-1, b, c)
    if b > 0: res += b*dp(a+1, b-1, c)
    if c > 0: res += c*dp(a, b+1, c-1)
    res /= a+b+c
    
    memo[a][b][c] = res
    return res

def main():
    A = list(map(int, input().split()))
    
    count = Counter(A)
    for i in range(1, 4):
        if not i in count: count[i] = 0
    x, y, z = count[1], count[2], count[3]

    
    ans = dp(x, y, z)
    
    print(ans)
        

    
if __name__ == "__main__":
    main()

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


def main():
    N = int(input())
    A = list(map(int, input().split()))
    
    count = Counter(A)
    for i in range(1, 4):
        if not i in count: count[i] = 0
    a, b, c = count[1], count[2], count[3]
    
    memo = dict()
    
    def dp(a, b, c):
        if (a, b, c) in memo:
            return memo[(a, b, c)]
        
        if a == b == c == 0:
            return 0
        
        if a < 0 or b < 0 or c < 0:
            return 0
        
        res = N
        res += a*dp(a-1, b, c)
        res += b*dp(a+1, b-1, c)
        res += c*dp(a, b+1, c-1)
        res /= a+b+c
        
        memo[(a, b, c)] = res
        return res
    
    ans = dp(a, b, c)
    
    print(ans)
        
        
    
    
if __name__ == "__main__":
    main()


Fixed submission:
import sys
from collections import Counter
input = sys.stdin.readline


N = int(input())
memo = [[[-1]*(N+1) for _ in range(N+1)] for _ in range(N+1)]

def dp(a, b, c):
    if a == b == c == 0: return 0
    if memo[a][b][c] != -1:
        return memo[a][b][c]
    
    res = N
    if a > 0: res += a*dp(a-1, b, c)
    if b > 0: res += b*dp(a+1, b-1, c)
    if c > 0: res += c*dp(a, b+1, c-1)
    res /= a+b+c
    
    memo[a][b][c] = res
    return res

def main():
    A = list(map(int, input().split()))
    
    count = Counter(A)
    for i in range(1, 4):
        if not i in count: count[i] = 0
    x, y, z = count[1], count[2], count[3]

    
    ans = dp(x, y, z)
    
    print(ans)
        

    
if __name__ == "__main__":
    main()

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


def main():
    N = int(input())
    A = list(map(int, input().split()))
    
    count = Counter(A)
    for i in range(1, 4):
        if not i in count: count[i] = 0
    a, b, c = count[1], count[2], count[3]
    
    memo = dict()
    
    def dp(a, b, c):
        if (a, b, c) in memo:
            return memo[(a, b, c)]
        
        if a == b == c == 0:
            return 0
        
        if a < 0 or b < 0 or c < 0:
            return 0
        
        res = N
        res += a*dp(a-1, b, c)
        res += b*dp(a+1, b-1, c)
        res += c*dp(a, b+1, c-1)
        res /= a+b+c
        
        memo[(a, b, c)] = res
        return res
    
    ans = dp(a, b, c)
    
    print(ans)
        
        
    
    
if __name__ == "__main__":
    main()


Fixed submission:
import sys
from collections import Counter
input = sys.stdin.readline


N = int(input())
memo = [[[-1]*(N+1) for _ in range(N+1)] for _ in range(N+1)]

def dp(a, b, c):
    if a == b == c == 0: return 0
    if memo[a][b][c] != -1:
        return memo[a][b][c]
    
    res = N
    if a > 0: res += a*dp(a-1, b, c)
    if b > 0: res += b*dp(a+1, b-1, c)
    if c > 0: res += c*dp(a, b+1, c-1)
    res /= a+b+c
    
    memo[a][b][c] = res
    return res

def main():
    A = list(map(int, input().split()))
    
    count = Counter(A)
    for i in range(1, 4):
        if not i in count: count[i] = 0
    x, y, z = count[1], count[2], count[3]

    
    ans = dp(x, y, z)
    
    print(ans)
        

    
if __name__ == "__main__":
    main()

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


def main():
    N = int(input())
    A = list(map(int, input().split()))
    count = Counter(A)
    
    memo = dict()
    def dp(a, b, c):
        if (a, b, c) in memo:
            return memo[(a, b, c)]
        
        if a == b == c == 0:
            return 0
        
        if a < 0 or b < 0 or c < 0:
            return 0
        
        res = 1
        res += a/N*dp(a-1, b, c)
        res += b/N*dp(a+1, b-1, c)
        res += c/N*dp(a, b+1, c-1)
        res /= 1-(N-a-b-c)/N
        
        memo[(a, b, c)] = res
        return res
    
    a, b, c = count[1], count[2], count[3]
    ans = dp(a, b, c)
    
    print(ans)
        
        
    
    
if __name__ == "__main__":
    main()


Fixed submission:
import sys
from collections import Counter
input = sys.stdin.readline


N = int(input())
memo = [[[-1]*(N+1) for _ in range(N+1)] for _ in range(N+1)]

def dp(a, b, c):
    if a == b == c == 0: return 0
    if memo[a][b][c] != -1:
        return memo[a][b][c]
    
    res = N
    if a > 0: res += a*dp(a-1, b, c)
    if b > 0: res += b*dp(a+1, b-1, c)
    if c > 0: res += c*dp(a, b+1, c-1)
    res /= a+b+c
    
    memo[a][b][c] = res
    return res

def main():
    A = list(map(int, input().split()))
    
    count = Counter(A)
    for i in range(1, 4):
        if not i in count: count[i] = 0
    x, y, z = count[1], count[2], count[3]

    
    ans = dp(x, y, z)
    
    print(ans)
        

    
if __name__ == "__main__":
    main()