Count the ways to label every remaining pair in a graph as like or dislike so that no triple has exactly two liking pairs, given some fixed edges.
Hard8GraphCombinatoricsUnion-findMathNo attempts yetTime limit2sMemory limit512 MBA love triangle appears in many dramas and films. Character A and character B like each other, character B and character C like each other, but character A and character C dislike each other. For convenience, assume that every pair of characters either likes each other or dislikes each other. No pair is neutral, no pair is left undecided, and no pair is one sided with A liking B while B dislikes A. So for each of the n(n−1)/2 pairs, like or dislike is fixed.
Consider a drama with n characters. Alice knows the relation of m pairs from the story so far. Alice is tired of stale love triangles and wants none of them in this drama. Alice also does not want all the characters to dislike one another, so among any three of them at least one pair must like each other. That is, for any three characters A, B, C, either exactly one of the pairs A-B, B-C, C-A likes each other, or all three pairs like each other.
Alice started drawing possible relation charts by filling in the pairs she does not know yet, but so many charts satisfy the condition that she gave up on drawing them all. She wants to count them instead. Given the m relations already known, print the number of ways to fill in the remaining pairs.
The first line contains the number of characters n and the number of known relations m. (3≤n≤100000, 0≤m≤100000, m≤n(n−1)/2)
Each of the next m lines contains one relation. Line i contains three positive integers ai, bi, ci. If ci is 1, then ai and bi like each other. If ci is 0, then they dislike each other. (1≤ai=bi≤n, ci is 0 or 1)
The same pair is given at most once. If (ai,bi) is given, then (bi,ai) does not appear in the input.
Print the number of ways to fill in the remaining pairs, modulo 1000000007 (109+7), on one line.