import sys def input(): return sys.stdin.buffer.readline()[:-1] MOD = 10**9+7 n, m = map(int, input().split()) two_pow = [1 for _ in range(n+2)] for i in range(1, n+2): two_pow[i] = (two_pow[i-1] * 2) % MOD zero_seg = [0 for _ in range(n+1)] nonzero_num = 0 nonzero_imos = [0 for _ in range(n+2)] for _ in range(m): l, r, p = map(int, input().split()) if p == 0: zero_seg[r] = l else: nonzero_num += 1 nonzero_imos[l] += 1 nonzero_imos[r+1] -= 1 for i in range(2, n+2): nonzero_imos[i] += nonzero_imos[i-1] dp = [0 for _ in range(n+1)] cum = [0 for _ in range(n+2)] #cum[-1]=cum[n+1]=0とするため(ズル) dp[0] = 1 cum[0] = 1 max_left = 0 for i in range(1, n+1): if nonzero_imos[i] == 0: dp[i] = (cum[i-1] - two_pow[i-max_left] * cum[max_left-1]) % MOD cum[i] = (2*cum[i-1] + dp[i]) % MOD max_left = max(max_left, zero_seg[i]) pre_ans = (cum[n] - two_pow[n-max_left+1] * cum[max_left-1]) % MOD ans = (pre_ans * pow(500000004, nonzero_num, MOD)) % MOD print(ans)