#include #include #include #include #include #include #include #include #include using namespace std; using ll = int64_t; #define rep(i, j, n) for (int i = j; i < (int)n; ++i) #define rrep(i, j, n) for (int i = (int)n - 1; j <= i; --i) constexpr ll MOD = 1000000007; constexpr int INF = 0x3f3f3f3f; constexpr ll INFL = 0x3f3f3f3f3f3f3f3fLL; struct UnionFind { vector par; UnionFind(int n) : par(n, -1) {} void unite(int x, int y) { x = find(x); y = find(y); if (x == y) return; if (par[x] > par[y]) swap(x, y); par[x] += par[y]; par[y] = x; return; } bool same(int x, int y) { return find(x) == find(y); } int find(int x) { if (par[x] < 0) return x; int r = find(par[x]); return par[x] = r; } int sizeOf(int x) { return -par[find(x)]; } }; struct Edge { int from, to; int64_t cost; Edge(int f, int t, int64_t c) : from(f), to(t), cost(c) {} bool operator<(const Edge& e) const { return cost < e.cost; } }; using Edges = vector; Edges kruskal(Edges& es, int n) { UnionFind uf(n); sort(es.begin(), es.end()); Edges mst; for (Edge e : es) { if (!uf.same(e.from, e.to)) { uf.unite(e.from, e.to); mst.push_back(e); } } return mst; } ll mpow(ll x, ll e) { ll res = 1; while (e) { if (e & 1) res = res * x % MOD; x = x * x % MOD; e >>= 1; } return res; } ll dfs1(int v, int p, vector& d, vector>>& g) { ll res = 1; for (auto e : g[v]) { int nv = e.first; if (nv == p) continue; res += dfs1(nv, v, d, g); } d[v] = res; return res; } ll ans = 0; ll n, m, x; void dfs2(int v, int p, vector& d, vector>>& g) { for (auto e : g[v]) { int nv = e.first; if (nv == p) continue; ans = (ans + d[nv] * (n - d[nv]) * mpow(x, e.second) % MOD) % MOD; dfs2(nv, v, d, g); } } int main() { ll a, b, c; cin >> n >> m >> x; Edges es; rep(i, 0, m) { cin >> a >> b >> c; --a, --b; es.emplace_back(a, b, c); } auto mst = kruskal(es, n); vector>> g(n); rep(i, 0, mst.size()) { int from = mst[i].from; int to = mst[i].to; ll cost = mst[i].cost; g[from].emplace_back(to, cost); g[to].emplace_back(from, cost); } vector d(n); dfs1(0, 0, d, g); dfs2(0, 0, d, g); cout << ans << endl; }