#pragma GCC optimize("Ofast")
#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>

#define F first
#define S second
//#define int long long
#define ll long long
//#define int  unsigned long long
#define pb push_back
//#define double long double

using namespace std;
using namespace __gnu_pbds;
typedef  tree< int , null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;
const int mod = 1e9 + 7;
const int N = 303;
const int K = 20;
int dp[2][4][N][N], b[N][N];
vector < pair < int, int > > con[N];
main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    //freopen("input.txt", "r", stdin);
    int n, m;
    cin >> n >> m;
    for (int i = 1; i <= m; i++){
        int l, r, x;
        cin >> l >> r >> x;
        if (b[l][r] != 0 && b[l][r] != x){
            cout << 0;
            return 0;
        }
        b[l][r] = x;
        con[r].pb({l, x});
    }
    dp[1][1][0][0] = 1;
    dp[1][2][0][0] = 1;
    dp[1][3][0][0] = 1;
    for (int i = 1; i <= n; i++){
        int c = i % 2;
        for (int j = 0; j <= n; j++){
            for (int l = 0; l <= n; l++){
                dp[c ^ 1][1][j][l] = dp[c ^ 1][2][j][l] = dp[c ^ 1][3][j][l] = 0;
            }
        }
        for (int j = 0; j < i; j++){
            for (int l = 0; l < i; l++){
                int x = dp[c][1][j][l];
                int y = dp[c][2][j][l];
                int z = dp[c][3][j][l];
                for (auto k: con[i]){
                    int lb = k.F;
                    if (k.S == 1){
                        if (j >= lb || l >= lb){
                            dp[c][1][j][l] = 0;
                            dp[c][2][j][l] = 0;
                            dp[c][3][j][l] = 0;
                            x = y = z = 0;
                        }
                    }
                    if (k.S == 2){
                        if ((j >= lb && l >= lb) || (j < lb && l < lb)){
                            dp[c][1][j][l] = 0;
                            dp[c][2][j][l] = 0;
                            dp[c][3][j][l] = 0;
                            x = y = z = 0;
                        }
                    }
                    if (k.S == 3){
                        if (j < lb || l < lb){
                            dp[c][1][j][l] = 0;
                            dp[c][2][j][l] = 0;
                            dp[c][3][j][l] = 0;
                            x = y = z = 0;
                        }
                    }
                }
               // cout << i << " " << j << " " << l << " " << x << " " << y << " " << z << endl;
                dp[c ^ 1][1][j][l] = (dp[c ^ 1][1][j][l] + x) % mod;
                dp[c ^ 1][2][j][l] = (dp[c ^ 1][2][j][l] + y) % mod;
                dp[c ^ 1][3][j][l] = (dp[c ^ 1][3][j][l] + z) % mod;
                dp[c ^ 1][2][l][i] = (dp[c ^ 1][2][l][i] + x) % mod;
                dp[c ^ 1][3][i][j] = (dp[c ^ 1][3][i][j] + x) % mod;
                dp[c ^ 1][3][l][i] = (dp[c ^ 1][3][l][i] + y) % mod;
                dp[c ^ 1][1][i][j] = (dp[c ^ 1][1][i][j] + y) % mod;
                dp[c ^ 1][1][l][i] = (dp[c ^ 1][1][l][i] + z) % mod;
                dp[c ^ 1][2][i][j] = (dp[c ^ 1][2][i][j] + z) % mod;
            }
        }
    }
    int ans = 0;
    for (int i = 1; i <= 3; i++){
        for (int j = 0; j <= n; j++){
            for (int l = 0; l <= n; l++){
                ans = (ans + dp[n % 2][i][j][l]) % mod;
            }
        }
    }
    cout << ans;
}


Measured memory_bytes: 2816