There are many anime that are about "love triangles": Alice loves Bob, and Charlie loves Bob as well, but Alice hates Charlie. You are thinking about an anime which has n characters. The characters are labeled from 1 to n. Every pair of two characters can either mutually love each other or mutually hate each other (there is no neutral state).

You hate love triangles (A-B are in love and B-C are in love, but A-C hate each other), and you also hate it when nobody is in love. So, considering any three characters, you will be happy if exactly one pair is in love (A and B love each other, and C hates both A and B), or if all three pairs are in love (A loves B, B loves C, C loves A).

You are given a list of m known relationships in the anime. You know for sure that certain pairs love each other, and certain pairs hate each other. You're wondering how many ways you can fill in the remaining relationships so you are happy with every triangle. Two ways are considered different if two characters are in love in one way but hate each other in the other. Print this count modulo 1 000 000 007.


-----Input-----

The first line of input will contain two integers n, m (3 ≤ n ≤ 100 000, 0 ≤ m ≤ 100 000).

The next m lines will contain the description of the known relationships. The i-th line will contain three integers a_{i}, b_{i}, c_{i}. If c_{i} is 1, then a_{i} and b_{i} are in love, otherwise, they hate each other (1 ≤ a_{i}, b_{i} ≤ n, a_{i} ≠ b_{i}, $c_{i} \in \{0,1 \}$).

Each pair of people will be described no more than once.


-----Output-----

Print a single integer equal to the number of ways to fill in the remaining pairs so that you are happy with every triangle modulo 1 000 000 007. 


-----Examples-----
Input
3 0

Output
4

Input
4 4
1 2 1
2 3 1
3 4 0
4 1 0

Output
1

Input
4 4
1 2 1
2 3 1
3 4 0
4 1 1

Output
0



-----Note-----

In the first sample, the four ways are to:   Make everyone love each other  Make 1 and 2 love each other, and 3 hate 1 and 2 (symmetrically, we get 3 ways from this). 

In the second sample, the only possible solution is to make 1 and 3 love each other and 2 and 4 hate each other.

Difficulty: HARD
Tags: Spanning trees

Graph algorithms

Graph traversal

Starter code:

Solutions:
class DisjointSet(object):

	def __init__(self, n):
		self.parent = list(range(n))
		self.rank = [0] * n
		self.num = n

	def union(self, x, y):
		self._link(self.find_set(x), self.find_set(y))

	def _link(self, x, y):
		if x == y:
			return
		self.num -= 1
		if self.rank[x] > self.rank[y]:
			self.parent[y] = x
		else:
			self.parent[x] = y
			if self.rank[x] == self.rank[y]:
				self.rank[y] += 1

	def find_set(self, x):
		xp = self.parent[x]
		if xp != x:
			self.parent[x] = self.find_set(xp)
		return self.parent[x]

def solve():
	(n, m) = list(map(int, input().split()))
	ds = DisjointSet(n * 2)
	for i in range(m):
		(a, b, c) = list(map(int, input().split()))
		a -= 1
		b -= 1
		aA = a * 2
		aB = aA + 1
		bA = b * 2
		bB = bA + 1
		if c == 0:
			if ds.find_set(aA) == ds.find_set(bA):
				return 0
			ds.union(aA, bB)
			ds.union(aB, bA)
		else:
			if ds.find_set(aA) == ds.find_set(bB):
				return 0
			ds.union(aA, bA)
			ds.union(aB, bB)
	return pow(2, ds.num // 2 - 1, 10 ** 9 + 7)
print(solve())


class DSU(object):

	def __init__(self, n):
		self.father = list(range(n))
		self.size = n

	def union(self, x, s):
		x = self.find(x)
		s = self.find(s)
		if x == s:
			return
		self.father[s] = x
		self.size -= 1

	def find(self, x):
		xf = self.father[x]
		if xf != x:
			self.father[x] = self.find(xf)
		return self.father[x]

def is_invalid(a, b, ds):
	return ds.find(a) == ds.find(b)
(n, k) = map(int, input().split())
ds = DSU(n * 2)
for i in range(k):
	(first, second, color) = map(int, input().split())
	first -= 1
	second -= 1
	if color == 0:
		if is_invalid(first, second, ds):
			print(0)
			exit()
		ds.union(first, second + n)
		ds.union(first + n, second)
	else:
		if is_invalid(first, second + n, ds):
			print(0)
			exit()
		ds.union(first, second)
		ds.union(first + n, second + n)
sum = 1
for i in range(ds.size // 2 - 1):
	sum = sum * 2 % (10 ** 9 + 7)
print(sum)


from sys import stdin, stdout
import sys

class Node(object):

	def __init__(self, label):
		self.label = label
		self.par = self
		self.rank = 0

class DisjointSet(object):

	def __init__(self, n):
		self.n = n
		self.nodes = [Node(i) for i in range(self.n)]

	def find(self, u):
		if u == u.par:
			return u
		return self.find(u.par)

	def union(self, u, v):
		(u, v) = (self.find(u), self.find(v))
		if u == v:
			return False
		if u.rank > v.rank:
			(u, v) = (v, u)
		u.par = v
		if u.rank == v.rank:
			v.rank += 1
		return True

	def size(self):
		cnt = 0
		for node in self.nodes:
			if node.par == node:
				cnt += 1
		return cnt

def color(adj, colors, root):
	neighbor_color = 1 - colors[root]
	for neighbor in adj[root]:
		if colors[neighbor] == -1:
			colors[neighbor] = neighbor_color
			if color(adj, colors, neighbor):
				pass
			else:
				return False
		elif colors[neighbor] != neighbor_color:
			return False
	return True
try:
	sys.setrecursionlimit(10000)
	mod = 1000000007
	(n, m) = map(int, input().split())
	dsu = DisjointSet(n)
	reds = []
	for _ in range(m):
		(a, b, c) = map(int, input().split())
		if c == 0:
			reds.append((a - 1, b - 1))
		else:
			dsu.union(dsu.nodes[a - 1], dsu.nodes[b - 1])
	new_graph = {}
	cnt = 0
	for i in range(n):
		comp = dsu.find(dsu.nodes[i])
		if comp.label not in new_graph.keys():
			new_graph[comp.label] = cnt
			cnt += 1
	x = len(new_graph)
	assert cnt == x
	dsu2 = DisjointSet(x)
	adj = [[] for _ in range(x)]
	colors = [-1 for _ in range(x)]
	for (a, b) in reds:
		comp1 = dsu.find(dsu.nodes[a]).label
		comp2 = dsu.find(dsu.nodes[b]).label
		if comp1 == comp2:
			print(0)
			exit(0)
		else:
			index1 = new_graph[comp1]
			index2 = new_graph[comp2]
			if index1 >= x or index2 >= x:
				print(-1)
				exit(0)
			dsu2.union(dsu2.nodes[index1], dsu2.nodes[index2])
			adj[index1].append(index2)
			adj[index2].append(index1)
	for i in range(x):
		if colors[i] == -1:
			colors[i] = 0
			ans = color(adj, colors, i)
			if ans:
				pass
			else:
				print(0)
				exit(0)
	comps = dsu2.size()
	ans = 1
	for _ in range(comps - 1):
		ans *= 2
		ans %= mod
	print(ans)
	exit(0)
except Exception as e:
	print(e)
	print(-1)
	exit(0)