class UnionFind(): def __init__(self, n): self.n = n self.parents = [-1] * n def find(self, x): if self.parents[x] < 0: return x else: self.parents[x] = self.find(self.parents[x]) return self.parents[x] def union(self, x, y): x = self.find(x) y = self.find(y) if x == y: return if self.parents[x] > self.parents[y]: x, y = y, x self.parents[x] += self.parents[y] self.parents[y] = x def same(self, x, y): return self.find(x) == self.find(y) N, M, X = map(int, input().split()) A = [list(map(int, input().split())) for _ in range(M)] uf = UnionFind(N) edge = [[] for _ in range(N)] num = 0 for x,y,z in A: x -= 1 y -= 1 if not uf.same(x,y): uf.union(x,y) edge[x].append((y,z,num)) edge[y].append((x,z,num)) num += 1 if num==N-1: break import sys sys.setrecursionlimit(10**6) def dfs(v): global ans res = 0 for u,z,num in edge[v]: if used[num]==False: used[num] = True v_num = dfs(u) ans += v_num*(N-v_num)*pow(X,z,mod) ans %= mod res += v_num return res+1 used = [False]*(N-1) ans = 0 mod = 10**9+7 dfs(0) print(ans)