#include #include using namespace std; using ll = long long; using ull = unsigned long long; const static int MOD = 1000000007; ull RepeatSquaring(ull N, ull P, ull mod){ if(P == 0 || N == 1) return 1; if(N == 0) return 0; if(N < 0) return P & 1 ? -RepeatSquaring(-N, P, mod) : RepeatSquaring(-N, P, mod); if(!(P & 1)){ ll t = RepeatSquaring(N, P >> 1, mod); return t*t %mod; } //Pが偶数のときN^(P/2)を求めてからそれの2乗を返す return N*RepeatSquaring(N, P - 1, mod) % mod; //Pが奇数のときN^((P - 1)/2)を求めてからそれの2乗を返す } int main(){ ull N; unsigned M; scanf("%llu %u", &N, &M); vector kaijou((N<<1)+1); kaijou[0] = 1; for (ull i = 0; i < N<<1; i++) { kaijou[i+1] = kaijou[i]*(i+1)%MOD; } vector rev_kaijou((N<<1)+1); rev_kaijou[N<<1] = RepeatSquaring(kaijou[N<<1], MOD - 2, MOD); for (ll i = (N<<1)-1; i >= 0; i--) { rev_kaijou[i] = rev_kaijou[i+1]*(i+1)%MOD; } ull answer = (N<<1)*kaijou[N<<1]%MOD*rev_kaijou[N]%MOD*rev_kaijou[N]%MOD; for (unsigned i = 0; i < M; i++) { unsigned t, x, y; scanf("%u %u %u", &t, &x, &y); answer = answer + MOD - (t == 1 ? kaijou[x+y]*rev_kaijou[x]%MOD*rev_kaijou[y]%MOD*kaijou[(N<<1) - x - y - 1]%MOD*rev_kaijou[N - x - 1]%MOD*rev_kaijou[N - y]%MOD : kaijou[x+y]*rev_kaijou[x]%MOD*rev_kaijou[y]%MOD*kaijou[(N<<1) - x - y - 1]%MOD*rev_kaijou[N - y - 1]%MOD*rev_kaijou[N - x]%MOD); if(answer >= MOD) answer -= MOD; } printf("%llu\n", answer); return 0; }