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 Rust (1.15.1) (judge verdict: Accepted)

#![allow(unused)]

use std::cmp::{min, max};
use std::io::Write;

macro_rules! dbg {
    ($($a:expr),*) => {
        writeln!(
            std::io::stderr(),
            concat!($(stringify!($a), " = {:?}, "),*),
            $($a),*);
    }
}

//
// Based on EbTech's Scanner https://stackoverflow.com/q/56449027
//
struct Scanner<'a, R: std::io::BufRead> {
    reader: R,
    buf: String,
    iter: std::str::SplitWhitespace<'a>,
}

impl<'a, R: std::io::BufRead> Scanner<'a, R> {
    fn new(reader: R) -> Scanner<'a, R> {
        Scanner { reader: reader, buf: String::new(), iter: "".split_whitespace() }
    }

    fn get_str(&mut self) -> &str {
        if let Some(t) = self.iter.next() {
            return t;
        }
        self.buf.clear();
        self.reader.read_line(&mut self.buf).expect("I/O error");
        self.iter = unsafe { std::mem::transmute(self.buf.split_whitespace()) };
        self.get_str()
    }

    fn get<T: std::str::FromStr>(&mut self) -> T {
        self.get_str().parse().ok().expect("Parse error")
    }
}

fn main() {
    let stdin = std::io::stdin();
    solve(Scanner::new(stdin.lock()));
}

fn solve<R: std::io::BufRead>(mut sc: Scanner<R>) {
    let n: usize = sc.get();
    let nsushis: Vec<usize> = (0..n).map(|_| sc.get()).collect();

    // dp[d1][d2][d3] = 1貫皿 d1 枚, 2貫皿 d2 枚, 3貫皿 d3 枚 の状態から全貫
    //                  食べる手数の期待値
    let mut dp = vec![vec![vec![0.; n + 1]; n + 1]; n + 1];

    for d3 in 0..(n + 1) {
        for d2 in 0..(n - d3 + 1) {
            for d1 in 0..(n - d3 - d2 + 1) {
                if d1 + d2 + d3 == 0 {
                    continue;
                }

                // i 貫乘った皿を選ぶ確率
                let p0 = (n - d1 - d2 - d3) as f64 / n as f64;
                let p1 = d1 as f64 / n as f64;
                let p2 = d2 as f64 / n as f64;
                let p3 = d3 as f64 / n as f64;

                //
                // dp[d1][d2][d3] =
                //     1.
                //     + dp[d1 - 1][d2][d3] * p1
                //     + dp[d1][d2 - 1][d3] * p2
                //     + dp[d1][d2][d3 - 1] * p3
                //     + dp[d1][d2][d3] * p0;
                //
                // dp[d1][d2][d3] を左辺へ移項し，両辺 (1 - p0) で除算
                //
                dp[d1][d2][d3] =
                    (
                        1.
                        + if d1 > 0 { dp[d1 - 1][d2][d3] * p1 } else { 0. }
                        + if d2 > 0 { dp[d1 + 1][d2 - 1][d3] * p2 } else { 0. }
                        + if d3 > 0 { dp[d1][d2 + 1][d3 - 1] * p3 } else { 0. }
                    ) / (1. - p0);
            }
        }
    }

    let dinit = nsushis.iter().fold([0; 3], |mut a, x| { a[x - 1] += 1; a });
    println!("{}", dp[dinit[0]][dinit[1]][dinit[2]]);
}