Count ways to split 3N students into teams of three so all M same-team and different-team pairs hold, modulo 1e9+9.
Hard8CombinatoricsUnion-findBacktrackingNo attempts yetTime limit3sMemory limit256 MBYou coach the ICPC club at your university. The club has 3N students, and you have to build N teams for the next ICPC. Every ICPC team has exactly 3 members, and each student belongs to exactly one team.
When you build the teams you have to account for several relationships among the students. Two students who get along extremely well perform much better when they share a team. The opposite happens for a pair that gets along badly. So two students with a good relationship must be placed on the same team, and two students with a bad relationship must be placed on different teams. As the coach you know all M relationships among the students.
Write a program that counts the team assignments satisfying every condition. Two assignments are different if and only if there is a pair of students who share a team in one assignment and do not share a team in the other.
The first line contains two integers N and M (1≤N≤106, 1≤M≤18). The i-th of the next M lines contains three integers Ai, Bi (1≤Ai,Bi≤3N, Ai=Bi) and Ci (Ci∈{0,1}). Ai and Bi are student numbers and Ci is the relationship type. If Ci is 0, student Ai and student Bi have a good relationship. If Ci is 1, they have a bad relationship. For all 1≤i,j≤M with i=j, {Ai,Bi}={Aj,Bj} holds.
Print the number of valid team assignments modulo 109+9 on one line.