MOD = 10 ** 9 + 7 class UnionFind: def __init__(self, n): self.par = [i for i in range(n+1)] self.rank = [0] * (n+1) # 検索 def find(self, x): if self.par[x] == x: return x else: self.par[x] = self.find(self.par[x]) return self.par[x] # 併合 def unite(self, x, y): x = self.find(x) y = self.find(y) if x == y: return if self.rank[x] < self.rank[y]: self.par[x] = y else: self.par[y] = x if self.rank[x] == self.rank[y]: self.rank[x] += 1 # 同じ集合に属するか判定 def same_check(self, x, y): return self.find(x) == self.find(y) n, m, x = map(int, input().split()) uf = UnionFind(n) g = [[] for _ in range(n)] ans = 0 for _ in range(m): a, b, c = map(int, input().split()) if not uf.same_check(a - 1, b - 1): uf.unite(a - 1, b - 1) g[a - 1].append((b - 1, pow(x, c, MOD))) g[b - 1].append((a - 1, pow(x, c, MOD))) d = [-2] * n c = [1] * n s = [0] ans = 0 while s: p = s.pop() if d[p] == -2: s.append(p) d[p] = -1 for node, cost in g[p]: if d[node] == -2: s.append(node) else: d[p] = 0 for node, cost in g[p]: if d[node] >= 0: c[p] += c[node] ans += (n - c[node]) * c[node] * cost print(ans % MOD)