Problem p03169 (C++)

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

//.define
// #define FILE_IN_OUT
#define RET(_x) return cout << (_x) << '\n', 0;
#define all(_obj) std::begin(_obj), std::end(_obj)
#define loop(_n) for (int i = 0; i < (_n); ++i)
#define ceilDiv(_dividend, _divider)                                           \
  (((_dividend)-1LL + (_divider)) / (_divider))
#define has(_obj, _val) ((_obj).find(_val) != (_obj).end())
#define sz(_obj) static_cast<int>((_obj).size())
#ifdef SHJ_LOCAL
#define debug(_x) std::cerr << (#_x) << " = " << (_x) << '\n' << std::flush;
#else
#define debug(_x)                                                              \
  {}
#endif
#define endl "\n"
template <typename Tp>
inline void outarr(Tp _begin, Tp _end, const char *_delim = " ") {
  for (Tp current = _begin; current != _end; ++current) {
    std::cout << *current << _delim;
  }
  std::cout << '\n';
}

//.constant
using ll = long long;
using pii = std::pair<int, int>;
constexpr int INF = 0x3f3f3f3f;
constexpr int MOD = static_cast<const int>(1e9 + 7);

//.data
int cnt[3];
double dp[301][301][301];
double p[301][301][301];

//.code
int main() {
#ifdef FILE_IN_OUT
  std::ifstream cin("input.txt", std::ios::in);
  std::ofstream cout("output.txt", std::ios::out);
#else
  std::ios_base::sync_with_stdio(false);
  std::cin.tie(nullptr);
#endif

  int n;
  cin >> n;
  loop(n) {
    int x;
    cin >> x;
    ++cnt[x - 1];
  }
  const int sum = n;
  p[cnt[0]][cnt[1]][cnt[2]] = 1;
  for (int x3 = cnt[2]; x3 >= 0; --x3) {
    for (int x2 = sum; x2 >= 0; --x2) {
      for (int x1 = sum; x1 >= 0; --x1) {
        if (x1 + x2 + x3 == 0) {
          break;
        }
        const int zero = sum - x1 - x2 - x3;
        double q = 1.0 * zero / sum;
        dp[x1][x2][x3] += p[x1][x2][x3] / (1 - q);
        if (x3 > 0) {
          double curp = 1.0 * x3 / (x1 + x2 + x3);
          p[x1][x2 + 1][x3 - 1] += p[x1][x2][x3] * curp;
          dp[x1][x2 + 1][x3 - 1] += dp[x1][x2][x3] * curp;
        }
        if (x2 > 0) {
          double curp = 1.0 * x2 / (x1 + x2 + x3);
          p[x1 + 1][x2 - 1][x3] += p[x1][x2][x3] * curp;
          dp[x1 + 1][x2 - 1][x3] += dp[x1][x2][x3] * curp;
        }
        if (x1 > 0) {
          double curp = 1.0 * x1 / (x1 + x2 + x3);
          p[x1 - 1][x2][x3] += p[x1][x2][x3] * curp;
          dp[x1 - 1][x2][x3] += dp[x1][x2][x3] * curp;
        }
      }
    }
  }
  cout << fixed << setprecision(15) << dp[0][0][0] << endl;

#ifdef FILE_IN_OUT
  cin.close();
  cout.close();
#endif
  return 0;
}


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

//.define
// #define FILE_IN_OUT
#define RET(_x) return cout << (_x) << '\n', 0;
#define all(_obj) std::begin(_obj), std::end(_obj)
#define loop(_n) for (int i = 0; i < (_n); ++i)
#define ceilDiv(_dividend, _divider)                                           \
  (((_dividend)-1LL + (_divider)) / (_divider))
#define has(_obj, _val) ((_obj).find(_val) != (_obj).end())
#define sz(_obj) static_cast<int>((_obj).size())
#ifdef SHJ_LOCAL
#define debug(_x) std::cerr << (#_x) << " = " << (_x) << '\n' << std::flush;
#else
#define debug(_x)                                                              \
  {}
#endif
#define endl "\n"
template <typename Tp>
inline void outarr(Tp _begin, Tp _end, const char *_delim = " ") {
  for (Tp current = _begin; current != _end; ++current) {
    std::cout << *current << _delim;
  }
  std::cout << '\n';
}

//.constant
using ll = long long;
using pii = std::pair<int, int>;
constexpr int INF = 0x3f3f3f3f;
constexpr int MOD = static_cast<const int>(1e9 + 7);

//.data
int cnt[3];
double dp[301][301][301];
double p[301][301][301];

//.code
int main() {
#ifdef FILE_IN_OUT
  std::ifstream cin("input.txt", std::ios::in);
  std::ofstream cout("output.txt", std::ios::out);
#else
  std::ios_base::sync_with_stdio(false);
  std::cin.tie(nullptr);
#endif

  int n;
  cin >> n;
  loop(n) {
    int x;
    cin >> x;
    ++cnt[x - 1];
  }
  const int sum = n;
  p[cnt[0]][cnt[1]][cnt[2]] = 1;
  for (int x3 = cnt[2]; x3 >= 0; --x3) {
    for (int x2 = sum; x2 >= 0; --x2) {
      for (int x1 = sum; x1 >= 0; --x1) {
        if (x1 + x2 + x3 == 0 || x1 + x2 + x3 > n) {
          continue;
        }
        const int zero = sum - x1 - x2 - x3;
        double q = 1.0 * zero / sum;
        dp[x1][x2][x3] += p[x1][x2][x3] / (1 - q);
        if (x3 > 0) {
          double curp = 1.0 * x3 / (x1 + x2 + x3);
          p[x1][x2 + 1][x3 - 1] += p[x1][x2][x3] * curp;
          dp[x1][x2 + 1][x3 - 1] += dp[x1][x2][x3] * curp;
        }
        if (x2 > 0) {
          double curp = 1.0 * x2 / (x1 + x2 + x3);
          p[x1 + 1][x2 - 1][x3] += p[x1][x2][x3] * curp;
          dp[x1 + 1][x2 - 1][x3] += dp[x1][x2][x3] * curp;
        }
        if (x1 > 0) {
          double curp = 1.0 * x1 / (x1 + x2 + x3);
          p[x1 - 1][x2][x3] += p[x1][x2][x3] * curp;
          dp[x1 - 1][x2][x3] += dp[x1][x2][x3] * curp;
        }
      }
    }
  }
  cout << fixed << setprecision(15) << dp[0][0][0] << endl;

#ifdef FILE_IN_OUT
  cin.close();
  cout.close();
#endif
  return 0;
}