#include #define rep(i,n) for (int i = 0; i < (n); ++i) #define chmax(a, b) a = max(a, b) #define chmin(a, b) a = min(a, b) #define all(x) (x).begin(), (x).end() using namespace std; using ll = long long; using P = pair; using VI = vector; using VVI = vector; #define MOD 1000000007 //#define MOD 998244353 struct mint { int i; mint() : i(0) {} mint(int x) { i = int(x % MOD); if (i < 0) i += MOD; } template mint(T x) { i = int(x % MOD); if (i < 0) i += MOD; } mint operator+(const mint x) const {return i + x.i;} mint operator-(const mint x) const {return i - x.i;} mint operator*(const mint x) const {return (long long)i * x.i;} mint operator/(const mint x) const {return (long long)i * x.pow(MOD - 2).i;} mint inv() {return pow(MOD - 2);} template mint pow(T p) const { long long r = 1; long long t = i; for(; p > 0; p >>= 1) { if (p & 1) r = r * t % MOD; t = t * t % MOD; } return r; } template static mint pow(T1 a, T2 b) { long long r = 1; long long t = (long long)(a % MOD); for(; b > 0; b >>= 1) { if (b & 1) r = r * t % MOD; t = t * t % MOD; } return r; } mint& operator+=(const mint x) { i = (i + x.i) % MOD; return *this; } mint& operator-=(const mint x) { i = i - x.i; if (i < 0) i += MOD; return *this; } mint& operator*=(const mint x) { i = (int)((long long)i * x.i % MOD); return *this; } mint& operator/=(const mint x) { i = (long long)i * x.pow(MOD - 2).i % MOD; return *this; } }; template struct BIT { int n; vector d; BIT(int n=0):n(n),d(n+1) {} void add(int i, T x=1) { for (i++; i <= n; i += i&-i) { d[i] += x; } } T sum(int i) { T x = 0; for (i++; i; i -= i&-i) { x += d[i]; } return x; } T sum(int l, int r) { return sum(r-1) - sum(l-1); } T operator[](int x) { return sum(x) - sum(x-1); } }; std::ostream& operator<<(std::ostream& os, const mint& m) { return os << m.i; } struct COND { int l; int r; COND(int l, int r):l(l),r(r){} }; int main() { int n, m; cin >> n >> m; vector c0, c1; rep(i, m) { int l, r, p; cin >> l >> r >> p; l--; r--; if (p == 0) c0.emplace_back(l, r); else c1.emplace_back(l, r); } map cover; for(auto x: c1) { int l = x.l, r = x.r, cnt = 1; auto it1 = cover.lower_bound(l); if (it1 != cover.begin()) { auto it0 = it1; it0--; if (l <= it0->second.first) { l = it0->first; cnt += it0->second.second; chmax(r, it0->second.first); cover.erase(it0); } } if (it1 != cover.end() && it1->first <= r) { chmax(r, it1->second.first); cnt += it1->second.second; cover.erase(it1); } cover[l] = {r, cnt}; } vector filled(n); mint ans = 1; for(auto x: cover) { int l = x.first, r = x.second.first, cnt = x.second.second; ans *= mint::pow(2, r - l + 1 - cnt); for(int i = l; i <= r; i++) filled[i] = 1; } int sz = c0.size(); { int idx = 0; vector> q(n); set alive; rep(i, sz) { q[c0[i].l].emplace_back(0, i); q[c0[i].r].emplace_back(1, i); } rep(i, n) { if (filled[i]) { for(auto p: q[i]) { if (p.first == 0) { c0[p.second].l = idx; alive.insert(p.second); } else { c0[p.second].r = idx - 1; alive.erase(p.second); } } } else { if (!alive.empty()) filled[i] = 1; for(auto p: q[i]) { filled[i] = 1; if (p.first == 0) { c0[p.second].l = idx; alive.insert(p.second); } else { c0[p.second].r = idx; alive.erase(p.second); } } idx++; } } } for(auto p: c0) { if (p.l > p.r) { cout << 0 << endl; return 0; } } sort(all(c0), [](COND x, COND y){return x.l < y.l;}); rep(i, sz) { int l = c0[i].l, r = c0[i].r; vector t; t.emplace_back(l, r); while(i + 1 < sz && c0[i+1].l <= r) { i++; t.emplace_back(c0[i].l, c0[i].r); chmax(r, c0[i].r); } for(int i = l; i <= r; i++) filled[i] = 1; sort(all(t), [](COND x, COND y){return x.r < y.r;}); BIT dp(r - l + 1 + 1); int nxtdel = 0, nxtcid = 0; mint tinv = mint(1) / 2; dp.add(0, mint::pow(2, r - l + 1)); rep(i, r - l + 1) { dp.add(i + 1, dp.sum(nxtdel, i + 1) * tinv); if (l + i == t[nxtcid].r) { chmax(nxtdel, t[nxtcid].l - l + 1); nxtcid++; } } mint acc = 0; mint now = 1; for(int i = r - l + 1; i >= nxtdel; i--) { acc += dp[i] * now; now *= 2; } ans *= acc; } rep(i, n) if (!filled[i]) ans *= 3; cout << ans << endl; }