#include #include #include #include #include #include #include #include #include #include using namespace std; struct UnionFind { vector par; vector siz; UnionFind(long long N) : par(N), siz(N) { for(long long i = 0; i < N; i++){ par[i] = i; siz[i] = 1; } } long long root(long long x) { if (par[x] == x) return x; return par[x] = root(par[x]); } void unite(long long x, long long y) { long long rx = root(x); long long ry = root(y); if (rx == ry) return; par[rx] = ry; siz[ry] += siz[rx]; } bool same(long long x, long long y) { long long rx = root(x); long long ry = root(y); return rx == ry; } long long size(long long x){ return siz[root(x)]; } }; vector subtree; long long subtree_size(long long from, long long p, vector>> &E){ long long cnt = 1; for (auto [to, C] : E[from]){ if (to == p) continue; cnt += subtree_size(to, from, E); } return subtree[from] = cnt; } const long long m = 1e9+7; long long mod_exp(long long b, long long e){ if (e > 0 && b == 0) return 0; long long ans = 1; b %= m; while (e > 0){ if ((e & 1LL)) ans = (ans * b) % m; e = e >> 1LL; b = (b*b) % m; } return ans; } long long ans=0, N, X; void solve(long long from, long long p, vector>> &E){ for (auto [to, C] : E[from]){ if (to == p) continue; ans += ((subtree[to]*(N-subtree[to]))%m * mod_exp(X, C)) % m; ans %= m; solve(to, from, E); } } template using pq = priority_queue, greater>; int main(){ long long M, x, y, c; cin >> N >> M >> X; pq> que; UnionFind tree(N); vector>> E(N); for (int i=0; i> x >> y >> c; x--; y--; que.push({c, x, y}); } while(!que.empty()){ tie(c, x, y) = que.top(); que.pop(); if (!tree.same(x, y)){ tree.unite(x, y); E[x].push_back({y, c}); E[y].push_back({x, c}); } } subtree.resize(N); subtree_size(0, -1, E); solve(0, -1, E); cout << ans << endl; return 0; }