Problem p03717 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define ll long long
#define inf 0x3f3f3f3f
#define N 203
#define pii pair<int, int>
#define mp make_pair
#define fi first
#define se second
#define pb push_back
using namespace std;
const int mod = 1000000007;
inline int read() {
  int x = 0, w = 0;
  char ch = getchar();
  while (!isdigit(ch))
    w |= ch == '-', ch = getchar();
  while (isdigit(ch))
    x = (x << 1) + (x << 3) + ch - '0', ch = getchar();
  return w ? -x : x;
}
int n, m, f[N][N][N], ans;
vector<pii> v[N];
inline void add(int &a, int b) {
  a += b;
  if (a >= mod)
    a -= mod;
}
inline bool check(int i, int j, int k, int m1, int m2, int m3, int M2) {
  return (k < m1 && (m2 == i || (j < m2 && k >= M2)) &&
          (m3 == -1 || (j >= m3 && k >= m3)));
}
signed main() {
  n = read(), m = read();
  for (int i = 1; i <= m; ++i) {
    int l = read(), r = read(), x = read();
    v[r].pb(mp(l, x));
  }
  f[0][0][0] = 1;
  for (int i = 0; i < n; ++i) {
    int m1 = i + 2, m2 = i + 2, M2 = -1, m3 = -1;
    for (int j = 0; j < v[i + 1].size(); ++j) {
      if (v[i + 1][j].se == 1)
        m1 = min(m1, v[i + 1][j].fi);
      if (v[i + 1][j].se == 2)
        M2 = max(M2, v[i + 1][j].fi), m2 = min(m2, v[i + 1][j].fi);
      if (v[i + 1][j].se == 3)
        m3 = max(m3, v[i + 1][j].fi);
    }
    for (int j = 0; j <= i; ++j)
      for (int k = j ? j + 1 : 0; k <= i; ++k) {
        if (check(i + 2, j, k, m1, m2, m3, M2))
          add(f[i + 1][j][k], f[i][j][k]);
        if (check(i + 2, j, i, m1, m2, m3, M2))
          add(f[i + 1][j][i], f[i][j][k]);
        if (check(i + 2, k, i, m1, m2, m3, M2))
          add(f[i + 1][k][i], f[i][j][k]);
      }
  }
  for (int j = 0; j < n; ++j)
    for (int k = j ? j + 1 : 0; k < n; ++k)
      add(ans, f[n][j][k]);
  printf("%d\n", ans % mod);
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define ll long long
#define inf 0x3f3f3f3f
#define N 403
#define pii pair<int, int>
#define mp make_pair
#define fi first
#define se second
#define pb push_back
using namespace std;
const int mod = 1000000007;
inline int read() {
  int x = 0, w = 0;
  char ch = getchar();
  while (!isdigit(ch))
    w |= ch == '-', ch = getchar();
  while (isdigit(ch))
    x = (x << 1) + (x << 3) + ch - '0', ch = getchar();
  return w ? -x : x;
}
int n, m, f[N][N][N], ans;
vector<pii> v[N];
inline void add(int &a, int b) {
  a += b;
  if (a >= mod)
    a -= mod;
}
inline bool check(int i, int j, int k, int m1, int m2, int m3, int M2) {
  return (k < m1 && (m2 == i || (j < m2 && k >= M2)) &&
          (m3 == -1 || (j >= m3 && k >= m3)));
}
signed main() {
  n = read(), m = read();
  for (int i = 1; i <= m; ++i) {
    int l = read(), r = read(), x = read();
    v[r].pb(mp(l, x));
  }
  f[0][0][0] = 1;
  for (int i = 0; i < n; ++i) {
    int m1 = i + 2, m2 = i + 2, M2 = -1, m3 = -1;
    for (int j = 0; j < v[i + 1].size(); ++j) {
      if (v[i + 1][j].se == 1)
        m1 = min(m1, v[i + 1][j].fi);
      if (v[i + 1][j].se == 2)
        M2 = max(M2, v[i + 1][j].fi), m2 = min(m2, v[i + 1][j].fi);
      if (v[i + 1][j].se == 3)
        m3 = max(m3, v[i + 1][j].fi);
    }
    for (int j = 0; j <= i; ++j)
      for (int k = j ? j + 1 : 0; k <= i; ++k) {
        if (check(i + 2, j, k, m1, m2, m3, M2))
          add(f[i + 1][j][k], f[i][j][k]);
        if (check(i + 2, j, i, m1, m2, m3, M2))
          add(f[i + 1][j][i], f[i][j][k]);
        if (check(i + 2, k, i, m1, m2, m3, M2))
          add(f[i + 1][k][i], f[i][j][k]);
      }
  }
  for (int j = 0; j < n; ++j)
    for (int k = j ? j + 1 : 0; k < n; ++k)
      add(ans, f[n][j][k]);
  printf("%d\n", ans % mod);
  return 0;
}