#include <bits/stdc++.h>
      
#define FOR(i,a,b) for( ll i = (a); i < (ll)(b); i++ )
#define REP(i,n) FOR(i,0,n)
#define YYS(x,arr) for(auto& x:arr)
#define ALL(x) (x).begin(),(x).end()
#define SORT(x) sort( (x).begin(),(x).end() )
#define REVERSE(x) reverse( (x).begin(),(x).end() )
#define UNIQUE(x) (x).erase( unique( ALL( (x) ) ) , (x).end() )
#define PW(x) (1LL<<(x))
#define SZ(x) ((ll)(x).size())
#define SHOW(x) cout << #x << " = " << x << endl
#define SHOWA(x,n) for( int yui = 0; yui < n; yui++ ){ cout << x[yui] << " "; } cout << endl

#define pb emplace_back
#define fi first
#define se second

using namespace std;

typedef long double ld;
typedef long long int ll;
typedef pair<ll,ll> pl;
typedef vector<ll> vl;
typedef vector<bool> vb;
typedef vector<ld> vd;
typedef vector<pl> vpl;
typedef vector<vpl> gr;
typedef vector<vl> ml;
typedef vector<vd> md;
     
const ll INF = (ll)1e9 + 10;
const ll INFLL = (ll)1e18 + 10;
const ll MOD = 1e9+7;
     
template<class T> T &chmin( T &a , const T &b ){ return a = min(a,b); }
template<class T> T &chmax( T &a , const T &b ){ return a = max(a,b); }
template<class T> inline T sq( T a ){ return a * a; }

ll in(){ long long int x; scanf( "%lld" , &x ); return x; }
char yuyushiki[1000010]; string stin(){ scanf( "%s" , yuyushiki ); return yuyushiki; }

// head

struct Mod{
  unsigned n;
  Mod() : n(0){}
  Mod( ll x ){
    if( x < 0 ) n = x%MOD+MOD;
    else n = x%MOD;
  }
};
Mod operator + ( Mod a  , Mod b ){ return Mod( a.n + b.n ); }
Mod operator +=( Mod &a , Mod b ){ return a = a + b; }
Mod operator - ( Mod a ){ return Mod( MOD - a.n ); }
Mod operator - ( Mod a  , Mod b ){ return Mod( a.n + MOD - b.n ); }
Mod operator -=( Mod &a , Mod b ){ return a = a - b; }
Mod operator * ( Mod a  , Mod b ){ return Mod( (ll)a.n * b.n ); }
Mod operator *=( Mod &a , Mod b ){ return a = a * b; }
Mod modpow( Mod x , ll k ){
  Mod res = 1;
  while( k ){
    if( k & 1 ) res *= x;
    k /= 2;
    x *= x;
  }
  return res;
}
ll extgcd( ll a , ll b , ll &x , ll &y ){
  ll d = a;
  if( b != 0 ){
    d = extgcd( b , a % b , y , x );
    y -= a / b * x;
  } else {
    x = 1, y = 0;
  }
  return d;
}
Mod inv( Mod a ){ ll x, y; assert( extgcd( a.n , MOD , x , y ) == 1 ); return Mod( x ); }
Mod operator / ( Mod a  , Mod b ){ return Mod( (ll)a.n * inv(b).n ); }
Mod operator /=( Mod &a , Mod b ){ return a = a / b; }


using T = tuple<ll, ll, ll>;

ll n, m;

vector<T> v;

Mod dp[310][310][310];

Mod one(ll i, ll j, ll k, ll l, ll r){
  ll c = 1;
  if(k <= l){
    c = 3;
  } else if(j <= l){
    c = 2;
  }
  ll nj = max(j, r);
  ll nk = max(k, nj);
  return dp[i+1][nj][nk] * c;
}

Mod two(ll i, ll j, ll k, ll l, ll r){
  Mod res = 1;
  if(r <= j){
    res = 0;
  } else {
    ll nj = max(l, j);
    ll nk = max(k, r);
    if(l < j){
      res = dp[i+1][nj][nk] - dp[i+1][r][nk];
      if(k < r){
        res += dp[i+1][k][r] - dp[i+1][r][r];
      }
    } else if(l < k){
      res = dp[i+1][nj][nk] - 2 * dp[i+1][r][nk];
      if(k < r){
        res += (dp[i+1][k][r] - dp[i+1][r][r]) * 2;
      }
    } else {
      res = (dp[i+1][nj][nk] - 2 * dp[i+1][r][nk]) * 3;
    }
  }
  return res;
}


int main(){

  n = in();
  m = in();
  REP( i , m ){
    ll l = in() - 1;
    ll r = in();
    ll x = in();
    v.pb(l, r, x);
  }

  SORT(v);

  for(ll j = 0; j <= n; j++){
    for(ll k = j; k <= n; k++){
      dp[m][j][k] = modpow(2, k-j) * modpow(3, n-k);
    }
  }
  
  for(ll i = m-1; i >= 0; i--){
    ll l, r, x;
    tie(l, r, x) = v[i];
    REP( j , n+1 ){
      FOR( k , j , n+1 ){
        dp[i][j][k] = 1;
        if(j <= l){
          dp[i][j][k] *= modpow(2, min(k, l) - j);
          dp[i][j][k] *= modpow(3, l - min(k, l));
        }
        if(x == 1){
          dp[i][j][k] *= one(i, j, k, l, r);
        } else if(x == 2){
          dp[i][j][k] *= two(i, j, k, l, r);
        } else if(x == 3){
          ll nj = max(j, l);
          ll nk = max(nj, k);
          dp[i][j][k] *= dp[i+1][nj][nk] - two(i, j, k, l, r) - one(i, j, k, l, r);
        }
        /*
        if(dp[i][j][k].n){
          cout << i << " " << j << " " << k << " " << dp[i][j][k].n << endl;
        }
        */
      }
    }
  }
  
  cout << dp[0][0][0].n << endl;
  
  return 0;
}


Measured memory_bytes: 117120