import sys sys.setrecursionlimit(10**6) input=sys.stdin.readline MOD=10**9+7 class UnionFind(): def __init__(self,n): self.upper=[-1]*n def root(self,x): if self.upper[x]<0: return x else: self.upper[x]=self.root(self.upper[x]) return self.upper[x] def equiv(self,x,y): return self.root(x)==self.root(y) def unite(self,x,y): x=self.root(x) y=self.root(y) if x==y: return if self.upper[x]>self.upper[y]: x,y=y,x self.upper[x]+=self.upper[y] self.upper[y]=x N,M,X=map(int,input().split()) g=[[] for _ in range(N+1)] uf=UnionFind(N+1) for i in range(M): x,y,z=map(int,input().split()) if not uf.equiv(x,y): g[x].append((y,pow(X,z,MOD))) g[y].append((x,pow(X,z,MOD))) uf.unite(x,y) sz=[1]*(N+1) ans=0 def dfs(v,p): global ans for dst,cost in g[v]: if dst==p: continue dfs(dst,v) ans+=sz[dst]*(N-sz[dst])%MOD*cost%MOD ans%=MOD sz[v]+=sz[dst] dfs(1,-1) print(ans)