Problem p03717 (C++)

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

#define FAST                                                                   \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(0);
#define pb push_back
#define eb emplace_back
#define ins insert
#define ph push
#define f first
#define s second
#define cbr cerr << "hi\n"
#define mmst(x, v) memset((x), v, sizeof((x)))
#define siz(x) ((ll)x.size())
#define all(x) (x).begin(), (x).end()
#define lbd(x, y) lower_bound(all(x), y)
#define ubd(x, y) upper_bound(all(x), y)
#define btinpct(x) __builtin_popcountll((x))
mt19937
    rng(chrono::steady_clock::now()
            .time_since_epoch()
            .count()); // can be used by calling rng() or shuffle(A, A+n, rng)
inline long long rand(long long x, long long y) {
  return (rng() % (y + 1 - x)) + x;
} // inclusivesss
string to_string(char c) {
  string s(1, c);
  return s;
}
template <typename T> inline T gcd(T a, T b) {
  return a == 0 ? b : gcd(b % a, a);
}

#define ll long long int
#define ld long double
#define FOR(ii, ss, ee) for (ll ii = (ss); ii <= (ll)(ee); ++ii)
#define DEC(ii, ss, ee) for (ll ii = (ss); ii >= (ll)(ee); --ii)
typedef pair<ll, ll> pi;
typedef pair<ll, pi> spi;
typedef pair<pi, pi> dpi;

#define LLINF ((long long)1e18) // 1234567890987654321
#define INF 1234567890ll
// #define cerr if(0)cout
#define MAXN (52)
ll n, m, dp[MAXN][MAXN][MAXN];
vector<pi> v[MAXN];
ll many(ll a, ll b, ll c, ll d) { return (a >= d) + (b >= d) + (c >= d); }
int main() {
  FAST cin >> n >> m;
  FOR(i, 0, m - 1) {
    ll a, b, c;
    cin >> a >> b >> c;
    v[b].eb(a, c);
  }
  const ll mod = 1e9 + 7;
  dp[0][0][0] = 1;
  ll ans = 0;
  FOR(i, 0, n) FOR(j, 0, n) FOR(k, 0, n) {
    if (i == j && i && j)
      continue;
    if (i == k && i && k)
      continue;
    if (j == k && j && k)
      continue;
    ll l = max({i, j, k}) + 1;
    if (l == n + 1) {
      ans += dp[i][j][k], ans %= mod;
      continue;
    }
    dp[l][j][k] += dp[i][j][k], dp[l][j][k] %= mod;
    dp[i][l][k] += dp[i][j][k], dp[i][l][k] %= mod;
    dp[i][j][l] += dp[i][j][k], dp[i][j][l] %= mod;
    for (auto p : v[l]) {
      if (many(l, j, k, p.f) ^ p.s)
        dp[l][j][k] = 0;
      if (many(i, l, k, p.f) ^ p.s)
        dp[i][l][k] = 0;
      if (many(i, j, l, p.f) ^ p.s)
        dp[i][j][l] = 0;
    }
  }
  cout << ans << '\n';
}


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

#define FAST                                                                   \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(0);
#define pb push_back
#define eb emplace_back
#define ins insert
#define ph push
#define f first
#define s second
#define cbr cerr << "hi\n"
#define mmst(x, v) memset((x), v, sizeof((x)))
#define siz(x) ((ll)x.size())
#define all(x) (x).begin(), (x).end()
#define lbd(x, y) lower_bound(all(x), y)
#define ubd(x, y) upper_bound(all(x), y)
#define btinpct(x) __builtin_popcountll((x))
mt19937
    rng(chrono::steady_clock::now()
            .time_since_epoch()
            .count()); // can be used by calling rng() or shuffle(A, A+n, rng)
inline long long rand(long long x, long long y) {
  return (rng() % (y + 1 - x)) + x;
} // inclusivesss
string to_string(char c) {
  string s(1, c);
  return s;
}
template <typename T> inline T gcd(T a, T b) {
  return a == 0 ? b : gcd(b % a, a);
}

#define ll long long int
#define ld long double
#define FOR(ii, ss, ee) for (ll ii = (ss); ii <= (ll)(ee); ++ii)
#define DEC(ii, ss, ee) for (ll ii = (ss); ii >= (ll)(ee); --ii)
typedef pair<ll, ll> pi;
typedef pair<ll, pi> spi;
typedef pair<pi, pi> dpi;

#define LLINF ((long long)1e18) // 1234567890987654321
#define INF 1234567890ll
// #define cerr if(0)cout
#define MAXN (302)
ll n, m, dp[MAXN][MAXN][MAXN];
vector<pi> v[MAXN];
ll many(ll a, ll b, ll c, ll d) { return (a >= d) + (b >= d) + (c >= d); }
int main() {
  FAST cin >> n >> m;
  FOR(i, 0, m - 1) {
    ll a, b, c;
    cin >> a >> b >> c;
    v[b].eb(a, c);
  }
  const ll mod = 1e9 + 7;
  dp[0][0][0] = 1;
  ll ans = 0;
  FOR(i, 0, n) FOR(j, 0, n) FOR(k, 0, n) {
    if (i == j && i && j)
      continue;
    if (i == k && i && k)
      continue;
    if (j == k && j && k)
      continue;
    ll l = max({i, j, k}) + 1;
    if (l == n + 1) {
      ans += dp[i][j][k], ans %= mod;
      continue;
    }
    dp[l][j][k] += dp[i][j][k], dp[l][j][k] %= mod;
    dp[i][l][k] += dp[i][j][k], dp[i][l][k] %= mod;
    dp[i][j][l] += dp[i][j][k], dp[i][j][l] %= mod;
    for (auto p : v[l]) {
      if (many(l, j, k, p.f) ^ p.s)
        dp[l][j][k] = 0;
      if (many(i, l, k, p.f) ^ p.s)
        dp[i][l][k] = 0;
      if (many(i, j, l, p.f) ^ p.s)
        dp[i][j][l] = 0;
    }
  }
  cout << ans << '\n';
}