Problem p03717 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <deque>
#include <functional>
#include <iostream>
#include <limits.h>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string.h>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
#define mod 1000000007
#define P pair<int, int> // l x
using namespace std;

int dp[301][301][301];
vector<P> a[301];
signed main() {
  int n, m;
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    int l, r, x;
    cin >> l >> r >> x;
    a[r].push_back({l, x});
  }
  for (P i : a[1]) {
    if (i.second >= 2) {
      puts("0");
      return 0;
    }
  }
  int ans = 0;
  dp[1][0][0] = dp[0][1][0] = dp[0][0][1] = 1;
  for (int i = 1; i <= n; i++) {
    for (int j = 0; j <= i; j++) {
      for (int k = 0; k <= j; k++) {
        bool OK = true;
        for (P S : a[i]) {
          int c = 1;
          if (S.first <= j)
            c++;
          if (S.first <= k)
            c++;
          if (c != S.second) {
            OK = false;
          }
        }
        int b[3] = {k, j, i};
        do {
          int x = b[0], y = b[1], z = b[2];
          if (!OK)
            dp[x][y][z] = 0;
          dp[i + 1][y][z] += dp[x][y][z];
          dp[i + 1][y][z] %= mod;
          dp[x][i + 1][z] += dp[x][y][z];
          dp[x][i + 1][z] %= mod;
          dp[x][y][i + 1] += dp[x][y][z];
          dp[x][y][i + 1] %= mod;
          if (i == n) {
            ans += dp[x][y][z];
            ans %= mod;
          }
        } while (next_permutation(b, b + 3));
      }
    }
  }
  cout << ans << endl;
}

Fixed submission:
#include <algorithm>
#include <cstdio>
#include <deque>
#include <functional>
#include <iostream>
#include <limits.h>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string.h>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
#define mod 1000000007
#define P pair<int, int> // l x
using namespace std;

int dp[302][302][302];
vector<P> a[301];
signed main() {
  int n, m;
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    int l, r, x;
    cin >> l >> r >> x;
    a[r].push_back({l, x});
  }
  for (P i : a[1]) {
    if (i.second >= 2) {
      puts("0");
      return 0;
    }
  }
  int ans = 0;
  dp[1][0][0] = dp[0][1][0] = dp[0][0][1] = 1;
  for (int i = 1; i <= n; i++) {
    for (int j = 0; j <= i; j++) {
      for (int k = 0; k <= j; k++) {
        bool OK = true;
        for (P S : a[i]) {
          int c = 1;
          if (S.first <= j)
            c++;
          if (S.first <= k)
            c++;
          if (c != S.second) {
            OK = false;
          }
        }
        int b[3] = {k, j, i};
        do {
          int x = b[0], y = b[1], z = b[2];
          if (!OK)
            dp[x][y][z] = 0;
          dp[i + 1][y][z] += dp[x][y][z];
          dp[i + 1][y][z] %= mod;
          dp[x][i + 1][z] += dp[x][y][z];
          dp[x][i + 1][z] %= mod;
          dp[x][y][i + 1] += dp[x][y][z];
          dp[x][y][i + 1] %= mod;
          if (i == n) {
            ans += dp[x][y][z];
            ans %= mod;
          }
        } while (next_permutation(b, b + 3));
      }
    }
  }
  cout << ans << endl;
}