/// c.cpp

# include <stdio.h>
# include <bits/stdc++.h>
using namespace std;
const pair < int , int > DD[] = {{1,0},{1,1},{0,1},{-1,1},{-1,0},{-1,-1},{0,-1},{1,-1}};
# define fi cin
# define fo cout
# define x first
# define y second
# define ll long long
# define IOS ios_base :: sync_with_stdio(0);cin.tie(0)
# define p(v) cerr << #v << " = " << v << '\n'
# define p2(v) cerr << #v << " = " << (complex < __typeof(v.x) > (v.x,v.y)) << '\n'
# define vi vector < int >
# define vl vector < ll >
# define pll pair < ll , ll >
# define pii pair < int , int >
# define mp make_pair
# define db long double
# define fail puts("-1")
# define yes puts("YES")
# define no puts("NO")
# define PP puts("Possible")
# define II puts("Impossible")
# define vii vector < pii >
# define vll vector < pll >
# define pb push_back
# define pdd pair < db , db >
template < class T > T smin(T &a,T b) {if (a > b) a = b;return a;}
template < class T > T smax(T &a,T b) {if (a < b) a = b;return a;}
int main(void)
{
    #ifdef CF
    freopen("input","r",stdin);
    #endif // CF
    srand(time(0));
    fo << fixed << setprecision(7);
    cerr << fixed << setprecision(7);
    const int mod = 1e9 + 7;
    int n,m;
    fi>>n>>m;
    static int dp[333][333][333];
    static vii w[333];
    while (m --)
    {
        int a,b,c;
        fi>>a>>b>>c;
        w[b].pb(mp(a,c));
    }
    for (auto it : w[1])
        if (it.y != 1)
            return puts("0") * 0;
    dp[1][0][0] = 3;
    auto add = [&](int &x,int y)
    {
        x = (x + y) % mod;
    };
    for (int i = 2;i <= n;++i)
    {
        for (int j = 0;j < i - 1;++j)
            for (int k = 0;k < i - 1;++k)
            {
                if (!j && !k)
                    add(dp[i][0][0],dp[i - 1][0][0]),add(dp[i][i - 1][0],(2ll * dp[i - 1][0][0]) % mod);
                else
                if (j && !k)
                {
                    add(dp[i][j][i - 1],dp[i - 1][j][k]);
                    add(dp[i][i - 1][0],dp[i - 1][j][k]);
                    add(dp[i][j][0],dp[i - 1][j][0]);
                }
                else
                if (j && k && j < k)
                {
                    add(dp[i][j][k],dp[i - 1][j][k]);
                    add(dp[i][k][i - 1],dp[i - 1][j][k]);
                    add(dp[i][j][i - 1],dp[i - 1][j][k]);
                }
            }
        for (auto it : w[i])
            for (int j = 0;j < i;++j)
                for (int k = 0;k < i;++k)
                {
                    if (!j && !k)
                    {
                        if (it.y != 1)
                            dp[i][j][k] = 0;
                    }
                    else
                    if (j && !k)
                    {
                        if (it.y == 1 && it.x <= j)
                            dp[i][j][k] = 0;
                        else
                        if (it.y == 2 && it.x > j)
                            dp[i][j][k] = 0;
                        else
                        if (it.y == 3)
                            dp[i][j][k] = 0;
                    }
                    else
                    if (j && k && j < k)
                    {
                        if (it.y == 1 && it.x <= k)
                            dp[i][j][k] = 0;
                        else
                        if (it.y == 2 && (it.x > k || it.x <= j))
                            dp[i][j][k] = 0;
                        else
                        if (it.y == 3 && it.x > j)
                            dp[i][j][k] = 0;
                    }
                }
    }
    int ans = 0;
    for (int i = 0;i < n;++i)
        for (int j = 0;j < n;++j)
            add(ans,dp[n][i][j]);
    fo << ans << '\n';
    cerr << "Time elapsed :" << clock() * 1000.0 / CLOCKS_PER_SEC << " ms" << '\n';
    return 0;
}


Measured memory_bytes: 129280