// eddy1021
#pragma GCC optimize("O3")
#include <bits/stdc++.h>
using namespace std;
typedef double D;
typedef long double LD;
typedef long long LL;
typedef pair<int,int> PII;
typedef pair<LL,LL> PLL;
#define mod9 1000000009LL
#define mod7 1000000007LL
#define INF  1023456789LL
#define INF16 10000000000000000LL
#define eps 1e-9
#define SZ(x) (int)(x).size()
#define ALL(x) (x).begin(), (x).end()
#ifndef ONLINE_JUDGE
#define debug(...) printf(__VA_ARGS__)
#else 
#define debug(...)
#endif
inline LL getint(){
  LL _x=0,_tmp=1; char _tc=getchar();    
  while( (_tc<'0'||_tc>'9')&&_tc!='-' ) _tc=getchar();
  if( _tc == '-' ) _tc=getchar() , _tmp = -1;
  while(_tc>='0'&&_tc<='9') _x*=10,_x+=(_tc-'0'),_tc=getchar();
  return _x*_tmp;
}
inline LL add( LL _x , LL _y , LL _mod = mod7 ){
  _x += _y;
  return _x >= _mod ? _x - _mod : _x;
}
inline LL sub( LL _x , LL _y , LL _mod = mod7 ){
  _x -= _y;
  return _x < 0 ? _x + _mod : _x;
}
inline LL mul( LL _x , LL _y , LL _mod = mod7 ){
  _x *= _y;
  return _x >= _mod ? _x % _mod : _x;
}
LL mypow( LL _a , LL _x , LL _mod ){
  if( _x == 0 ) return 1LL;
  LL _ret = mypow( mul( _a , _a , _mod ) , _x >> 1 , _mod );
  if( _x & 1 ) _ret = mul( _ret , _a , _mod );
  return _ret;
}
LL mymul( LL _a , LL _x , LL _mod ){
  if( _x == 0 ) return 0LL;
  LL _ret = mymul( add( _a , _a , _mod ) , _x >> 1 , _mod );
  if( _x & 1 ) _ret = add( _ret , _a , _mod );
  return _ret;
}
inline bool equal( D _x ,  D _y ){
  return _x > _y - eps && _x < _y + eps;
}
#define Bye exit(0)
int __ = 1 , _cs;
/*********default*********/
#define N 333
void build(){

}
int n , m;
vector< pair<int,int> > rule[ N ];
void init(){
  n = getint();
  m = getint();
  while( m -- ){
    int li = getint();
    int ri = getint();
    int xi = getint();
    rule[ ri ].push_back( { li , xi } );
    if( li == 1 and ri == 1 and xi > 1 ){
      puts( "0" );
      Bye;
    }
  }
}
int dp[ 2 ][ N ][ N ];
void solve(){
  dp[ 1 ][ 1 ][ 0 ] = 3;
  for( int i = 2 ; i <= n ; i ++ ){
    int now = i & 1 , pre = 1 - now;
    for( int j = 1 ; j <= i ; j ++ )
      for( int k = 0 ; k <= i ; k ++ )
        dp[ now ][ j ][ k ] = 0;
    for( int j = 1 ; j < i ; j ++ )
      for( int k = 0 ; k < j ; k ++ ){
        if( dp[ pre ][ j ][ k ] == 0 ) continue;
        { // same as a[ pre ]
          int nxtj = j;
          int nxtk = k;
          bool ok = true;
          for( auto rr : rule[ i ] ){
            int kd = 1;
            if( rr.first < j and j > 1 ) kd ++;
            if( rr.first < k and k > 1 ) kd ++;
            if( kd != rr.second ){
              ok = false;
              break;
            }
          }
          if( ok )
            dp[ now ][ nxtj ][ nxtk ] = add( dp[ now ][ nxtj ][ nxtk ] ,
                                             dp[ pre ][ j ][ k ] );
        }
        { // same as the second one
          int nxtj = i;
          int nxtk = k;
          bool ok = true;
          for( auto rr : rule[ i ] ){
            int kd = 1;
            if( rr.first < i and i > 1 ) kd ++;
            if( rr.first < k and k > 1 ) kd ++;
            if( kd != rr.second ){
              ok = false;
              break;
            }
          }
          if( ok )
            dp[ now ][ nxtj ][ nxtk ] = add( dp[ now ][ nxtj ][ nxtk ] ,
                                             dp[ pre ][ j ][ k ] );
        }
        { // same as the other
          int nxtj = i;
          int nxtk = j;
          bool ok = true;
          for( auto rr : rule[ i ] ){
            int kd = 1;
            if( rr.first < i and i > 1 ) kd ++;
            if( rr.first < j and j > 1 ) kd ++;
            if( kd != rr.second ){
              ok = false;
              break;
            }
          }
          if( ok )
            dp[ now ][ nxtj ][ nxtk ] = add( dp[ now ][ nxtj ][ nxtk ] ,
                                             dp[ pre ][ j ][ k ] );
        }
      }
  }
  int ans = 0;
  for( int i = 0 ; i <= n ; i ++ )
    for( int j = 0 ; j <= n ; j ++ )
      ans = add( ans , dp[ n & 1 ][ i ][ j ] );
  printf( "%d\n" , ans );
}
int main(){
  build();
  //__ = getint();
  while( __ -- ){
    init();
    solve();
  }
}


Measured memory_bytes: 1024