Language: C++

#include "bits/stdc++.h"
using namespace std;


const int mod = 1000000009;
struct Mod {
public:
	int num;
	Mod() : Mod(0) { ; }
	Mod(long long int n) : num((n % mod + mod) % mod) {
		static_assert(mod<INT_MAX / 2, "mod is too big, please make num 'long long int' from 'int'");
	}
	Mod(int n) : Mod(static_cast<long long int>(n)) { ; }
	operator int() { return num; }
};

Mod operator+(const Mod a, const Mod b) { return Mod((a.num + b.num) % mod); }
Mod operator+(const long long int a, const Mod b) { return Mod(a) + b; }
Mod operator+(const Mod a, const long long int  b) { return b + a; }
Mod operator++(Mod &a) { return a + Mod(1); }
Mod operator-(const Mod a, const Mod b) { return Mod((mod + a.num - b.num) % mod); }
Mod operator-(const long long int a, const Mod b) { return Mod(a) - b; }
Mod operator--(Mod &a) { return a - Mod(1); }
Mod operator*(const Mod a, const Mod b) { return Mod(((long long)a.num * b.num) % mod); }
Mod operator*(const long long int a, const Mod b) { return Mod(a)*b; }
Mod operator*(const Mod a, const long long int b) { return Mod(b)*a; }
Mod operator*(const Mod a, const int b) { return Mod(b)*a; }
Mod operator+=(Mod &a, const Mod b) { return a = a + b; }
Mod operator+=(long long int &a, const Mod b) { return a = a + b; }
Mod operator-=(Mod &a, const Mod b) { return a = a - b; }
Mod operator-=(long long int &a, const Mod b) { return a = a - b; }
Mod operator*=(Mod &a, const Mod b) { return a = a * b; }
Mod operator*=(long long int &a, const Mod b) { return a = a * b; }
Mod operator*=(Mod& a, const long long int &b) { return a = a * b; }
Mod operator^(const Mod a, const int n) {
	if (n == 0) return Mod(1);
	Mod res = (a * a) ^ (n / 2);
	if (n % 2) res = res * a;
	return res;
}
Mod mod_pow(const Mod a, const int n) {
	if (n == 0) return Mod(1);
	Mod res = mod_pow((a * a), (n / 2));
	if (n % 2) res = res * a;
	return res;
}
Mod inv(const Mod a) { return a ^ (mod - 2); }
Mod operator/(const Mod a, const Mod b) {
	assert(b.num != 0);
	return a * inv(b);
}
Mod operator/(const long long int a, const Mod b) {
	return Mod(a) / b;
}
Mod operator/=(Mod &a, const Mod b) {
	return a = a / b;
}

#define MAX_MOD_N 3024000

Mod fact[MAX_MOD_N], factinv[MAX_MOD_N];
void init() {
	fact[0] = Mod(1); factinv[0] = 1;
	for (int i = 0; i < MAX_MOD_N - 1; ++i) {
		fact[i + 1] = fact[i] * Mod(i + 1);
		factinv[i + 1] = factinv[i] / Mod(i + 1);
	}
}
Mod comb(const int a, const int b) {
	assert(a >= b);
	assert(a >= 0 && b >= 0);
	return fact[a] * factinv[b] * factinv[a - b];
}

struct rel {
	int a;
	int b;
	int love;
};

struct UnionFind {
	vector<int> data;
	UnionFind(int size) : data(size, -1) { }
	bool unionSet(int x, int y) {
		x = root(x); y = root(y);
		if (x != y) {
			if (data[y] < data[x]) swap(x, y);
			data[x] += data[y]; data[y] = x;
		}
		return x != y;
	}
	bool findSet(int x, int y) {
		return root(x) == root(y);
	}
	int root(int x) {
		return data[x] < 0 ? x : data[x] = root(data[x]);
	}
	int size(int x) {
		return -data[root(x)];
	}
};
vector<Mod>dp(1e6 + 10);
Mod getrest(const int rest) {
	if (rest < 0) {
		return 0;
	}
	else {
		if (rest % 3) {
			assert(false);
			return 0;
		}
		else {
			return dp[rest/3];
		}
	}
}
int N, M;
Mod getans(const map<int,int>&mp) {
	vector<int>v;

	Mod ans = 1;
	for (auto m : mp) {
		if (m.second >= 4)return 0;
		else if(m.second==1){
		}
		else {
			v.emplace_back(m.second);
		}
	}
	int rest = 3 * N - accumulate(v.begin(), v.end(),0);
	if (N < v.size())return 0;
	for (auto n : v) {
		ans *= comb(rest, 3 - n);
		rest -= (3-n);
	}
	ans *= getrest(rest);
	return ans;
}
int main() {
	dp[0] = 1;
	for (int i = 1; i <= 1e6+1; ++i) {
		dp[i] = dp[i - 1] * Mod(3*i - 1)*Mod(3*i - 2) / Mod(2);
	}
	init();cin >> N >> M;
	vector<rel>rels;
	vector<rel>hates;
	set<int>aset;
	for (int i = 0; i < M; ++i) {
		int a, b, c; cin >> a >> b >> c;
		c = !c;
		a--; b--;
		aset.emplace(a);
		aset.emplace(b);
		rels.push_back(rel{ a,b,c });
		if (find(rels.begin(), rels.end(), rel{ b,a,c }) != rels.end()) {
			//cout << "a" << endl;
			return 0;
		}
		if (find(rels.begin(), rels.end(), rel{ b,a,!c }) != rels.end()) {
			//cout << "a" << endl;
			return 0;
		}
	}
	map<int, int>mp;
	int n = 0;
	for (auto s : aset) {
		mp[s] = n;
		n++;
	}
	for (auto&r : rels) {
		r.a = mp[r.a];
		r.b = mp[r.b];
	}
	UnionFind uf(aset.size());
	for (int i = 0; i < M; ++i) {
		if (rels[i].love) {
			uf.unionSet(rels[i].a, rels[i].b);
		}
		else {
			hates.emplace_back(rels[i]);
		}
	}
	assert(hates.size() <= 18);
	Mod ans = 0;
	for (int i = 0; i < (1<<hates.size()); ++i) {
		UnionFind nowuf(uf);
		bitset<18>bs(i);
		for (int j = 0; j < hates.size(); ++j) {
			
			if (bs[j]) {
				nowuf.unionSet(hates[j].a, hates[j].b);
			}
		}
		map<int, int>lovemp;
		for (int j = 0; j < aset.size(); ++j) {
			lovemp[nowuf.root(j)]++;
		}
		if (bs.count() % 2) {
			ans -= getans(lovemp);
		}
		else {
			ans += getans(lovemp);
		}
	}
	cout << ans << endl;
	return 0;
}

Compiler output:
In file included from /usr/include/c++/14/bits/stl_algobase.h:71,
                 from /usr/include/c++/14/algorithm:60,
                 from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:51,
                 from a.cc:1:
/usr/include/c++/14/bits/predefined_ops.h: In instantiation of 'bool __gnu_cxx::__ops::_Iter_equals_val<_Value>::operator()(_Iterator) [with _Iterator = __gnu_cxx::__normal_iterator<rel*, std::vector<rel> >; _Value = const rel]':
/usr/include/c++/14/bits/stl_algobase.h:2107:14:   required from '_RandomAccessIterator std::__find_if(_RandomAccessIterator, _RandomAccessIterator, _Predicate, random_access_iterator_tag) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<rel*, vector<rel> >; _Predicate = __gnu_cxx::__ops::_Iter_equals_val<const rel>]'
 2107 |           if (__pred(__first))
      |               ~~~~~~^~~~~~~~~
/usr/include/c++/14/bits/stl_algobase.h:2152:23:   required from '_Iterator std::__find_if(_Iterator, _Iterator, _Predicate) [with _Iterator = __gnu_cxx::__normal_iterator<rel*, vector<rel> >; _Predicate = __gnu_cxx::__ops::_Iter_equals_val<const rel>]'
 2152 |       return __find_if(__first, __last, __pred,
      |              ~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
 2153 |                        std::__iterator_category(__first));
      |                        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_algo.h:3850:28:   required from '_IIter std::find(_IIter, _IIter, const _Tp&) [with _IIter = __gnu_cxx::__normal_iterator<rel*, vector<rel> >; _Tp = rel]'
 3850 |       return std::__find_if(__first, __last,
      |              ~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~
 3851 |                             __gnu_cxx::__ops::__iter_equals_val(__val));
      |                             ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
a.cc:155:11:   required from here
  155 |                 if (find(rels.begin(), rels.end(), rel{ b,a,c }) != rels.end()) {
      |                     ~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/predefined_ops.h:270:24: error: no match for 'operator==' (operand types are 'rel' and 'const rel')
  270 |         { return *__it == _M_value; }
      |                  ~~~~~~^~~~~~~~~~~
In file included from /usr/include/c++/14/bits/stl_algobase.h:67:
/usr/include/c++/14/bits/stl_iterator.h:1208:5: note: candidate: 'template<class _IteratorL, class _IteratorR, class _Container> bool __gnu_cxx::operator==(const __normal_iterator<_IteratorL, _Container>&, const __normal_iterator<_IteratorR, _Container>&)'
 1208 |     operator==(const __normal_iterator<_IteratorL, _Container>& __lhs,
      |     ^~~~~~~~
/usr/include/c++/14/bits/stl_iterator.h:1208:5: note:   template argument deduction/substitution failed:
/usr/include/c++/14/bits/predefined_ops.h:270:24: note:   'rel' is not derived from 'const __gnu_cxx::__normal_iterator<_IteratorL, _Container>'
  270 |         { return *__it == _M_value; }
      |                  ~~~~~~^~~~~~~~~~~
/usr/include/c++/14/bits/stl_iterator.h:1216:5: note: candidate: 'template<class _Iterator, class _Container> bool __gnu_cxx::operator==(const __normal_iterator<_Iterator, _Container>&, const __normal_iterator<_Iterator, _Container>&)'
 1216 |     operator==(const __normal_iterator<_Iterator, _Container>& __lhs,
      |     ^~~~~~~~
/usr/include/c++/14/bits/stl_iterator.h:1216:5: note:   template argument deduction/substitution failed:
/usr/include/c++/14/bits/predefined_ops.h:270:24: note:   'rel' is not derived from 'const __gnu_cxx::__normal_iterator<_Iterator, _Container>'
  270 |         { return *__it == _M_value; }
      |                  ~~~~~~^~~~~~~~~~~