Problem p03169 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define pb push_back
#define int long long
#define mp make_pair
#define ff first
#define ss second
#define inf 9e18
#define pii pair<int, int>
#define setp(x) fixed << showpoint << setprecision(x)
#define fastio                                                                 \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);                                                                  \
  cout.tie(0);
using namespace std;
long double n;
long double dp[303][303][303];
int a[303];
long double solve(int a1, int a2, int a3) {
  if (a1 + a2 + a3 == 0)
    return 0;
  if (dp[a1][a2][a3] != -1)
    return dp[a1][a2][a3];
  long double cur = n / (a1 + a2 + a3);
  long double q1, q2, q3;
  q1 = q2 = q3 = 0;
  long double n2 = a1 + a2 + a3;

  if (a1 > 0)
    q1 = a1 * solve(a1 - 1, a2, a3);
  if (a2 > 0)
    q2 = a2 * solve(a1 + 1, a2 - 1, a3);
  if (a3 > 0)
    q3 = a3 * solve(a1, a2 + 1, a3 - 1);
  cur = cur + (q1 + q2 + q3) / n2;
  return dp[a1][a2][a3] = cur;
}
int32_t main() {

  fastio
#ifndef ONLINE_JUDGE
      // for getting input from input.txt
      freopen("input.txt", "r", stdin);
  // for writing output to output.txt
  freopen("output.txt", "w", stdout);
#endif
  cin >> n;
  int a1, a3, a2;
  a1 = a2 = a3 = 0;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    if (a[i] == 1)
      a1++;
    else if (a[i] == 2)
      a2++;
    else
      a3++;
  }
  for (int i = 0; i < 303; i++)
    for (int j = 0; j < 303; j++)
      for (int k = 0; k < 303; k++)
        dp[i][j][k] = -1;

  long double ans = solve(a1, a2, a3);

  cout << setp(10) << ans;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define pb push_back
#define int long long
#define mp make_pair
#define ff first
#define ss second
#define inf 9e18
#define pii pair<int, int>
#define setp(x) fixed << showpoint << setprecision(x)
#define fastio                                                                 \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);                                                                  \
  cout.tie(0);
using namespace std;
long double n;
long double dp[303][303][303];
int a[303];
long double solve(int a1, int a2, int a3) {
  if (a1 + a2 + a3 == 0)
    return 0;
  if (dp[a1][a2][a3] != -1)
    return dp[a1][a2][a3];
  long double cur = n / (a1 + a2 + a3);
  long double q1, q2, q3;
  q1 = q2 = q3 = 0;
  long double n2 = a1 + a2 + a3;

  if (a1 > 0)
    q1 = a1 * solve(a1 - 1, a2, a3);
  if (a2 > 0)
    q2 = a2 * solve(a1 + 1, a2 - 1, a3);
  if (a3 > 0)
    q3 = a3 * solve(a1, a2 + 1, a3 - 1);
  cur = cur + (q1 + q2 + q3) / n2;
  return dp[a1][a2][a3] = cur;
}
int32_t main() {

  fastio

          cin >>
      n;
  int a1, a3, a2;
  a1 = a2 = a3 = 0;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    if (a[i] == 1)
      a1++;
    else if (a[i] == 2)
      a2++;
    else
      a3++;
  }
  for (int i = 0; i < 303; i++)
    for (int j = 0; j < 303; j++)
      for (int k = 0; k < 303; k++)
        dp[i][j][k] = -1;

  long double ans = solve(a1, a2, a3);

  cout << setp(10) << ans;
  return 0;
}