import sys sys.setrecursionlimit(10 ** 8) N,M,X = map(int,input().split()) G = [[] for _ in range(N+1)] P = 10 ** 9 + 7 edge = [] parent = list(range(N+1)) def find(i): if parent[i] == i:return i parent[i] = find(parent[i]) return parent[i] def unite(i,j): I = find(i) J = find(j) if I == J:return False parent[I] = J parent[i] = J return True def isSame(i,j): return find(i) == find(j) for _ in range(M): x,y,z = map(int,input().split()) if isSame(x,y): continue else: unite(x,y) G[x].append(y) G[y].append(x) edge.append((x,y,z)) child = [1] * (N + 1) def dfs(now = 1,p = 0): for v in G[now]: if v == p:continue dfs(v,now) child[now] += child[v] dfs() ans = 0 for x,y,z in edge: if child[x] > child[y]: x,y = y,x ans = (ans + child[x] * (N-child[x]) * pow(X,z,P)) % P print(ans)