Problem: 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.

Examples

Input

3 1
1 3 3


Output

6


Input

4 2
1 3 1
2 4 2


Output

6


Input

1 3
1 1 1
1 1 2
1 1 3


Output

0


Input

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


Output

108

Solution (in CPP):

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

#define for_(i,a,b) for(int i=(a);i<(b);++i)
typedef pair< int, int > pii;
template< typename T > using Vec = vector< T >;

int N, M, dp[301][301][301];
Vec< pii > seg[301];

const int MOD = (int)1e9 + 7;
void updateDP(int &a, int b) {
	if (a == -1 || b == -1) return;
	a = (a + b) % MOD;
}

bool validate(int r, int g, int b) {
	for (pii p : seg[r]) {
		int cnt = 1 + (p.first <= g) + (p.first <= b);
		if (p.second != cnt) return false;
	}
	return true;
}

int main() {
	cin >> N >> M;
	for_(i,0,M) {
		int l, r, x;
		cin >> l >> r >> x;
		seg[r].push_back(pii(l, x));
	}
	
	memset(dp, 0, sizeof(dp));
	for_(mx,0,N+1) for_(a,0,mx) for_(b,0,mx) {
		if (!validate(mx, a, b)) {
			dp[mx][a][b] = -1;
			dp[a][mx][b] = -1;
			dp[a][b][mx] = -1;
		}
	}
	
	dp[0][0][0] = 1;
	for_(r,0,N) for_(g,0,N) for_(b,0,N) {
		int k = max(r, max(g, b)), num = dp[r][g][b];
		updateDP(dp[k+1][g][b], num);
		updateDP(dp[r][k+1][b], num);
		updateDP(dp[r][g][k+1], num);
	}
	
	int ans = 0;
	for_(i,0,N) for_(j,0,N) {
		updateDP(ans, dp[N][i][j]);
		updateDP(ans, dp[i][N][j]);
		updateDP(ans, dp[i][j][N]);
	}
	
	cout << ans << endl;
}