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

#define DEBUG(x) cout<<#x<<": "<<x<<endl;
#define DEBUG_VEC(v) cout<<#v<<":";for(int i=0;i<v.size();i++) cout<<" "<<v[i]; cout<<endl

typedef long long ll;
#define vi vector<int>
#define vl vector<ll>
#define vii vector< vector<int> >
#define vll vector< vector<ll> >
#define vs vector<string>
#define pii pair<int,int>
#define pis pair<int,string>
#define psi pair<string,int>
#define pll pair<ll,ll>
#define fi first
#define se second
#define rep(i,n) for(int i=0;i<(int)(n);i++)
#define rep1(i,n) for(int i=1;i<=(int)(n);i++)
#define all(c) c.begin(),c.end()
const int inf = 1000000001;
const ll INF = 2e18 * 2;
#define MOD 1000000007
#define mod 1000000009
#define pi 3.14159265358979323846
#define Sp(p) cout<<setprecision(15)<< fixed<<p<<endl;
int dx[4] = { 1,0,-1,0 }, dy[4] = { 0,1,0,-1 };
int dx2[8] = { 1,1,0,-1,-1,-1,0,1 }, dy2[8] = { 0,1,1,1,0,-1,-1,-1 };

int main() {
	int n, m, i, j, k;
	cin >> n >> m;
	vector<pair<pll, ll> > rlx(m);
	for (i = 0; i < m; i++) {
		cin >> rlx[i].first.second >> rlx[i].first.first >> rlx[i].second;

	}
	sort(rlx.begin(), rlx.end());

	/*
	rep(i, m) {
	cout << rlx[i].first.first << " " << rlx[i].first.second << " " << rlx[i].second << endl;
	}
	cout << endl;
	//*/

	vector<vll> rgb(n + 1, vll(n + 1, vl(n + 1)));
	vector<vector<vector<bool> > > used(n + 1, vector<vector<bool> >(n + 1, vector<bool>(n + 1)));
	rgb[0][0][0] = 1;
	j = 0;
	rep1(i, n) {
		rep(r, i) {
			rep(g, i) {
				if (!used[r][g][i - 1]) {
					(rgb[i][g][i - 1] += rgb[r][g][i - 1]) %= MOD;
					(rgb[r][i][i - 1] += rgb[r][g][i - 1]) %= MOD;
					(rgb[r][g][i] += rgb[r][g][i - 1]) %= MOD;
					used[r][g][i - 1] = true;
				}
			}
			rep(b, i) {
				if (!used[r][i - 1][b]) {
					(rgb[i][i - 1][b] += rgb[r][i - 1][b]) %= MOD;
					(rgb[r][i - 1][i] += rgb[r][i - 1][b]) %= MOD;
					(rgb[r][i][b] += rgb[r][i - 1][b]) %= MOD;
					used[r][i - 1][b] = true;
				}
			}
		}
		rep(g, i) {
			rep(b, i) {
				if (!used[i - 1][g][b]) {
					(rgb[i][g][b] += rgb[i - 1][g][b]) %= MOD;
					(rgb[i - 1][i][b] += rgb[i - 1][g][b]) %= MOD;
					(rgb[i - 1][g][i] += rgb[i - 1][g][b]) %= MOD;
					used[i - 1][g][b] = true;
				}
			}
		}



		while (j < m && i == rlx[j].first.first) {
			int x = rlx[j].second;
			int l = rlx[j].first.second;
			if (x == 1) {
				for (int g = l; g <= i; g++) {
					for (int b = 0; b <= i; b++) {
						rgb[i][g][b] = rgb[i][b][g] = 0;
						rgb[g][i][b] = rgb[b][i][g] = 0;
						rgb[g][b][i] = rgb[b][g][i] = 0;
					}
				}
			}
			else if (x == 2) {
				for (int g = l; g <= i; g++) {
					for (int b = l; b <= i; b++) {
						rgb[i][g][b] = rgb[i][b][g] = 0;
						rgb[g][i][b] = rgb[b][i][g] = 0;
						rgb[g][b][i] = rgb[b][g][i] = 0;
					}
				}
				for (int g = 0; g < l; g++) {
					for (int b = 0; b < l; b++) {
						rgb[i][g][b] = rgb[i][b][g] = 0;
						rgb[g][i][b] = rgb[b][i][g] = 0;
						rgb[g][b][i] = rgb[b][g][i] = 0;
					}
				}
			}
			else {
				for (int g = 0; g <= i; g++) {
					for (int b = 0; b < l; b++) {
						rgb[i][g][b] = rgb[i][b][g] = 0;
						rgb[g][i][b] = rgb[b][i][g] = 0;
						rgb[g][b][i] = rgb[b][g][i] = 0;
					}
				}
			}
			j++;
		}

		/*
		rep(r, i + 1) {
		rep(g, i + 1) {
		rep(b, i + 1) {
		cout << rgb[r][g][b] << " ";
		}
		cout << endl;
		}
		cout << endl;
		}
		//*/
	}
	ll ans = 0;
	rep(g, n + 1) {
		rep(b, n + 1) {
			(ans += rgb[n][g][b]) %= MOD;
			(ans += rgb[g][n][b]) %= MOD;
			(ans += rgb[b][g][n]) %= MOD;
		}
	}
	cout << ans << endl;
}

Measured memory_bytes: 226048