Love Triangles

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 MB

Problem

A 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(n1)/2n(n-1)/2 pairs, like or dislike is fixed.

Consider a drama with nn characters. Alice knows the relation of mm 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 mm relations already known, print the number of ways to fill in the remaining pairs.

Input

The first line contains the number of characters nn and the number of known relations mm. (3n1000003 \le n \le 100\,000, 0m1000000 \le m \le 100\,000, mn(n1)/2m \le n(n-1)/2)

Each of the next mm lines contains one relation. Line ii contains three positive integers aia_i, bib_i, cic_i. If cic_i is 1, then aia_i and bib_i like each other. If cic_i is 0, then they dislike each other. (1aibin1 \le a_i \ne b_i \le n, cic_i is 0 or 1)

The same pair is given at most once. If (ai,bi)(a_i, b_i) is given, then (bi,ai)(b_i, a_i) does not appear in the input.

Output

Print the number of ways to fill in the remaining pairs, modulo 10000000071\,000\,000\,007 (109+710^9 + 7), on one line.