import java.util.*; public class Main { static final int MOD = 1000000007; static ArrayList> graph = new ArrayList<>(); static int[] sizes; static int n; static int x; static long sum = 0; public static void main(String[] args) { Scanner sc = new Scanner(System.in); n = sc.nextInt(); int m = sc.nextInt(); x = sc.nextInt(); for (int i = 0; i < n; i++) { graph.add(new HashMap<>()); } UnionFindTree uft = new UnionFindTree(n); for (int i = 0; i < m; i++) { int a = sc.nextInt() - 1; int b = sc.nextInt() - 1; int c = sc.nextInt(); if (!uft.same(a, b)) { uft.unite(a, b); graph.get(a).put(b, c); graph.get(b).put(a, c); } } sizes = new int[n]; getSize(0, 0); System.out.println(sum); } static int getSize(int idx, int parent) { sizes[idx] = 1; for (int y : graph.get(idx).keySet()) { if (y == parent) { continue; } int s = getSize(y, idx); sum += pow(x, graph.get(idx).get(y)) * (n - s) % MOD * s % MOD; sum %= MOD; sizes[idx] += s; } return sizes[idx]; } static long pow(long x, int p) { if (p == 0) { return 1; } else if (p % 2 == 0) { return pow(x * x % MOD, p / 2); } else { return x * pow(x, p - 1) % MOD; } } static class UnionFindTree { int[] parents; public UnionFindTree(int size) { parents = new int[size]; for (int i = 0; i < size; i++) { parents[i] = i; } } public int find(int a) { if (parents[a] == a) { return a; } else { return parents[a] = find(parents[a]); } } public boolean same(int a, int b) { return find(a) == find(b); } public void unite(int a, int b) { if (!same(a, b)) { parents[find(a)] = find(b); } } } }