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 (GCC 5.4.1) (judge verdict: Accepted)

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define ll long long
#define rep(i,l,r)for(ll i=(l);i<(r);i++)
#define repp(i,l,r,k)for(ll i=(l);i<(r);i+=(k))
#define INF ((1LL<<62)-(1LL<<31))
#define max(p,q)((p)>(q)?(p):(q))
#define min(p,q)((p)<(q)?(p):(q))
#define bit(n,m)(((n)>>(m))&1)
int upll(const void*a, const void*b){return*(ll*)a<*(ll*)b?-1:*(ll*)a>*(ll*)b?1:0;}
int downll(const void*a, const void*b){return*(ll*)a<*(ll*)b?1:*(ll*)a>*(ll*)b?-1:0;}
void sortup(ll*a,int n){qsort(a,n,sizeof(ll),upll);}
void sortdown(ll*a,int n){qsort(a,n,sizeof(ll),downll);}
ll pom(ll a,ll n,int m){ll x=1;for(a%=m;n;n/=2)n&1?x=x*a%m:0,a=a*a%m;return x;}
//#define MOD 998244353
#define MOD 1000000007
#define invp(a,p)pom(a,p-2,p)



typedef struct t{ll l,r,x;}T;
int sort(const void*a,const void*b){
	T*p=(T*)a,*q=(T*)b;
	if((*p).r<(*q).r)return -1;
	return 1;
}

ll dp[310][310][310];
T c[310];
#define add(p,q)(p=(p+q)%MOD)
int main(){
	ll n,m;
	scanf("%lld%lld",&n,&m);
	rep(i,0,m){
		ll l,r,x;
		scanf("%lld%lld%lld",&l,&r,&x);
		c[i].l=l;
		c[i].r=r;
		c[i].x=x;
	}
	qsort(c,m,sizeof(T),sort);
	
	ll cnt=0;
	while(c[cnt].r==1){
		if(c[cnt].x>1){
			puts("0");
			return 0;
		}
		cnt++;
	}
	
	dp[1][0][0]=dp[0][1][0]=dp[0][0][1]=1;
	rep(i,2,n+1){
		//i番目を決める
		rep(j,0,i-1)rep(k,0,i-1){
			add(dp[i  ][j][k],dp[i-1][j][k]);
			add(dp[i-1][i][k],dp[i-1][j][k]);
			add(dp[i-1][j][i],dp[i-1][j][k]);
			add(dp[i][i-1][k],dp[j][i-1][k]);
			add(dp[j][i  ][k],dp[j][i-1][k]);
			add(dp[j][i-1][i],dp[j][i-1][k]);
			add(dp[i][k][i-1],dp[j][k][i-1]);
			add(dp[j][i][i-1],dp[j][k][i-1]);
			add(dp[j][k][i  ],dp[j][k][i-1]);
		}
		while(c[cnt].r==i){
			rep(j,0,i)rep(k,0,i){
				int temp=1;
				if(j>=c[cnt].l)temp++;
				if(k>=c[cnt].l)temp++;
				if(temp!=c[cnt].x){
					dp[i][j][k]=0;
					dp[i][k][j]=0;
					dp[j][i][k]=0;
					dp[k][i][j]=0;
					dp[j][k][i]=0;
					dp[k][j][i]=0;
				}
			}
			cnt++;
		}
	}
	ll ans=0;
	rep(j,0,n)rep(k,0,n){
		add(ans,dp[n][j][k]);
		add(ans,dp[j][n][k]);
		add(ans,dp[j][k][n]);
	}
	printf("%lld",ans);
}