Problem p03717 (C++)

Buggy submission (status: Memory Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> ii;
typedef vector<ii> vii;
const int MN = 320;
ll dp[MN][MN][MN];
const ll M = 1e9 + 7;
vii q[MN];
int main() {
  ll n, m;
  scanf("%lld %lld ", &n, &m);
  for (int i = 0; i < m; i++) {
    ll a, b, c;
    scanf("%lld %lld %lld ", &a, &b, &c);
    q[b].push_back({a, c});
  }
  memset(dp, 0, sizeof(dp));
  dp[1][0][0] = dp[0][1][0] = dp[0][0][1] = 1;
  for (int i = 0; i <= n; i++) {
    for (int j = 0; j < i; j++) {
      for (int k = 0; k < i; k++) {
        for (int l = 0; l < q[i].size(); l++) {
          int ct = 1;
          int st = q[i][l].first;
          if (j >= st) {
            ct++;
          }
          if (k >= st) {
            ct++;
          }
          if (q[i][l].second != ct) {
            dp[i][j][k] = dp[j][i][k] = dp[j][k][i] = 0;
          }
        }
      }
    }
    for (int j = 0; j < i; j++) {
      for (int k = 0; k < i; k++) {
        dp[i + 1][j][k] += dp[i][j][k];
        dp[i][i + 1][k] += dp[i][j][k];
        dp[i][j][i + 1] += dp[i][j][k];
        dp[i + 1][i][k] += dp[j][i][k];
        dp[j][i + 1][k] += dp[j][i][k];
        dp[j][i][i + 1] += dp[j][i][k];
        dp[i + 1][k][i] += dp[j][k][i];
        dp[j][i + 1][i] += dp[j][k][i];
        dp[j][k][i + 1] += dp[j][k][i];
      }
    }
    for (int j = 0; j <= i; j++) {
      for (int k = 0; k <= i; k++) {
        dp[i + 1][j][k] %= M;
        dp[j][i + 1][k] %= M;
        dp[j][k][i + 1] %= M;
      }
    }
  }
  ll tot = 0;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      // printf("%lld %d %d\n",dp[i][j][n],i,j);
      tot += dp[n][i][j];
      tot += dp[i][j][n];
      tot += dp[i][n][j];
    }
  }
  tot %= M;
  printf("%lld\n", tot);
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> ii;
typedef vector<ii> vii;
const int MN = 305;
ll dp[MN][MN][MN];
const ll M = 1e9 + 7;
vii q[MN];
int main() {
  ll n, m;
  scanf("%lld %lld ", &n, &m);
  for (int i = 0; i < m; i++) {
    ll a, b, c;
    scanf("%lld %lld %lld ", &a, &b, &c);
    q[b].push_back({a, c});
  }
  memset(dp, 0, sizeof(dp));
  dp[1][0][0] = dp[0][1][0] = dp[0][0][1] = 1;
  for (int i = 0; i <= n; i++) {
    for (int j = 0; j < i; j++) {
      for (int k = 0; k < i; k++) {
        for (int l = 0; l < q[i].size(); l++) {
          int ct = 1;
          int st = q[i][l].first;
          if (j >= st) {
            ct++;
          }
          if (k >= st) {
            ct++;
          }
          if (q[i][l].second != ct) {
            dp[i][j][k] = dp[j][i][k] = dp[j][k][i] = 0;
          }
        }
      }
    }
    for (int j = 0; j < i; j++) {
      for (int k = 0; k < i; k++) {
        dp[i + 1][j][k] += dp[i][j][k];
        dp[i][i + 1][k] += dp[i][j][k];
        dp[i][j][i + 1] += dp[i][j][k];
        dp[i + 1][i][k] += dp[j][i][k];
        dp[j][i + 1][k] += dp[j][i][k];
        dp[j][i][i + 1] += dp[j][i][k];
        dp[i + 1][k][i] += dp[j][k][i];
        dp[j][i + 1][i] += dp[j][k][i];
        dp[j][k][i + 1] += dp[j][k][i];
      }
    }
    for (int j = 0; j <= i; j++) {
      for (int k = 0; k <= i; k++) {
        dp[i + 1][j][k] %= M;
        dp[j][i + 1][k] %= M;
        dp[j][k][i + 1] %= M;
      }
    }
  }
  ll tot = 0;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      // printf("%lld %d %d\n",dp[i][j][n],i,j);
      tot += dp[n][i][j];
      tot += dp[i][j][n];
      tot += dp[i][n][j];
    }
  }
  tot %= M;
  printf("%lld\n", tot);
}