class Bit: def __init__(self, n): self.size = n self.tree = [0] * (n + 1) def sum(self, i): s = 0 while i > 0: s += self.tree[i] i -= i & -i return s def add(self, i, x): while i <= self.size: self.tree[i] += x i += i & -i N, M = map(int, input().split()) mod = 10**9+7 A = [0] * (N+1) r2l = [0] * (N+1) c = 0 for _ in range(M): l, r, p = map(int, input().split()) if p: A[l-1] += 1 A[r] -= 1 c += 1 else: r2l[r] = l bt = Bit(N+1) bt.add(1, pow(2, N, mod)) mxl = 0 m2=500000004 for i in range(1, N+1): A[i] += A[i-1] if A[i-1] == 0: bt.add(i+1, pow(2, N-i, mod)*pow(m2, N-i+1, mod)*(bt.sum(i) - bt.sum(mxl))%mod) mxl = max(mxl, r2l[i]) print((bt.sum(N+1)-bt.sum(mxl))*pow(m2, c, mod)%mod)