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) def roots(self): return [i for i, x in enumerate(self.parents) if x < 0] def num_roots(self): return len([i for i, x in enumerate(self.parents) if x < 0]) def members(self, x): root = self.find(x) return [i for i in range(self.n) if self.find(i) == root] def num_members(self,x): return abs(self.parents[self.find(x)]) def __str__(self): return '\n'.join('{}: {}'.format(r, self.members(r)) for r in self.roots()) import sys sys.setrecursionlimit(10**6) input = sys.stdin.readline def dfs(v): ans = 0 for unum in edge[v]: u,num = divmod(unum,mod2) if used[num]==False: used[num] = True v_num = dfs(u) e_cnt[num] = v_num*(N-v_num) ans += v_num return ans+1 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)] e_length = [0]*(N-1) num = 0 mod2 = 10**6 for x,y,z in A: x -= 1 y -= 1 if not uf.same(x,y): uf.union(x,y) e_length[num] = z edge[x].append(y*mod2+num) edge[y].append(x*mod2+num) num += 1 if num==N-1: break del uf,A e_cnt = [0]*(N-1) used = [False]*(N-1) dfs(0) mod = 10**9+7 ans = 0 for i in range(N-1): ans += e_cnt[i]*pow(X,e_length[i],mod) ans %= mod print(ans)