//code by lynmisakura.wish to be accepted!
/*************************/
#include<iostream>
#include<iomanip>
#include<math.h>
#include<vector>
#include<string>
#include<stack>
#include<queue>
#include<map>
#include<algorithm>
#include<bitset>
#include <climits>
#include<set>
#include<bitset>
using namespace std;
/***************************/
typedef long long ll;
typedef long long ijt;
typedef vector<int> vi;
typedef vector<long long> vl;
typedef pair<int, int> pi;
typedef vector<pair<int, int>> vpi;

const long long INF = 1LL << 55;

#define itn int
#define endl '\n'
#define pb push_back
#define mp make_pair
#define ss second
#define ff first
#define dup(x,y) ((x) + (y) - 1)/(y)
#define mins(x,y) x = min(x,y)
#define maxs(x,y) x = max(x,y)
#define all(x) (x).begin(),(x).end()
#define Rep(n) for(int i = 0;i < n;i++)
#define rep(i,n) for(int i = 0;i < n;i++)
#define rrep(i,n) for(int i = n - 1;i >= 0;i--)
#define UNIQUE(v) v.erase( unique(v.begin(), v.end()), v.end() )

ll gcd(ll a, ll b) { return b ? gcd(b, a%b) : a; }
ll qp(ll a, ll b) { ll ans = 1; do { if (b & 1)ans = 1ll * ans*a; a = 1ll * a*a; } while (b >>= 1); return ans; }
ll qp(ll a, ll b, int mo) { ll ans = 1; do { if (b & 1)ans = 1ll * ans*a%mo; a = 1ll * a*a%mo; } while (b >>= 1); return ans; }

#define _GLIBCXX_DEBUG
#define debug(x) cerr << #x << " = " << (x) << " (L" << __LINE__ << ")" << " " << __FILE__ << endl;
//mint
const int mod = 1e9+7;
struct mint {
	ll x; // typedef long long ll;
	mint(ll x = 0) :x(x%mod) {}
	mint& operator+=(const mint a) {
		if ((x += a.x) >= mod) x -= mod;
		return *this;
	}
	mint& operator-=(const mint a) {
		if ((x += mod - a.x) >= mod) x -= mod;
		return *this;
	}
	mint& operator*=(const mint a) {
		(x *= a.x) %= mod;
		return *this;
	}
	mint operator+(const mint a) const {
		mint res(*this);
		return res += a;
	}
	mint operator-(const mint a) const {
		mint res(*this);
		return res -= a;
	}
	mint operator*(const mint a) const {
		mint res(*this);
		return res *= a;
	}
	mint pow(ll t) const {
		if (!t) return 1;
		mint a = pow(t >> 1);
		a *= a;
		if (t & 1) a *= *this;
		return a;
	}

	// for prime mod
	mint inv() const {
		return pow(mod - 2);
	}
	mint& operator/=(const mint a) {
		return (*this) *= a.inv();
	}
	mint operator/(const mint a) const {
		mint res(*this);
		return res /= a;
	}
};
mint dp[310][310][310];
vector<pair<int,int>> G[310];
int main(void) {
	int n,m;cin >> n >> m;
	for (int i = 0; i < m; i++)
	{
		int a,b,x;cin >> a >> b >> x;
		G[b].pb(mp(a,x));
	}
	dp[0][0][0] = 1;
	for (int r = 0; r <= n; r++)
	{
		for (int g = 0; g <= n; g++)
		{
			for (int b = 0; b <= n; b++)
			{
				if(r != 0 and g != 0 and r == g)continue;
				if(g != 0 and b != 0 and g == b)continue;
				if(b != 0 and r != 0 and b == r)continue;

				int k = max({r,g,b});
				bool check = true;
				for (int j = 0; j < G[k].size(); j++)
				{
					int l = G[k][j].first;
					int s = G[k][j].second;
					
					
					vector<int> h = {r,g,b};sort(all(h));
					int mid = h[1];
					int low = h[0];
					if (s == 3) {
						if (!(low >= l)) {
							check = false;
							break;
						}
					}else if (s == 2) {
						if (!(mid >= l and low < l)) {
							check = false;
							break;
						}
					}else {
						if (!(mid < l)) {
							check = false;
							break;
						}
					}


				}
				if (!check) {
					dp[r][g][b] = 0;
				}
				else {
					
					dp[k + 1][g][b] += dp[r][g][b];
					dp[r][k + 1][b] += dp[r][g][b];
					dp[r][g][k + 1] += dp[r][g][b];
				}
			}
		}
	}
	
	mint ans = 0;
	for (int r = 0; r < n; r++)
	{
		for (int g = 0; g < n; g++)
		{
			if(r != 0 and r == g) continue;
			ans += dp[r][g][n];
		}
	}
	ans *= (mint)3;
	cout << ans.x << endl;
	return 0;
}

Measured memory_bytes: 233592