Problem p03717 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <climits>
#include <cmath>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <string>
#include <vector>

using namespace std;

#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define REP(i, n) FOR(i, 0, n)
#define SORT(c) sort((c).begin(), (c).end())

typedef long long ll;
typedef pair<int, int> P;
typedef vector<int> V;
typedef map<int, int> M;

constexpr ll MOD = 1e9 + 7;

int main() {
  int n, m, le[310], ri[310], x[310];

  ll dp[31][31][31];

  vector<P> v[310];

  cin >> n >> m;
  REP(i, m) {
    cin >> le[i] >> ri[i] >> x[i];
    v[ri[i]].push_back({le[i], x[i]});
  }

  memset(dp, 0, sizeof(dp));

  dp[0][0][0] = 1;

  ll res = 0;

  for (int i = 0; i <= n; i++)
    for (int j = 0; j <= n; j++)
      for (int k = 0; k <= n; k++) {
        int ma = max(i, max(j, k));

        int a[3];
        a[0] = i;
        a[1] = j;
        a[2] = k;

        sort(a, a + 3);

        bool check = true;

        for (P l : v[ma]) {
          switch (l.second) {
          case 1:
            if (l.first <= a[1])
              check = false;
            break;
          case 2:
            if (a[1] < l.first || a[0] >= l.first)
              check = false;
            break;
          case 3:
            if (a[0] < l.first)
              check = false;
            break;
          default:
            break;
          }
        }

        if (!check)
          dp[i][j][k] = 0;

        if (ma == n) {
          res += dp[i][j][k];
          res %= MOD;
        }

        (dp[ma + 1][j][k] += dp[i][j][k]) %= MOD;
        (dp[i][ma + 1][k] += dp[i][j][k]) %= MOD;
        (dp[i][j][ma + 1] += dp[i][j][k]) %= MOD;
      }

  cout << res << endl;

  return 0;
}

Fixed submission:
#include <algorithm>
#include <climits>
#include <cmath>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <string>
#include <vector>

using namespace std;

#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define REP(i, n) FOR(i, 0, n)
#define SORT(c) sort((c).begin(), (c).end())

typedef long long ll;
typedef pair<int, int> P;
typedef vector<int> V;
typedef map<int, int> M;

constexpr ll MOD = 1e9 + 7;

int main() {
  int n, m, le[310], ri[310], x[310];

  ll dp[310][310][310];

  vector<P> v[310];

  cin >> n >> m;
  REP(i, m) {
    cin >> le[i] >> ri[i] >> x[i];
    v[ri[i]].push_back({le[i], x[i]});
  }

  memset(dp, 0, sizeof(dp));

  dp[0][0][0] = 1;

  ll res = 0;

  for (int i = 0; i <= n; i++)
    for (int j = 0; j <= n; j++)
      for (int k = 0; k <= n; k++) {
        int ma = max(i, max(j, k));

        int a[3];
        a[0] = i;
        a[1] = j;
        a[2] = k;

        sort(a, a + 3);

        bool check = true;

        for (P l : v[ma]) {
          switch (l.second) {
          case 1:
            if (l.first <= a[1])
              check = false;
            break;
          case 2:
            if (a[1] < l.first || a[0] >= l.first)
              check = false;
            break;
          case 3:
            if (a[0] < l.first)
              check = false;
            break;
          default:
            break;
          }
        }

        if (!check)
          dp[i][j][k] = 0;

        if (ma == n) {
          res += dp[i][j][k];
          res %= MOD;
        }

        (dp[ma + 1][j][k] += dp[i][j][k]) %= MOD;
        (dp[i][ma + 1][k] += dp[i][j][k]) %= MOD;
        (dp[i][j][ma + 1] += dp[i][j][k]) %= MOD;
      }

  cout << res << endl;

  return 0;
}