#include <iostream>
#include <map>
#include <set>
#include <cmath>
#include <algorithm>
#include <vector>
#include <string>
#include <fstream>
#include <bitset>
#include <queue>
#include <stack>
#include <deque>
#include <time.h>
#include <complex>

using std::cin;
using std::cout;
using std::cerr;
using std::endl;

using std::map;
using std::set;
using std::bitset;
using std::vector;
using std::string;
using std::multimap;
using std::multiset;
using std::deque;
using std::queue;
using std::stack;
using std::pair;
using std::iterator;
using std::complex;
using std::priority_queue;

using std::sort;
using std::stable_sort;
using std::reverse;
using std::max_element;
using std::min_element;
using std::unique;
using std::ios_base;
using std::swap;
using std::fill;

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<long long> vl;
typedef string S;
typedef complex<double> cd;

ll min(ll a, ll b)
{
    if (a < b)
        return a;
    return b;
}

ll max(ll a, ll b)
{
    if (a > b)
        return a;
    return b;
}

#define F(i, n) for (int (i) = 0; (i) != (n); (i)++)
#define fi first
#define se second
#define re return
#define all(x) (x).begin(), (x).end()
#define int long long

namespace MySpace{
    struct Small_Segment_Tree{
        vector<int> a;
        int l, r;

        private:
        int __summ(int L, int R, int l, int r, int v)
        {
            if (L <= l && r <= R)
                return a[v];
            if (R <= l || r <= L)
                return 0;
            int m = (l + r) / 2;
            return __summ(L, R, l, m, 2 * v + 1) + __summ(L, R, m, r, 2 * v + 2);
        }

        void __set(int P, int l, int r, int v, int V)
        {
            if (l + 1 == r)
            {
                a[v] = V;
                return;
            }
            int m = (l + r) / 2;
            if (P < m)
            {
                __set(P, l, m, 2 * v + 1, V);
                a[v] = a[2 * v + 1] + a[2 * v + 2];
            }
            else
            {
                __set(P, m, r, 2 * v + 2, V);
                a[v] = a[2 * v + 1] + a[2 * v + 2];
            }
        }

        public:
        int Summ(int L, int R)
        {
            if (R < L)
                return 0;
            return __summ(L, R + 1, l, r, 0);
        }

        void Set(int P, int V)
        {
            __set(P, l, r, 0, V);
        }

        Small_Segment_Tree(int _n)
        {
            a.resize(4 * _n);
            l = 0, r = _n;
        }
    };
    long long inq(long long x, long long q, long long MOD)
    {
        if (q == 0)
            return 1;
        long long l = inq(x, q / 2, MOD);
        if (q % 2)
            return l * l % MOD * x % MOD;
        return l * l % MOD;
    }
};


using namespace MySpace;

struct kewa1{
    vector<pii> a;
    void insert(int x)
    {
        if (a.size() == 0)
            a.push_back({x, x});
        else
            a.push_back({x, std::min(x, 1LL * a[a.size() - 1].second)});
    }
    int min()
    {
        if (a.size() == 0)
            return 1e9;
        else
            return a[a.size() - 1].second;
    }
    void erase()
    {
        if (a.size()) a.pop_back();
    }
    void pri()
    {
        for (int i = 0; i < a.size(); i++)
        {
            cout << a[i].first << " ";
        }
        cout << endl;
    }
};

struct kewa2{
    kewa1 a, b;
    void insert(int x)
    {
        b.insert(x);
    }
    void erase()
    {
        if (a.a.size() == 0)
        {
            while (b.a.size())
            {
                a.insert(b.a[b.a.size() - 1].first);
                b.erase();
            }
            //reverse(all(a.a));
        }
        a.erase();
    }
    int min()
    {
        return std::min(a.min(), b.min());
    }
    void pri()
    {
        cout << "a : ";
        a.pri();
        cout << "b : ";
        b.pri();
        cout << endl;
    }
};

void YN(int val)
{
    if (val >= 1)
    {
        printf("YES\n");
    }
    else
    {
        printf("NO\n");
    }
}

//const ll MOD = 998244353;
const ll MOD = 1e9 + 7;
//const ll MOD = 1e9 + 9;

const ll INF = 1e15;

int n, m;
vector<pair<int, int> > qs[301];
int psi[4][500];
int xsi[4][500];
int ok[301][301][301];
ll dp[2][3][301][301];

bool alo(int a, int b, int c)
{
    if (a == b && b == c) return 0;
    int d[3] = {a, b, c};
    sort(d, d + 3);
    if (a == b && a != 0) return 0;
    if (b == c && b != 0) return 0;
    if (a == c && a != 0) return 0;
    int t = 1;
    if (psi[1][d[2]] != 0)
    {
        if (psi[1][d[2]] > d[1])
        {
            ;
        }
        else
        {
            t = 0;
        }
    }
    if (psi[2][d[2]] != 0)
    {
        if (psi[2][d[2]] > d[0] && psi[2][d[2]] <= d[1])
        {
            ;
        }
        else
        {
            t = 0;
        }
    }
    if (psi[3][d[2]] != 0)
    {
        if (psi[3][d[2]] <= d[0])
        {
            ;
        }
        else
        {
            t = 0;
        }
    }
    if (xsi[1][d[2]] != 0)
    {
        if (xsi[1][d[2]] > d[1])
        {
            ;
        }
        else
        {
            t = 0;
        }
    }
    if (xsi[2][d[2]] != 0)
    {
        if (xsi[2][d[2]] > d[0] && xsi[2][d[2]] <= d[1])
        {
            ;
        }
        else
        {
            t = 0;
        }
    }
    if (xsi[3][d[2]] != 0)
    {
        if (xsi[3][d[2]] <= d[0])
        {
            ;
        }
        else
        {
            t = 0;
        }
    }
    return t;
}

signed main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);

    cin >> n >> m;
    for (int i = 0; i < m; i++)
    {
        int l, r, x;
        cin >> l >> r >> x;
        qs[r].push_back({l, x});
    }
    for (int i = 0; i <= n; i++)
    {
        qs[i].push_back({i, 1});
        sort(all(qs[i]));
        int minimals[4] = {0, 999999, 999999, 999999};
        int maximals[4] = {0, -1, -1, -1};
        for (int j = 0; j + 1 < qs[i].size(); j++)
        {
            if (qs[i][j + 1].second > qs[i][j].second)
            {
                cout << 0;
                return 0;
            }
        }
        for (int j = 0; j < qs[i].size(); j++)
        {
            minimals[qs[i][j].second] = min(minimals[qs[i][j].second], qs[i][j].first);
            maximals[qs[i][j].second] = max(maximals[qs[i][j].second], qs[i][j].first);
        }
        if (minimals[1] != 999999) psi[1][i] = minimals[1];
        if (minimals[2] != 999999) psi[2][i] = minimals[2];
        if (minimals[3] != 999999) psi[3][i] = minimals[3];
        if (maximals[3] != -1) xsi[3][i] = maximals[3];
        if (maximals[2] != -1) xsi[2][i] = maximals[2];
        if (maximals[1] != -1) xsi[1][i] = maximals[1];
    }
    for (int a = 0; a <= n; a++)
    {
        for (int b = 0; b <= n; b++)
        {
            for (int c = 0; c <= n; c++)
            {
                ok[a][b][c] = alo(a, b, c);
                if (ok[a][b][c])
                {
                    ;//cout << a << " " << b << " " << c << endl;
                }
            }
        }
    }
    if (ok[1][0][0])
    {
        dp[1][0][0][0] = 1;
        dp[1][1][0][0] = 1;
        dp[1][2][0][0] = 1;
    }
    for (int i = 1; i < n; i++)
    {
        for (int b = 0; b < n; b++)
        {
            for (int c = 0; c < n; c++)
            {
                dp[1 ^ (1 & i)][0][b][c] = 0;
                dp[1 ^ (1 & i)][1][b][c] = 0;
                dp[1 ^ (1 & i)][2][b][c] = 0;
            }
        }
        for (int b = 0; b < i; b++)
        {
            for (int c = 0; c < i; c++)
            {
                if (b == c && b != 0)
                    continue;
                for (int here = 0; here < 3; here++)
                {
                    int p[3];
                    if (here == 0)
                    {
                        p[0] = i, p[1] = b, p[2] = c;
                    }
                    if (here == 1)
                    {
                        p[0] = b, p[1] = i, p[2] = c;
                    }
                    if (here == 2)
                    {
                        p[0] = b, p[1] = c, p[2] = i;
                    }
                    for (int place = 0; place < 3; place++)
                    {
                        int g[3] = {p[0], p[1], p[2]};
                        g[place] = i + 1;
                        if (ok[g[0]][g[1]][g[2]])
                        {
                            ;//cout << "perexod " << endl;
                            ;//cout << i << " " << (i & 1) << " " <<  (1 ^ (1 & i)) << endl;
                            ;//cout << dp[(i & 1)][here][p[(here == 0) ? 1 : 0]][p[(here == 2) ? 1 : 2]] << endl;
                            dp[1 ^ (i & 1)][place][g[(place == 0) ? 1 : 0]][g[(place == 2) ? 1 : 2]] = (dp[1 ^ (i & 1)][place][g[(place == 0) ? 1 : 0]][g[(place == 2) ? 1 : 2]]
                            + dp[(i & 1)][here][p[(here == 0) ? 1 : 0]][p[(here == 2) ? 1 : 2]]) % MOD;
                        }
                    }
                }
            }
        }
    }
    ll ans = 0;
    for (int a = 0; a <= n; a++)
    {
        for (int b = 0; b <= n; b++)
        {
            for (int j = 0; j < 3; j++)
            {
                ans = (ans + dp[n & 1][j][a][b]) % MOD;
            }
        }
    }
    cout << ans;
}


Measured memory_bytes: 217600