ICPC Teams

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 MB

Problem

You coach the ICPC club at your university. The club has 3N3N students, and you have to build NN 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 MM 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.

Input

The first line contains two integers NN and MM (1N1061 \le N \le 10^6, 1M181 \le M \le 18). The ii-th of the next MM lines contains three integers AiA_i, BiB_i (1Ai,Bi3N1 \le A_i, B_i \le 3N, AiBiA_i \ne B_i) and CiC_i (Ci{0,1}C_i \in \{0, 1\}). AiA_i and BiB_i are student numbers and CiC_i is the relationship type. If CiC_i is 0, student AiA_i and student BiB_i have a good relationship. If CiC_i is 1, they have a bad relationship. For all 1i,jM1 \le i, j \le M with iji \ne j, {Ai,Bi}{Aj,Bj}\{A_i, B_i\} \ne \{A_j, B_j\} holds.

Output

Print the number of valid team assignments modulo 109+910^9 + 9 on one line.