Problem p03169 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

inline int toInt(string s) {
  int v;
  istringstream sin(s);
  sin >> v;
  return v;
}
template <class T> inline string toString(T x) {
  ostringstream sout;
  sout << x;
  return sout.str();
}

typedef long long ll;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> P;
typedef tuple<int, int, int> tpl;

#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define SORT(c) sort((c).begin(), (c).end())
#define REVERSE(c) reverse((c).begin(), (c).end())
#define LB(a, x) lower_bound((a).begin(), (a).end(), x) - (a).begin()
#define UB(a, x) upper_bound((a).begin(), (a).end(), x) - (a).begin()

#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
#define RFOR(i, a, b) for (int i = (a)-1; i >= (b); --i)
#define RREP(i, n) RFOR(i, n, 0)

#define en "\n"

const double EPS = 1e-9;
const double PI = acos(-1.0);
const int INT_INF = 2147483647;
const long long LL_INF = 1LL << 60;
const long long MOD = 1000000007; // 998244353

#define CLR(a) memset((a), 0, sizeof(a))

#define dump(x) cerr << #x << " = " << (x) << endl;
#define debug(x)                                                               \
  cerr << #x << " = " << (x) << " (L" << __LINE__ << ")"                       \
       << " " << __FILE__ << endl;

template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

int N;
VI a;
double dp[300][300][300];

double rec(int a, int b, int c) {
  if (dp[a][b][c] != -1)
    return dp[a][b][c];
  if (a == 0 && b == 0 && c == 0)
    return dp[a][b][c] = 0;

  double n = a + b + c;
  double res = (double)N / n;
  if (a > 0)
    res += rec(a - 1, b, c) * a / n;
  if (b > 0)
    res += rec(a + 1, b - 1, c) * b / n;
  if (c > 0)
    res += rec(a, b + 1, c - 1) * c / n;
  return dp[a][b][c] = res;
}

int main(void) {
  cin >> N;
  REP(i, N + 1) REP(j, N + 1) REP(k, N + 1) dp[i][j][k] = -1;
  a.assign(N, 0);
  int n[3];
  REP(i, 3) n[i] = 0;
  REP(i, N) {
    cin >> a[i];
    n[a[i] - 1]++;
  }

  double ans = rec(n[0], n[1], n[2]);
  cout << fixed << setprecision(12) << ans << en;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;

inline int toInt(string s) {
  int v;
  istringstream sin(s);
  sin >> v;
  return v;
}
template <class T> inline string toString(T x) {
  ostringstream sout;
  sout << x;
  return sout.str();
}

typedef long long ll;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> P;
typedef tuple<int, int, int> tpl;

#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define SORT(c) sort((c).begin(), (c).end())
#define REVERSE(c) reverse((c).begin(), (c).end())
#define LB(a, x) lower_bound((a).begin(), (a).end(), x) - (a).begin()
#define UB(a, x) upper_bound((a).begin(), (a).end(), x) - (a).begin()

#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
#define RFOR(i, a, b) for (int i = (a)-1; i >= (b); --i)
#define RREP(i, n) RFOR(i, n, 0)

#define en "\n"

const double EPS = 1e-9;
const double PI = acos(-1.0);
const int INT_INF = 2147483647;
const long long LL_INF = 1LL << 60;
const long long MOD = 1000000007; // 998244353

#define CLR(a) memset((a), 0, sizeof(a))

#define dump(x) cerr << #x << " = " << (x) << endl;
#define debug(x)                                                               \
  cerr << #x << " = " << (x) << " (L" << __LINE__ << ")"                       \
       << " " << __FILE__ << endl;

template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

int N;
VI a;
double dp[301][301][301];

double rec(int a, int b, int c) {
  if (dp[a][b][c] != -1)
    return dp[a][b][c];
  if (a == 0 && b == 0 && c == 0)
    return dp[a][b][c] = 0;

  double n = a + b + c;
  double res = (double)N / n;
  if (a > 0)
    res += rec(a - 1, b, c) * a / n;
  if (b > 0)
    res += rec(a + 1, b - 1, c) * b / n;
  if (c > 0)
    res += rec(a, b + 1, c - 1) * c / n;
  return dp[a][b][c] = res;
}

int main(void) {
  cin >> N;
  REP(i, N + 1) REP(j, N + 1) REP(k, N + 1) dp[i][j][k] = -1;
  a.assign(N, 0);
  int n[3];
  REP(i, 3) n[i] = 0;
  REP(i, N) {
    cin >> a[i];
    n[a[i] - 1]++;
  }

  double ans = rec(n[0], n[1], n[2]);
  cout << fixed << setprecision(12) << ans << en;
  return 0;
}