#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; typedef long long ll; typedef unsigned int ui; const ll mod = 1000000007; const ll INF = (ll)1000000007 * 1000000007; typedef pair P; #define stop char nyaa;cin>>nyaa; #define rep(i,n) for(int i=0;i=0;i--) #define Rep(i,sta,n) for(int i=sta;i=sta;i--) #define rep1(i,n) for(int i=1;i<=n;i++) #define per1(i,n) for(int i=n;i>=1;i--) #define Rep1(i,sta,n) for(int i=sta;i<=n;i++) typedef long double ld; const ld eps = 1e-8; const ld pi = acos(-1.0); typedef pair LP; int dx[4]={1,-1,0,0}; int dy[4]={0,0,1,-1}; struct T{ int l,r,t; T(){} T(int l,int r,int t):l(l),r(r),t(t){} }; struct E{ int to,c; E(){} E(int to,int c):to(to),c(c){} }; int n,m,L[200010]; int cnt[200010],cnt2[200010]; vector z; vector q1; vector G[200010]; vector col; bool flag=true; template struct ModInt { long long x; ModInt() : x(0) {} ModInt(long long y) : x(y >= 0 ? y % mod : (mod - (-y) % mod) % mod) {} explicit operator int() const {return x;} ModInt &operator+=(const ModInt &p) { if((x += p.x) >= mod) x -= mod; return *this; } ModInt &operator-=(const ModInt &p) { if((x += mod - p.x) >= mod) x -= mod; return *this; } ModInt &operator*=(const ModInt &p) { x = (int)(1LL * x * p.x % mod); return *this; } ModInt &operator/=(const ModInt &p) { *this *= p.inverse(); return *this; } ModInt operator-() const { return ModInt(-x); } ModInt operator+(const ModInt &p) const { return ModInt(*this) += p; } ModInt operator-(const ModInt &p) const { return ModInt(*this) -= p; } ModInt operator*(const ModInt &p) const { return ModInt(*this) *= p; } ModInt operator/(const ModInt &p) const { return ModInt(*this) /= p; } bool operator==(const ModInt &p) const { return x == p.x; } bool operator!=(const ModInt &p) const { return x != p.x; } ModInt inverse() const{ int a = x, b = mod, u = 1, v = 0, t; while(b > 0) { t = a / b; a -= t * b; swap(a, b); u -= t * v; swap(u, v); } return ModInt(u); } ModInt power(long long p) const{ int a = x; if (p==0) return 1; if (p==1) return ModInt(a); if (p%2==1) return (ModInt(a)*ModInt(a)).power(p/2)*ModInt(a); else return (ModInt(a)*ModInt(a)).power(p/2); } ModInt power(const ModInt p) const{ return ((ModInt)x).power(p.x); } friend ostream &operator<<(ostream &os, const ModInt &p) { return os << p.x; } friend istream &operator>>(istream &is, ModInt &a) { long long x; is >> x; a = ModInt(x); return (is); } }; using modint = ModInt; template struct SegmentTree{ using F = function; int n; F f;//二項演算 T ti;//単位元 vector dat; SegmentTree(){} SegmentTree(F f,T ti):f(f),ti(ti){} void init(int n_){//sizeがn_のsegtreeを作る n=1; while(n &v){//vによってsegtreeをbuildする int n_=v.size(); init(n_); for(int i=0;i>=1) dat[k]=f(dat[(k<<1)|0],dat[(k<<1)|1]); } T query(int a,int b){//区間[a,b)に対しFを適応した値を返す if(a>=b) return ti; T vl=ti,vr=ti; for(int l=a+n,r=b+n;l>=1,r>>=1) { if(l&1) vl=f(vl,dat[l++]); if(r&1) vr=f(dat[--r],vr); } return f(vl,vr); } template int find(int st,C &check,T &acc,int k,int l,int r){// if(l+1==r){ acc=f(acc,dat[k]); return check(acc)?k-n:-1; } int m=(l+r)>>1; if(m<=st) return find(st,check,acc,(k<<1)|1,m,r); if(st<=l&&!check(f(acc,dat[k]))){ acc=f(acc,dat[k]); return -1; } int vl=find(st,check,acc,(k<<1)|0,l,m); if(~vl) return vl; return find(st,check,acc,(k<<1)|1,m,r); } template int find(int st,C &check){ T acc=ti; return find(st,check,acc,1,0,n); } T &operator [] (int i) {return dat[i+n];}; }; int comp(int x){ return lower_bound(z.begin(),z.end(),x)-z.begin(); } void dfs(int s,int co){ //cout << s << " " << co << endl; col[s]=co; for(E e:G[s]){ int t=e.to,c=e.c; if(col[t]==-1) dfs(t,(col[s]+c)%2); else if((col[s]+c)%2!=col[t]){ flag=false; } } } void solve(){ cin >> n >> m; rep(i,n+1){ L[i]=-1; } rep(i,m){ int l,r,t;cin >> l >> r >> t; if(t!=0){ cnt[l]+=1; cnt[r+1]-=1; if(t==1)q1.push_back(T(l,r,0)); if(t==-1)q1.push_back(T(l,r,1)); } else{ L[r]=l; } } rep(i,n){ cnt[i+1]+=cnt[i]; cnt2[i+1]+=cnt2[i]; } z.push_back(0); rep(i,n+1){ if(cnt[i]) z.push_back(i); } int k=z.size(); for(T q:q1){ G[comp(q.l)-1].push_back(E(comp(q.r),q.t)); G[comp(q.r)].push_back(E(comp(q.l)-1,q.t)); } col.resize(k,-1); int num=0; rep(i,k){ if(col[i]!=-1) continue; dfs(i,0); num+=1; } if(!flag){ cout << 0 << endl; return; } //cout << num << endl; int pos=0; SegmentTree seg([](modint a,modint b){return a+b;},0); seg.init(n+1); seg.set_val(0,((modint)2).power(n)); int v=0; rep(i,n){ if(cnt[i+1]==0) seg.set_val(i+1,seg.query(pos,i+1)/(modint)2); else v+=1; pos=max(pos,L[i+1]); //cout << i+1 << " " << pos << " " << seg.query(i+1,i+2) << endl; } cout << seg.query(pos,n+1)/((modint)2).power(v-num+1) << endl; } int main(){ ios::sync_with_stdio(false); cin.tie(0); cout << fixed << setprecision(50); solve(); }