Problem:
Score : 800 points

Problem Statement

There are N squares arranged in a row.
The squares are numbered 1, 2, ..., N, from left to right.

Snuke is painting each square in red, green or blue.
According to his aesthetic sense, the following M conditions must all be satisfied.
The i-th condition is:

There are exactly x_i different colors among squares l_i, l_i + 1, ..., r_i.

In how many ways can the squares be painted to satisfy all the conditions?
Find the count modulo 10^9+7.

Constraints

1 ≤ N ≤ 300

1 ≤ M ≤ 300

1 ≤ l_i ≤ r_i ≤ N

1 ≤ x_i ≤ 3

Input

Input is given from Standard Input in the following format:

N M
l_1 r_1 x_1
l_2 r_2 x_2
:
l_M r_M x_M

Output

Print the number of ways to paint the squares to satisfy all the conditions, modulo 10^9+7.

Sample Input 1

3 1
1 3 3

Sample Output 1

6

The six ways are:

RGB

RBG

GRB

GBR

BRG

BGR

where R, G and B correspond to red, green and blue squares, respectively.

Sample Input 2

4 2
1 3 1
2 4 2

Sample Output 2

6

The six ways are:

RRRG

RRRB

GGGR

GGGB

BBBR

BBBG

Sample Input 3

1 3
1 1 1
1 1 2
1 1 3

Sample Output 3

0

There are zero ways.

Sample Input 4

8 10
2 6 2
5 5 1
3 5 2
4 7 3
4 4 1
2 3 1
7 7 1
1 5 2
1 7 3
3 4 2

Sample Output 4

108

Solution in C++14 (GCC 5.4.1) (judge verdict: Accepted)

#include <bits/stdc++.h>
using namespace std;

using VI = vector<int>;
using VVI = vector<VI>;
using PII = pair<int, int>;
using LL = long long;
using VL = vector<LL>;
using VVL = vector<VL>;
using PLL = pair<LL, LL>;
using VS = vector<string>;

#define ALL(a)  begin((a)),end((a))
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define EB emplace_back
#define MP make_pair
#define SZ(a) int((a).size())
#define SORT(c) sort(ALL((c)))
#define RSORT(c) sort(RALL((c)))
#define UNIQ(c) (c).erase(unique(ALL((c))), end((c)))

#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n)  FOR(i,0,n)

#define FF first
#define SS second
template<class S, class T>
istream& operator>>(istream& is, pair<S,T>& p){
  return is >> p.FF >> p.SS;
}
template<class S, class T>
ostream& operator<<(ostream& os, const pair<S,T>& p){
  return os << p.FF << " " << p.SS;
}
template<class T>
void maxi(T& x, T y){
  if(x < y) x = y;
}
template<class T>
void mini(T& x, T y){
  if(x > y) x = y;
}


const double EPS = 1e-10;
const double PI  = acos(-1.0);
const LL MOD = 1e9+7;

int N, M;
LL dp[301][301][301];

int main(){
  cin.tie(0);
  ios_base::sync_with_stdio(false);

  cin >> N >> M;
  vector<vector<PII>> ban(301);
  REP(i,M){
	int l, r, x;
	cin >> l >> r >> x;
	ban[r].EB(l,x);
  }

  dp[0][0][0] = 1;
  REP(r,N) REP(g,N) REP(b,N){
	int k = max({r, g, b});
	(dp[k+1][g][b] += dp[r][g][b]) %= MOD;
	(dp[r][k+1][b] += dp[r][g][b]) %= MOD;
	(dp[r][g][k+1] += dp[r][g][b]) %= MOD;

	for(auto&& p: ban[k+1]){
	  int nr = (p.FF <= r);
	  int ng = (p.FF <= g);
	  int nb = (p.FF <= b);
	  if(1+ng+nb != p.SS)
		dp[k+1][g][b] = 0;
	  if(nr+1+nb != p.SS)
		dp[r][k+1][b] = 0;
	  if(nr+ng+1 != p.SS)
		dp[r][g][k+1] = 0;
	}
  }

  LL ans = 0;
  REP(i,N) REP(j,N) (ans += dp[N][i][j] + dp[i][N][j] + dp[i][j][N]) %= MOD;
  cout << ans << endl;
  
  return 0;
}