Problem:
Score : 100 points

Problem Statement

There are N dishes, numbered 1, 2, \ldots, N.
Initially, for each i (1 \leq i \leq N), Dish i has a_i (1 \leq a_i \leq 3) pieces of sushi on it.

Taro will perform the following operation repeatedly until all the pieces of sushi are eaten:

Roll a die that shows the numbers 1, 2, \ldots, N with equal probabilities, and let i be the outcome. If there are some pieces of sushi on Dish i, eat one of them; if there is none, do nothing.

Find the expected number of times the operation is performed before all the pieces of sushi are eaten.

Constraints

All values in input are integers.

1 \leq N \leq 300

1 \leq a_i \leq 3

Input

Input is given from Standard Input in the following format:

N
a_1 a_2 \ldots a_N

Output

Print the expected number of times the operation is performed before all the pieces of sushi are eaten.
The output is considered correct when the relative difference is not greater than 10^{-9}.

Sample Input 1

3
1 1 1

Sample Output 1

5.5

The expected number of operations before the first piece of sushi is eaten, is 1.
After that, the expected number of operations before the second sushi is eaten, is 1.5.
After that, the expected number of operations before the third sushi is eaten, is 3.
Thus, the expected total number of operations is 1 + 1.5 + 3 = 5.5.

Sample Input 2

1
3

Sample Output 2

3

Outputs such as 3.00, 3.000000003 and 2.999999997 will also be accepted.

Sample Input 3

2
1 2

Sample Output 3

4.5

Sample Input 4

10
1 3 2 3 3 2 3 2 1 3

Sample Output 4

54.48064457488221

Solution in C++14 (GCC 5.4.1) (judge verdict: Accepted)

#include <bits/stdc++.h>

using namespace std;

#define sz(a) int(a.size())

typedef long double Double;
typedef long long Long;
struct ConvexHullTrick {
	struct Line {
		Double m,b;
		Line(Double m,Double b):m(m),b(b){}
		Double inter(const Line &L)const{
			return (L.b-b) / (m - L.m);
		}
		long long eval(Double x){
			return m*x + b;
		}
	};
	struct Node {
		Line l = Line(1, 0);
		Double intWithPrev = -1e100;
		vector<int> par, to;
	};
	vector<Node> nodes;
	ConvexHullTrick() {}
	void add(Line l, int version = -1){
		int id = nodes.size();
		Node nn; nn.l = l;
		if(!nodes.empty()) {
			int p = version == -1 ? nodes.size() -1 : version;
			for (int i = nodes[p].par.size(); i >= 0 && p != 0; --i) {
				if (nodes[p].par.size() <= i) continue;
				if (nodes[p].par[i] == 0) continue;
				int pp = nodes[p].par[i];
				int ppp = nodes[pp].par[0];
				if(nodes[ppp].l.inter(l) < nodes[pp].intWithPrev) {
					p = ppp;
				}
				else break;
			}
			while(p != 0) {
				int pp = nodes[p].par[0];
				if(nodes[pp].l.inter(l) < nodes[p].intWithPrev) {
					p = pp;
				} else {
					break;
				}
			}
			nodes[p].to.push_back(id);
			nn.par.push_back(p);
			nn.intWithPrev = nn.l.inter(nodes[p].l);
			for(int i = 0; i < nodes[ nn.par[i] ].par.size(); ++i) {
				int rp = nn.par[i];
				nn.par.push_back(nodes[rp].par[i]);
			}
			assert(nn.par.size() <= 20);
		}
		nodes.push_back(nn);
	}
	long long eval(Double x, int version = -1){
		int n = version == -1 ? nodes.size() - 1 : version;
		for (int i = nodes[n].par.size(); i >= 0; --i) {
			if (nodes[n].par.size() <= i) continue;
			int p = nodes[n].par[i];
			if (x < nodes[p].intWithPrev) {
				n = p;
			}
		}
		if (x < nodes[n].intWithPrev) n = nodes[n].par[0];
		return nodes[n].l.eval(x);
	}
	void print(int n = 0, int lvl = 0) {
		cout << string(lvl * 3, ' ')<< n << " " << nodes[n].intWithPrev << endl;
		for(int v : nodes[n].to)
			print(v, lvl+1);
	}
};

int N;
double memo [310][310][310];
bool vis [310][310][310];

double dp (int c0, int c1, int c2, int c3) {
	if (c0 == N) return 0;
	if (vis[c1][c2][c3]) return memo[c1][c2][c3];
	double p0 = 1.0*c0/N;
	double e1 = c1 ? (1+dp(c0+1, c1-1, c2, c3))*c1/N : 0;
	double e2 = c2 ? (1+dp(c0, c1+1, c2-1, c3))*c2/N : 0;
	double e3 = c3 ? (1+dp(c0, c1, c2+1, c3-1))*c3/N : 0;

	vis[c1][c2][c3] = 1;
	return memo[c1][c2][c3] = (e1+e2+e3+p0)/(1-p0);
}

int main() {
	cin >> N;
	vector <int> c(4);
	for (int i = 0; i < N; i++) {
		int t;
		cin >> t;
		c[t]++;
	}
	printf("%0.9f\n", dp(c[0], c[1], c[2], c[3]));
}