import java.util.*; import java.io.*; public class Main { static final int MOD = 1000000007; static ArrayList> graph = new ArrayList<>(); static int n; static int x; static long sum = 0; public static void main(String[] args) throws Exception { Scanner sc = new Scanner(System.in); BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String[] first = br.readLine().split(" ", 3); n = Integer.parseInt(first[0]); int m = Integer.parseInt(first[1]); x = Integer.parseInt(first[2]); for (int i = 0; i < n; i++) { graph.add(new HashMap<>()); } UnionFindTree uft = new UnionFindTree(n); for (int i = 0; i < m; i++) { String[] line = br.readLine().split(" ", 3); int a = Integer.parseInt(line[0]) - 1; int b = Integer.parseInt(line[1]) - 1; int c = Integer.parseInt(line[2]); if (!uft.same(a, b)) { uft.unite(a, b); graph.get(a).put(b, c); graph.get(b).put(a, c); } } getSize(0, 0); System.out.println(sum); } static int getSize(int idx, int parent) { int size = 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; size += s; } return size; } 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); } } } }