#include #define rep(a,n) for (int a = 0; a < (n); ++a) using namespace std; using ll = long long; typedef pair P; typedef pair PP; typedef vector > Graph; template inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; } template inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; } const ll INF = 1e18; // auto mod int // depends on Template const int mod = 1000000007; //const int mod = 998244353; 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 { return mint(*this) += a;} mint operator-(const mint a) const { return mint(*this) -= a;} mint operator*(const mint a) const { return mint(*this) *= 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 { return mint(*this) /= a;} }; istream& operator>>(istream& is, const mint& a) { return is >> a.x;} ostream& operator<<(ostream& os, const mint& a) { return os << a.x;} long long modpow(long long a, long long n) { long long res = 1; while (n > 0) { if (n & 1) res = res * a % mod; a = a * a % mod; n >>= 1; } return res; } //UnionFind struct UnionFind { vector par; // par[i]:iの親の番号 (例) par[3] = 2 : 3の親が2 UnionFind(int n) : par(n, -1) { } int root(int x) { // データxが属する木の根を再帰で得る:root(x) = {xの木の根} if (par[x] < 0) return x; return par[x] = root(par[x]); } bool merge(int x, int y) {//xの木とyの木を結合する x = root(x); y = root(y); if (x == y) return false; if (par[x] > par[y]) swap(x, y); // merge technique par[x] += par[y]; par[y] = x; return true; } int size(int x) {//sizeの取得 return -par[root(x)]; } bool same(int x, int y) { // 2つのデータx, yが属する木が同じならtrueを返す int rx = root(x); int ry = root(y); return rx == ry; } }; template< typename T = int > struct Edge { int from, to; T cost; int idx; Edge() = default; Edge(int from, int to, T cost = 1, int idx = -1) : from(from), to(to), cost(cost), idx(idx) {} operator int() const { return to; } }; template struct TreeDP{ using FX = function;//T●T->Tとなる関数の型 using FA = function; const T e;//単位元 struct Edge{ int to; ll cost; }; using Graph = vector >; FX merge; FA add_root; Graph G; int n;//木の要素数 vector< T > dp; TreeDP(FX merge_,FA add_root_, int n_, T e_):merge(merge_),add_root(add_root_),n(n_),e(e_){ dp.resize(n); G.resize(n); } void add_edge(int a,int b){ G[a].push_back({b}); } void build(){ dfs(0); } void dfs(int v,int p=-1){ T dp_cum = e; int deg = G[v].size(); for(int i = 0; i < deg; i++){ int x = G[v][i].to; if(x==p)continue; dfs(x,v); dp_cum = merge(dp_cum,dp[x]); } dp[v] = add_root(dp_cum); } }; //input ll N,M,X; int main(){ cin >> N >> M >> X; UnionFind u(N); Graph g(N); vector > edges; auto merge = [](ll dp_cum,ll d)->ll{ return dp_cum+d; }; auto add_root = [](ll d) -> ll{ return d+1; }; TreeDPtdp(merge,add_root,N,0); rep(i,M){ int x,y; ll z; cin >> x >> y >> z; x--;y--; if(u.same(x,y)){ continue; } u.merge(x,y); tdp.add_edge(x,y); tdp.add_edge(y,x); Edge e = {x,y,z}; edges.push_back(e); } tdp.build(); mint ans = 0; rep(i,edges.size()){ int x = edges[i].from; int y = edges[i].to; ll z = edges[i].cost; ll ch = min(tdp.dp[x],tdp.dp[y]); mint tmp = 1; tmp *= ch; tmp *= N-ch; tmp *= modpow(X,z); ans += tmp; } cout << ans << endl; return 0; }