N, M, X = map(int, input().split()) XYZ = [] p = 10 ** 9 + 7 class UnionFind: def __init__(self, N): self.N = N self.P = [-1] * N def Find(self, x): if self.P[x] < 0: return x else: self.P[x] = self.Find(self.P[x]) return self.P[x] def query(self, x, y): x -= 1 y -= 1 x = self.Find(x) y = self.Find(y) if x == y: return False if self.P[x] > self.P[y]: x, y = y, x self.P[x], self.P[y] = self.P[x] + self.P[y], x return True G = UnionFind(N) for i in range(M): x, y, z = map(int, input().split()) XYZ += [[z, x, y]] XYZ.sort() E = [] V1 = [[] for i in range(N)] for z, x, y in XYZ: if G.query(x, y): E += [[x - 1, y - 1, z]] V1[x - 1] += [y - 1] V1[y - 1] += [x - 1] que = [0] P = [-1] * N P[0] = 0 V = [[] for i in range(N)] while que: q = que.pop() for v in V1[q]: if P[v] == -1: V[q] += [v] P[v] = q que += V[q] A = [1] * N que = [0] while que: q = que.pop() if V[q]: que += [V[q].pop()] else: if q == 0: break A[P[q]] += A[q] que += [P[q]] ans = 0 for x, y, z in E: if P[x] != y: x, y = y, x ans += A[x] * (N - A[x]) % p * pow(X, z, p) % p ans %= p print(ans)