Problem p03717 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;

typedef pair<int, int> P;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define all(c) (c).begin(), (c).end()
#define uniq(c) c.erase(unique(all(c)), (c).end())
#define _1 first
#define _2 second
#define pb push_back
#define INF 1145141919
#define MOD 1000000007

int N, M;
int dp[300][300][300];
vector<P> G[300];

bool valid(int i, int a, int b, int c) {
  for (P p : G[i]) {
    int ctr = 0;
    if (a > p._1)
      ctr++;
    if (b > p._1)
      ctr++;
    if (c > p._1)
      ctr++;
    if (ctr != p._2)
      return false;
  }
  return true;
}

signed main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> N >> M;
  rep(i, M) {
    int l, r, x;
    cin >> l >> r >> x;
    l--, r--;
    G[r].pb(P(l, x));
  }

  dp[0][0][0] = 1;
  for (int i = 1; i <= N; i++) {
    for (int a = 0; a < i; a++) {
      for (int b = 0; b < i; b++) {
        if (a != 0 && a == b)
          continue;
        int c = i - 1;
        if (c != 0 && (a == c || b == c))
          continue;
        if (dp[i - 1][a][b] == 0)
          continue;
        // (a,b,c) ->
        // a
        if (valid(i - 1, i, b, c)) {
          dp[i][b][c] += dp[i - 1][a][b];
          dp[i][b][c] %= MOD;
        }

        // b
        if (valid(i - 1, i, a, c)) {
          dp[i][a][c] += dp[i - 1][a][b];
          dp[i][a][c] %= MOD;
        }

        // c
        if (valid(i - 1, a, b, i)) {
          dp[i][a][b] += dp[i - 1][a][b];
          dp[i][a][b] %= MOD;
        }
      }
    }
  }
  int s = 0;
  rep(a, N) {
    rep(b, N) {
      s += dp[N][a][b];
      s %= MOD;
    }
  }
  cout << s << "\n";
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;

typedef pair<int, int> P;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define all(c) (c).begin(), (c).end()
#define uniq(c) c.erase(unique(all(c)), (c).end())
#define _1 first
#define _2 second
#define pb push_back
#define INF 1145141919
#define MOD 1000000007

int N, M;
int dp[301][301][301];
vector<P> G[300];

bool valid(int i, int a, int b, int c) {
  for (P p : G[i]) {
    int ctr = 0;
    if (a > p._1)
      ctr++;
    if (b > p._1)
      ctr++;
    if (c > p._1)
      ctr++;
    if (ctr != p._2)
      return false;
  }
  return true;
}

signed main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> N >> M;
  rep(i, M) {
    int l, r, x;
    cin >> l >> r >> x;
    l--, r--;
    G[r].pb(P(l, x));
  }

  dp[0][0][0] = 1;
  for (int i = 1; i <= N; i++) {
    for (int a = 0; a < i; a++) {
      for (int b = 0; b < i; b++) {
        if (a != 0 && a == b)
          continue;
        int c = i - 1;
        if (c != 0 && (a == c || b == c))
          continue;
        if (dp[i - 1][a][b] == 0)
          continue;
        // (a,b,c) ->
        // a
        if (valid(i - 1, i, b, c)) {
          dp[i][b][c] += dp[i - 1][a][b];
          dp[i][b][c] %= MOD;
        }

        // b
        if (valid(i - 1, i, a, c)) {
          dp[i][a][c] += dp[i - 1][a][b];
          dp[i][a][c] %= MOD;
        }

        // c
        if (valid(i - 1, a, b, i)) {
          dp[i][a][b] += dp[i - 1][a][b];
          dp[i][a][b] %= MOD;
        }
      }
    }
  }
  int s = 0;
  rep(a, N) {
    rep(b, N) {
      s += dp[N][a][b];
      s %= MOD;
    }
  }
  cout << s << "\n";
  return 0;
}