Problem p03717 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>

using namespace std;

const int MOD = 1000000007; // 998244353; // 998244853;

template <typename T> struct modular {
  constexpr modular() : val(0) {}
  constexpr modular(const modular<T> &_m) : val(_m.val) {}
  template <typename U> constexpr modular(const U &_r = U()) {
    val = -MOD <= _r && _r < MOD ? _r : _r % MOD;
    if (val < 0) {
      val += MOD;
    }
  }
  const T operator()() { return val; }
  template <typename U> explicit operator U() const {
    return static_cast<U>(val);
  }
  modular<T> &operator+=(const modular<T> &_m) {
    if ((val += _m.val) >= MOD) {
      val -= MOD;
    }
    return *this;
  }
  modular<T> &operator-=(const modular<T> &_m) {
    if ((val -= _m.val) < 0) {
      val += MOD;
    }
    return *this;
  }
  modular<T> &operator*=(const modular<T> &_m) {
    val = modular<T>(static_cast<int64_t>(val) * static_cast<int64_t>(_m.val))
              .val;
    return *this;
  }
  modular<T> &operator/=(const modular<T> &_m) {
    T a = _m.val, b = MOD, u = 0, v = 1;
    while (a != 0) {
      T q = b / a;
      b -= q * a;
      swap(a, b);
      u -= q * v;
      swap(u, v);
    }
    return *this *= u;
  }
  modular<T> &operator=(const modular<T> &_m) {
    val = _m.val;
    return *this;
  }
  template <typename U> modular<T> &operator+=(const U &_r) {
    return *this += modular<T>(_r);
  }
  template <typename U> modular<T> &operator-=(const U &_r) {
    return *this -= modular<T>(_r);
  }
  template <typename U> modular<T> &operator*=(const U &_r) {
    return *this *= modular<T>(_r);
  }
  template <typename U> modular<T> &operator/=(const U &_r) {
    return *this /= modular<T>(_r);
  }
  template <typename U> modular<T> &operator=(const U &_r) {
    val = modular<T>(_r).val;
    return *this;
  }
  modular<T> operator-() { return modular<T>(-val); }
  template <typename U>
  friend bool operator==(const modular<U> &, const modular<U> &);
  friend std::istream &operator>>(std::istream &os, modular<T> &_m) {
    os >> _m.val;
    _m *= 1;
    return os;
  }
  friend std::ostream &operator<<(std::ostream &os, const modular<T> &_m) {
    return os << _m.val;
  }
  template <typename U> modular<T> exp(U e) {
    modular<T> res = 1;
    modular<T> b = val;
    if (e < 0) {
      b = 1 / b;
      e *= -1;
    }
    for (; e; e >>= 1) {
      if (e & 1) {
        res *= b;
      }
      b *= b;
    }
    return res;
  }

private:
  T val;
};
template <typename T>
inline modular<T> operator+(const modular<T> &_lhs, const modular<T> &_rhs) {
  return modular<T>(_lhs) += _rhs;
}
template <typename T, typename U>
inline modular<T> operator+(const modular<T> &_lhs, const U &_rhs) {
  return modular<T>(_lhs) += _rhs;
}
template <typename T, typename U>
inline modular<T> operator+(const U &_lhs, const modular<T> &_rhs) {
  return modular<T>(_lhs) += _rhs;
}
template <typename T>
inline modular<T> operator-(const modular<T> &_lhs, const modular<T> &_rhs) {
  return modular<T>(_lhs) -= _rhs;
}
template <typename T, typename U>
inline modular<T> operator-(const modular<T> &_lhs, const U &_rhs) {
  return modular<T>(_lhs) -= _rhs;
}
template <typename T, typename U>
inline modular<T> operator-(const U &_lhs, const modular<T> &_rhs) {
  return modular<T>(_lhs) -= _rhs;
}
template <typename T>
inline modular<T> operator*(const modular<T> &_lhs, const modular<T> &_rhs) {
  return modular<T>(_lhs) *= _rhs;
}
template <typename T, typename U>
inline modular<T> operator*(const modular<T> &_lhs, const U &_rhs) {
  return modular<T>(_lhs) *= _rhs;
}
template <typename T, typename U>
inline modular<T> operator*(const U &_lhs, const modular<T> &_rhs) {
  return modular<T>(_lhs) *= _rhs;
}
template <typename T>
inline modular<T> operator/(const modular<T> &_lhs, const modular<T> &_rhs) {
  return modular<T>(_lhs) /= _rhs;
}
template <typename T, typename U>
inline modular<T> operator/(const modular<T> &_lhs, const U &_rhs) {
  return modular<T>(_lhs) /= _rhs;
}
template <typename T, typename U>
inline modular<T> operator/(const U &_lhs, const modular<T> &_rhs) {
  return modular<T>(_lhs) /= _rhs;
}
template <typename T>
inline bool operator==(const modular<T> &_lhs, const modular<T> &_rhs) {
  return _lhs.val == _rhs.val;
}
template <typename T, typename U>
inline bool operator==(const modular<T> &_lhs, const U &_rhs) {
  return _lhs == modular<T>(_rhs);
}
template <typename T, typename U>
inline bool operator==(const U &_lhs, const modular<T> &_rhs) {
  return modular<T>(_lhs) == _rhs;
}
template <typename T>
inline bool operator!=(const modular<T> &_lhs, const modular<T> &_rhs) {
  return !(_lhs == _rhs);
}
template <typename T, typename U>
inline bool operator!=(const modular<T> &_lhs, const U &_rhs) {
  return !(_lhs == _rhs);
}
template <typename T, typename U>
inline bool operator!=(const U &_lhs, const modular<T> &_rhs) {
  return !(_lhs == _rhs);
}
typedef modular<int> mint;

const int N = 305;
mint dp[N][N][N];

void solve() {
  int n, m;
  cin >> n >> m;
  vector<vector<pair<int, int>>> g(n + 1);
  for (int _ = 0; _ < m; _++) {
    int l, r, x;
    cin >> l >> r >> x;
    g[r].emplace_back(l, x);
  }

  auto ck = [&](vector<int> a) {
    sort(a.begin(), a.end());
    for (auto &p : g[a[2]]) {
      int l, x;
      tie(l, x) = p;
      if (l > a[1]) {
        if (x != 1)
          return false;
      } else if (l > a[0]) {
        if (x != 2)
          return false;
      } else if (x != 3)
        return false;
    }
    return true;
  };

  dp[0][0][0] = 1;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      for (int k = 0; k < n; k++) {
        int r = max({i, j, k});
        if (ck({r + 1, j, k}))
          dp[r + 1][j][k] += dp[i][j][k];
        if (ck({r + 1, i, k}))
          dp[i][r + 1][k] += dp[i][j][k];
        if (ck({r + 1, j, i}))
          dp[i][j][r + 1] += dp[i][j][k];
      }
    }
  }
  mint res = 0;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      res += dp[i][j][n];
      res += dp[i][n][j];
      res += dp[n][i][j];
    }
  }
  cout << res;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  solve();
  cout << endl;
}


Fixed submission:
#include <bits/stdc++.h>

using namespace std;

const int MOD = 1000000007; // 998244353; // 998244853;

template <typename T> struct modular {
  constexpr modular() : val(0) {}
  constexpr modular(const modular<T> &_m) : val(_m.val) {}
  template <typename U> constexpr modular(const U &_r = U()) {
    val = -MOD <= _r && _r < MOD ? _r : _r % MOD;
    if (val < 0) {
      val += MOD;
    }
  }
  const T operator()() { return val; }
  template <typename U> explicit operator U() const {
    return static_cast<U>(val);
  }
  modular<T> &operator+=(const modular<T> &_m) {
    if ((val += _m.val) >= MOD) {
      val -= MOD;
    }
    return *this;
  }
  modular<T> &operator-=(const modular<T> &_m) {
    if ((val -= _m.val) < 0) {
      val += MOD;
    }
    return *this;
  }
  modular<T> &operator*=(const modular<T> &_m) {
    val = modular<T>(static_cast<int64_t>(val) * static_cast<int64_t>(_m.val))
              .val;
    return *this;
  }
  modular<T> &operator/=(const modular<T> &_m) {
    T a = _m.val, b = MOD, u = 0, v = 1;
    while (a != 0) {
      T q = b / a;
      b -= q * a;
      swap(a, b);
      u -= q * v;
      swap(u, v);
    }
    return *this *= u;
  }
  modular<T> &operator=(const modular<T> &_m) {
    val = _m.val;
    return *this;
  }
  template <typename U> modular<T> &operator+=(const U &_r) {
    return *this += modular<T>(_r);
  }
  template <typename U> modular<T> &operator-=(const U &_r) {
    return *this -= modular<T>(_r);
  }
  template <typename U> modular<T> &operator*=(const U &_r) {
    return *this *= modular<T>(_r);
  }
  template <typename U> modular<T> &operator/=(const U &_r) {
    return *this /= modular<T>(_r);
  }
  template <typename U> modular<T> &operator=(const U &_r) {
    val = modular<T>(_r).val;
    return *this;
  }
  modular<T> operator-() { return modular<T>(-val); }
  template <typename U>
  friend bool operator==(const modular<U> &, const modular<U> &);
  friend std::istream &operator>>(std::istream &os, modular<T> &_m) {
    os >> _m.val;
    _m *= 1;
    return os;
  }
  friend std::ostream &operator<<(std::ostream &os, const modular<T> &_m) {
    return os << _m.val;
  }
  template <typename U> modular<T> exp(U e) {
    modular<T> res = 1;
    modular<T> b = val;
    if (e < 0) {
      b = 1 / b;
      e *= -1;
    }
    for (; e; e >>= 1) {
      if (e & 1) {
        res *= b;
      }
      b *= b;
    }
    return res;
  }

private:
  T val;
};
template <typename T>
inline modular<T> operator+(const modular<T> &_lhs, const modular<T> &_rhs) {
  return modular<T>(_lhs) += _rhs;
}
template <typename T, typename U>
inline modular<T> operator+(const modular<T> &_lhs, const U &_rhs) {
  return modular<T>(_lhs) += _rhs;
}
template <typename T, typename U>
inline modular<T> operator+(const U &_lhs, const modular<T> &_rhs) {
  return modular<T>(_lhs) += _rhs;
}
template <typename T>
inline modular<T> operator-(const modular<T> &_lhs, const modular<T> &_rhs) {
  return modular<T>(_lhs) -= _rhs;
}
template <typename T, typename U>
inline modular<T> operator-(const modular<T> &_lhs, const U &_rhs) {
  return modular<T>(_lhs) -= _rhs;
}
template <typename T, typename U>
inline modular<T> operator-(const U &_lhs, const modular<T> &_rhs) {
  return modular<T>(_lhs) -= _rhs;
}
template <typename T>
inline modular<T> operator*(const modular<T> &_lhs, const modular<T> &_rhs) {
  return modular<T>(_lhs) *= _rhs;
}
template <typename T, typename U>
inline modular<T> operator*(const modular<T> &_lhs, const U &_rhs) {
  return modular<T>(_lhs) *= _rhs;
}
template <typename T, typename U>
inline modular<T> operator*(const U &_lhs, const modular<T> &_rhs) {
  return modular<T>(_lhs) *= _rhs;
}
template <typename T>
inline modular<T> operator/(const modular<T> &_lhs, const modular<T> &_rhs) {
  return modular<T>(_lhs) /= _rhs;
}
template <typename T, typename U>
inline modular<T> operator/(const modular<T> &_lhs, const U &_rhs) {
  return modular<T>(_lhs) /= _rhs;
}
template <typename T, typename U>
inline modular<T> operator/(const U &_lhs, const modular<T> &_rhs) {
  return modular<T>(_lhs) /= _rhs;
}
template <typename T>
inline bool operator==(const modular<T> &_lhs, const modular<T> &_rhs) {
  return _lhs.val == _rhs.val;
}
template <typename T, typename U>
inline bool operator==(const modular<T> &_lhs, const U &_rhs) {
  return _lhs == modular<T>(_rhs);
}
template <typename T, typename U>
inline bool operator==(const U &_lhs, const modular<T> &_rhs) {
  return modular<T>(_lhs) == _rhs;
}
template <typename T>
inline bool operator!=(const modular<T> &_lhs, const modular<T> &_rhs) {
  return !(_lhs == _rhs);
}
template <typename T, typename U>
inline bool operator!=(const modular<T> &_lhs, const U &_rhs) {
  return !(_lhs == _rhs);
}
template <typename T, typename U>
inline bool operator!=(const U &_lhs, const modular<T> &_rhs) {
  return !(_lhs == _rhs);
}
typedef modular<int> mint;

const int N = 305;
mint dp[N][N][N];

void solve() {
  int n, m;
  cin >> n >> m;
  vector<vector<pair<int, int>>> g(n + 1);
  for (int _ = 0; _ < m; _++) {
    int l, r, x;
    cin >> l >> r >> x;
    g[r].emplace_back(l, x);
  }

  auto ck = [&](vector<int> a) {
    sort(a.begin(), a.end());
    for (auto &p : g[a[2]]) {
      int l, x;
      tie(l, x) = p;
      if (l > a[1]) {
        if (x != 1)
          return false;
      } else if (l > a[0]) {
        if (x != 2)
          return false;
      } else if (x != 3)
        return false;
    }
    return true;
  };

  dp[0][0][0] = 1;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j <= i; j++) {
      for (int k = 0; k <= i; k++) {
        if (ck({i + 1, j, k}))
          dp[i + 1][j][k] += dp[i][j][k];
        if (ck({i, i + 1, k}))
          dp[i + 1][i][k] += dp[i][j][k];
        if (ck({i, j, i + 1}))
          dp[i + 1][i][j] += dp[i][j][k];
      }
    }
  }
  mint res = 0;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      res += dp[i][j][n];
      res += dp[i][n][j];
      res += dp[n][i][j];
    }
  }
  cout << res;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  solve();
  cout << endl;
}