MOD = 10**9 + 7 def main(): import sys input = sys.stdin.read().split() ptr = 0 N = int(input[ptr]) ptr += 1 M = int(input[ptr]) ptr += 1 max_fac = 400000 + 10 # Precompute up to 4e5 +10 to handle all cases fac = [1] * (max_fac + 1) for i in range(1, max_fac + 1): fac[i] = fac[i-1] * i % MOD inv_fac = [1] * (max_fac + 1) inv_fac[max_fac] = pow(fac[max_fac], MOD - 2, MOD) for i in range(max_fac - 1, -1, -1): inv_fac[i] = inv_fac[i + 1] * (i + 1) % MOD def comb(a, b): if a < 0 or b < 0 or a < b: return 0 return fac[a] * inv_fac[b] % MOD * inv_fac[a - b] % MOD total_paths = comb(2 * N, N) total_cost = total_paths * 2 * N % MOD sum_free = 0 for _ in range(M): t = int(input[ptr]) ptr += 1 x = int(input[ptr]) ptr += 1 y = int(input[ptr]) ptr += 1 if t == 1: a = x + y way1 = comb(a, x) rem_r = N - (x + 1) rem_c = N - y c = rem_r + rem_c way2 = comb(c, rem_r) else: a = x + y way1 = comb(a, x) rem_r = N - x rem_c = N - (y + 1) c = rem_r + rem_c way2 = comb(c, rem_r) sum_free = (sum_free + way1 * way2) % MOD ans = (total_cost - sum_free) % MOD print(ans) if __name__ == "__main__": main()