Problem p03717 (C++)

Buggy submission (status: Memory Limit Exceeded):
#include <algorithm>
#include <array>
#include <chrono>
#include <cmath>
#include <deque>
#include <functional>
#include <iostream>
#include <iterator>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <string>
#include <tuple>
#include <unordered_set>
#include <vector>
using namespace std;
typedef long long int llint;
#define mp make_pair
#define mt make_tuple
#define pub push_back
#define puf push_front
#define pob pop_back
#define pof pop_front
#define fir first
#define sec second
#define res resize
#define ins insert
#define era erase
const int mod = 1000000007;
const llint big = 2e18 + 10;
const long double pai = 3.141592653589793238462643383279502884197;
const long double eps = 1e-9;
template <class T, class U> void mineq(T &a, U b) {
  if (a > b) {
    a = b;
  }
}
template <class T, class U> void maxeq(T &a, U b) {
  if (a < b) {
    a = b;
  }
}
int gcd(int a, int b) {
  if (a % b == 0) {
    return b;
  } else
    return gcd(b, a % b);
}
vector<pair<int, int>> seiya[302]; // 制約
llint dp[334][334][334] = {0};
int n, m;
llint solve(int R, int G, int B) {
  int r = max(R, max(G, B));
  int b = min(R, min(G, B));
  int g = R + B + G - r - b;
  // r>g>b
  if ((r == g && g != 0) || (g == b && b != 0)) {
    return mod;
  }
  if (dp[r][g][b] != 0) {
    return dp[r][g][b];
  }
  // cout<<r<<" "<<g<<" "<<b<<endl;
  for (int i = 0; i < seiya[r].size(); i++) {
    int mx = 1;
    if (g >= seiya[r][i].fir) {
      mx++;
    }
    if (b >= seiya[r][i].fir) {
      mx++;
    }
    if (seiya[r][i].sec != mx) {
      dp[r][g][b] = mod;
      return mod;
    } // 0ではない,どうせ後でmodとる
  }
  if (r > g + 1) {
    return dp[r][g][b] = solve(r - 1, g, b);
  } // 1つ前を考える
  llint ans = 0;
  for (int i = 0; i < r; i++) {
    ans += solve(i, g, b);
  }
  ans %= mod;
  if (ans == 0) {
    ans = mod;
  }
  return dp[r][g][b] = ans;
}
int main(void) {
  dp[0][0][0] = 1;
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    int l, r, x;
    cin >> l >> r >> x;
    seiya[r].pub(mp(l, x));
  }
  cout << solve(n + 3, n + 2, n + 1) % mod << endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <array>
#include <chrono>
#include <cmath>
#include <deque>
#include <functional>
#include <iostream>
#include <iterator>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <string>
#include <tuple>
#include <unordered_set>
#include <vector>
using namespace std;
typedef long long int llint;
#define mp make_pair
#define mt make_tuple
#define pub push_back
#define puf push_front
#define pob pop_back
#define pof pop_front
#define fir first
#define sec second
#define res resize
#define ins insert
#define era erase
const int mod = 1000000007;
const llint big = 2e18 + 10;
const long double pai = 3.141592653589793238462643383279502884197;
const long double eps = 1e-9;
template <class T, class U> void mineq(T &a, U b) {
  if (a > b) {
    a = b;
  }
}
template <class T, class U> void maxeq(T &a, U b) {
  if (a < b) {
    a = b;
  }
}
int gcd(int a, int b) {
  if (a % b == 0) {
    return b;
  } else
    return gcd(b, a % b);
}
vector<pair<int, int>> seiya[304]; // 制約
int dp[304][304][304] = {0};
int n, m;
llint solve(int R, int G, int B) {
  int r = max(R, max(G, B));
  int b = min(R, min(G, B));
  int g = R + B + G - r - b;
  // r>g>b
  if ((r == g && g != 0) || (g == b && b != 0)) {
    return mod;
  }
  if (dp[r][g][b] != 0) {
    return dp[r][g][b];
  }
  // cout<<r<<" "<<g<<" "<<b<<endl;
  for (int i = 0; i < seiya[r].size(); i++) {
    int mx = 1;
    if (g >= seiya[r][i].fir) {
      mx++;
    }
    if (b >= seiya[r][i].fir) {
      mx++;
    }
    if (seiya[r][i].sec != mx) {
      dp[r][g][b] = mod;
      return mod;
    } // 0ではない,どうせ後でmodとる
  }
  if (r > g + 1) {
    return dp[r][g][b] = solve(r - 1, g, b);
  } // 1つ前を考える
  llint ans = 0;
  for (int i = 0; i < r; i++) {
    ans += solve(i, g, b);
  }
  ans %= mod;
  if (ans == 0) {
    ans = mod;
  }
  return dp[r][g][b] = ans;
}
int main(void) {
  dp[0][0][0] = 1;
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    int l, r, x;
    cin >> l >> r >> x;
    seiya[r].pub(mp(l, x));
  }
  cout << solve(n + 3, n + 2, n + 1) % mod << endl;
  return 0;
}