Example

Input

2 2
1 2 0
3 4 1


Output

2
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> l_l;
typedef pair<int, int> i_i;
template<class T>
inline bool chmax(T &a, T b) {
    if(a < b) {
        a = b;
        return true;
    }
    return false;
}

template<class T>
inline bool chmin(T &a, T b) {
    if(a > b) {
        a = b;
        return true;
    }
    return false;
}

const long double EPS = 1e-10;
const long long INF = 1e18;
const long double PI = acos(-1.0L);
const ll mod = 1000000009;
ll dp[2000000];
ll N, M;
vector<ll> A, B, C;

struct UnionFind {
    vector<int> par;
    vector<int> rank;
    vector<ll> Size;
    UnionFind(int n = 1) {
        init(n);
    }

    void init(int n = 1) {
        par.resize(n + 1); rank.resize(n + 1); Size.resize(n + 1);
        for (int i = 0; i <= n; ++i) par[i] = i, rank[i] = 0, Size[i] = 1;
    }

    int root(int x) {
        if (par[x] == x) {
            return x;
        }
        else {
            int r = root(par[x]);
            return par[x] = r;
        }
    }

    bool issame(int x, int y) {
        return root(x) == root(y);
    }

    bool merge(int x, int y) {
        x = root(x); y = root(y);
        if (x == y) return false;
        if (rank[x] < rank[y]) swap(x, y);
        if (rank[x] == rank[y]) ++rank[x];
        par[y] = x;
        Size[x] += Size[y];
        return true;
    }

    ll size(int x){
        return Size[root(x)];
    }
};

int main() {
    dp[0] = 1;
    for(ll i = 1; i <= 1e6; i++) {
        ll tmp = (3*i-1) * (3*i-2) / 2;
        tmp %= mod;
        tmp *= dp[i-1];
        tmp %= mod;
        dp[i] = tmp;
    }
    cin >> N >> M;
    A.resize(M), B.resize(M), C.resize(M);
    vector<ll> cmp;
    for(int i = 0; i < M; i++) {
        cin >> A[i] >> B[i] >> C[i];
        cmp.push_back(A[i]);
        cmp.push_back(B[i]);
    }
    sort(cmp.begin(), cmp.end());
    cmp.erase(unique(cmp.begin(), cmp.end()), cmp.end());
    for(int i = 0; i < M; i++) {
        auto itr = lower_bound(cmp.begin(), cmp.end(), A[i]);
        A[i] = itr - cmp.begin();
        itr = lower_bound(cmp.begin(), cmp.end(), B[i]);
        B[i] = itr - cmp.begin();
    }
    ll ans = 0;
    for(int bits = 0; bits < (1 << M); bits++) {
        ll tmp = 1;
        UnionFind uni(50);
        for(int i = 0; i < M; i++) {
            int a = (1 & (bits >> i));
            if(C[i] == 0 and a) {
                tmp = 0;
            } else if(C[i] == 1 and a) {
                uni.merge(A[i], B[i]);
            } else if(C[i] == 0) {
                uni.merge(A[i], B[i]);
            }
        }
        ll two = 0;
        ll three = 0;
        for(int i = 0; i < 50; i++) {
            if(uni.root(i) != i) continue;
            if(uni.size(i) > 3) tmp = 0;
            else if(uni.size(i) == 2) two++;
            else if(uni.size(i) == 3) three++;
        }
        ll one = 3 * N - 2*two - 3*three;
        while(two) {
            tmp *= one;
            tmp %= mod;
            two--;
            one--;
        }
        if(one >= 0) tmp *= dp[one / 3];
        tmp %= mod;
        //if(tmp) cerr << bits << " " << tmp << endl;
        if(__builtin_popcount(bits) % 2 == 0) ans += tmp;
        else ans += mod - tmp;
        ans %= mod;
    }
    cout << ans << endl;
    return 0;
}
