using System; using static System.Console; using System.Linq; using System.Collections.Generic; class Program { static int NN => int.Parse(ReadLine()); static int[] NList => ReadLine().Split().Select(int.Parse).ToArray(); static int[][] NArr(long n) => Enumerable.Repeat(0, (int)n).Select(_ => NList).ToArray(); public static void Main() { Solve(); } static void Solve() { var c = NList; var (n, m, k) = (c[0], c[1], c[2]); var map = NArr(m); var tree = new List<(int to, int z)>[n]; for (var i = 0; i < tree.Length; ++i) tree[i] = new List<(int to, int z)>(); var uf = new UnionFindTree(n); foreach (var edge in map) { uf.Unite(edge[0] - 1, edge[1] - 1); tree[edge[0] - 1].Add((edge[1] - 1, edge[2])); tree[edge[1] - 1].Add((edge[0] - 1, edge[2])); } var mod = 1_000_000_007; var ans1 = 1L; var ans2 = 1L; var visited1 = new bool[n]; var visited2 = new bool[n]; for (var i = 0; i < n; ++i) if (uf.GetRoot(i) == i) { var min = 1L; var max = (long)k; DFS(i, k, 0, true, tree, visited1, ref min, ref max); ans1 = ans1 * Math.Max(0, max - min + 1) % mod; min = 1L; max = k - 1; DFS(i, k - 1, 0, true, tree, visited2, ref min, ref max); ans2 = ans2 * Math.Max(0, max - min + 1) % mod; } WriteLine((ans1 - ans2 + mod) % mod); } static void DFS(int cur, int k, long sum, bool minus, List<(int to, int z)>[] tree, bool[] visited, ref long min, ref long max) { visited[cur] = true; foreach (var next in tree[cur]) { if (visited[next.to]) continue; if (minus) { max = Math.Min(max, next.z - sum - 1); min = Math.Max(min, next.z - sum - k); } else { min = Math.Max(min, 1 - next.z + sum); max = Math.Min(max, k - next.z + sum); } DFS(next.to, k, next.z - sum, !minus, tree, visited, ref min, ref max); } } class UnionFindTree { int[] roots; public UnionFindTree(int size) { roots = new int[size]; for (var i = 0; i < size; ++i) roots[i] = -1; } public int GetRoot(int a) { if (roots[a] < 0) return a; return roots[a] = GetRoot(roots[a]); } public bool IsSameTree(int a, int b) { return GetRoot(a) == GetRoot(b); } public bool Unite(int a, int b) { var x = GetRoot(a); var y = GetRoot(b); if (x == y) return false; if (-roots[x] < -roots[y]) { var tmp = x; x = y; y = tmp; } roots[x] += roots[y]; roots[y] = x; return true; } public int GetSize(int a) { return -roots[GetRoot(a)]; } } }