#include "bits/stdc++.h" using namespace std; //#define int long long #define ll long long #define rep(i,n) for(ll i = 0; i < (n); i++) #define P pair #define ld long double ll INF = (1LL << 60); int mod = 1000000007; struct mint { ll x; // typedef long long ll; mint(ll x = 0) :x((x%mod + mod) % mod) {} mint operator-() const { return mint(-x); } mint& operator+=(const mint a) { if ((x += a.x) >= mod) x -= mod; return *this; } mint& operator-=(const mint a) { if ((x += mod - a.x) >= mod) x -= mod; return *this; } mint& operator*=(const mint a) { (x *= a.x) %= mod; return *this; } mint operator+(const mint a) const { mint res(*this); return res += a; } mint operator-(const mint a) const { mint res(*this); return res -= a; } mint operator*(const mint a) const { mint res(*this); return res *= a; } mint pow(ll t) const { if (!t) return 1; mint a = pow(t >> 1); a *= a; if (t & 1) a *= *this; return a; } // for prime mod mint inv() const { return pow(mod - 2); } mint& operator/=(const mint a) { return (*this) *= a.inv(); } mint operator/(const mint a) const { mint res(*this); return res /= a; } }; istream& operator>>(istream& is, mint& a) { return is >> a.x; } ostream& operator<<(ostream& os, const mint& a) { return os << a.x; } struct UnionFind { vectorp; UnionFind() {} UnionFind(ll n) { p.resize(n, -1); } ll find(ll x) { if (p[x] == -1)return x; else return p[x] = find(p[x]); } void unite(ll x, ll y) { x = find(x), y = find(y); if (x == y)return; p[x] = y; } }; struct Edge { ll a, b, cost; }; bool comp_e(const Edge &e1, const Edge &e2) { return e1.cost < e2.cost; } vector>AfterGraph; struct Kruskal { UnionFind uf; ll sum; vectoredges; ll n; Kruskal(const vector &edges_, ll n_) : edges(edges_), n(n_) { sort(edges.begin(), edges.end(), comp_e); uf = UnionFind(n); sum = 0; vector costs; for (auto e : edges) { if (uf.find(e.a) != uf.find(e.b)) { uf.unite(e.a, e.b); AfterGraph[e.a].push_back({ e.b, e.cost }); AfterGraph[e.b].push_back({ e.a,e.cost }); costs.push_back(e.cost); sum += e.cost; } } } }; vectorsubtree_size; void dfs(const vector> &G, ll v, ll p, ll now) { for (auto nv : G[v]) { if (nv.first == p)continue; dfs(G, nv.first, v, now + 1); } subtree_size[v] = 1; for (auto c : G[v]) { if (c.first == p)continue; subtree_size[v] += subtree_size[c.first]; } } signed main() { ios::sync_with_stdio(false); cin.tie(nullptr); ll N, M, X; cin >> N >> M >> X; AfterGraph.resize(N); subtree_size.resize(N); vectorG(M); rep(i, M) { ll x, y, z; cin >> x >> y >> z; x--; y--; Edge e = { x,y,z }; G[i] = e; } Kruskal tmp(G, N); dfs(AfterGraph, 0, -1, 0); mint ans = 0; rep(i, N) { for (auto v : AfterGraph[i]) { mint now = mint(X).pow(v.second); ll idx = 0; if (subtree_size[i] > subtree_size[v.first]) { idx = v.first; } else idx = i; now *= subtree_size[idx]; now *= (N - subtree_size[idx]); ans += now; } } ans /= 2; cout << ans << endl; return 0; }