Problem p03169 (C++)

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

#define ll long long
#define pb push_back
#define N 1000000007
#define nmax 200001
#define pll pair<ll, ll>
#define fill_nums(A, n)                                                        \
  for (ll i = 0; i < n; i++)                                                   \
    scanf("%lld", &A[i]);
#define fill_edges(adj, m)                                                     \
  for (ll i = 0; i < m; i++) {                                                 \
    ll a, b;                                                                   \
    scanf("%lld %lld", &a, &b);                                                \
    adj[a - 1].pb(b - 1);                                                      \
    adj[b - 1].pb(a - 1);                                                      \
  }
#define fill_direct(adj, m)                                                    \
  for (ll i = 0; i < m; i++) {                                                 \
    ll a, b;                                                                   \
    scanf("%lld %lld", &a, &b);                                                \
    adj[a - 1].pb(b - 1);                                                      \
  }

ll extended_gcd(ll a, ll b, ll &x, ll &y) {
  if (a == 0) {
    x = 0;
    y = 1;
    return b;
  }

  ll x1, y1;
  ll gcd = extended_gcd(b % a, a, x1, y1);

  x = y1 - (b / a) * x1;
  y = x1;

  return gcd;
}

vector<vector<vector<double>>>
    dp(301, vector<vector<double>>(301, vector<double>(301, 0)));
int main() {

  ll n, c1 = 0, c2 = 0, c3 = 0;
  cin >> n;

  for (int i = 0; i < n; i++) {
    int a;
    cin >> a;
    if (a == 1)
      c1++;
    else if (a == 2)
      c2++;
    else if (a == 3)
      c3++;
  }

  for (int i = 0; i <= c3; i++) {
    for (int j = 0; j <= c2 + c3; j++) {
      for (int k = 0; k <= c1 + c2 + c3; k++) {
        if (i == 0 && k == 0 && j == 0)
          continue;
        double a = (k > 0) ? k * dp[i][j][k - 1] : 0;
        double b = (j > 0) ? j * dp[i][j - 1][k + 1] : 0;
        double c = (i > 0) ? i * dp[i - 1][j + 1][k] : 0;
        dp[i][j][k] = (a + b + c + n) / (i + j + k);
      }
    }
  }
  cout << fixed << setprecision(10);
  cout << dp[c3][c2][c1] << endl;
  return 0;
}


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

#define ll long long
#define pb push_back
#define N 1000000007
#define nmax 200001
#define pll pair<ll, ll>
#define fill_nums(A, n)                                                        \
  for (ll i = 0; i < n; i++)                                                   \
    scanf("%lld", &A[i]);
#define fill_edges(adj, m)                                                     \
  for (ll i = 0; i < m; i++) {                                                 \
    ll a, b;                                                                   \
    scanf("%lld %lld", &a, &b);                                                \
    adj[a - 1].pb(b - 1);                                                      \
    adj[b - 1].pb(a - 1);                                                      \
  }
#define fill_direct(adj, m)                                                    \
  for (ll i = 0; i < m; i++) {                                                 \
    ll a, b;                                                                   \
    scanf("%lld %lld", &a, &b);                                                \
    adj[a - 1].pb(b - 1);                                                      \
  }

ll extended_gcd(ll a, ll b, ll &x, ll &y) {
  if (a == 0) {
    x = 0;
    y = 1;
    return b;
  }

  ll x1, y1;
  ll gcd = extended_gcd(b % a, a, x1, y1);

  x = y1 - (b / a) * x1;
  y = x1;

  return gcd;
}

vector<vector<vector<double>>>
    dp(303, vector<vector<double>>(303, vector<double>(303, 0)));
int main() {

  ll n, c1 = 0, c2 = 0, c3 = 0;
  cin >> n;

  for (int i = 0; i < n; i++) {
    int a;
    cin >> a;
    if (a == 1)
      c1++;
    else if (a == 2)
      c2++;
    else if (a == 3)
      c3++;
  }

  for (int i = 0; i <= c3; i++) {
    for (int j = 0; j <= c2 + c3; j++) {
      for (int k = 0; k <= c1 + c2 + c3; k++) {
        if (i == 0 && k == 0 && j == 0)
          continue;
        double a = (k > 0) ? k * dp[i][j][k - 1] : 0;
        double b = (j > 0) ? j * dp[i][j - 1][k + 1] : 0;
        double c = (i > 0) ? i * dp[i - 1][j + 1][k] : 0;
        dp[i][j][k] = (a + b + c + n) / (i + j + k);
      }
    }
  }
  cout << fixed << setprecision(10);
  cout << dp[c3][c2][c1] << endl;
  return 0;
}