// #pragma GCC optimize ("Ofast") // #pragma GCC optimize ("unroll-loops") // #pragma GCC target ("avx,avx2,fma") #include using std::cin, std::cout, std::cerr; using ll = long long; const ll P = 1e9 + 7; ll Pow(ll a, ll b) { ll r = 1; for(; b; b /= 2) { if(b & 1) r = r * a % P; a = a * a % P; } return r; } int main() { std::ios::sync_with_stdio(false); int n, m, b; cin >> n >> m >> b; std::vector p(n + 1); std::iota(p.begin(), p.end(), 0); std::function root = [&](int x) { if(p[x] == x) return x; return p[x] = root(p[x]); }; std::vector>> e(n + 1); for(int i = 0; i < m; i ++) { int x, y, z; cin >> x >> y >> z; int rx = root(x); int ry = root(y); if(rx != ry) { e[x].push_back({y, z}); e[y].push_back({x, z}); p[ry] = rx; } } ll ans = 0; std::vector sz(n + 1); std::function dfs = [&](int x, int p) { sz[x] = 1; for(auto [i, z] : e[x]) if(i != p) { dfs(i, x); sz[x] += sz[i]; ans += ll(sz[i]) * (n - sz[i]) % P * Pow(b, z); } }; dfs(1, 0); cout << ans % P << '\n'; }