#include using namespace std; using ll = long long; #define MOD 1000000007 template struct ModInt{ int x; ModInt():x(0){} ModInt(long long y):x(y>=0?y%mod:(mod-(-y)%mod)%mod){} ModInt &operator+=(const ModInt &p){ if((x+=p.x)>=mod)x-=mod; return *this; } ModInt &operator-=(const ModInt &p){ if((x+=mod-p.x)>=mod)x-=mod; return *this; } ModInt &operator*=(const ModInt &p){ x=(int)(1LL*x*p.x%mod); return *this; } ModInt &operator/=(const ModInt &p){ *this*=p.inverse(); return *this; } ModInt &operator^=(long long p){ ModInt res = 1; for (; p; p >>= 1) { if (p & 1) res *= *this; *this *= *this; } return *this = res; } ModInt operator-()const{return ModInt(-x);} ModInt operator+(const ModInt &p)const{return ModInt(*this)+=p;} ModInt operator-(const ModInt &p)const{return ModInt(*this)-=p;} ModInt operator*(const ModInt &p)const{return ModInt(*this)*=p;} ModInt operator/(const ModInt &p)const{return ModInt(*this)/=p;} ModInt operator^(long long p)const{return ModInt(*this)^=p;} bool operator==(const ModInt &p)const{return x==p.x;} bool operator!=(const ModInt &p)const{return x!=p.x;} explicit operator int() const { return x; } ModInt operator=(const int p) {x = p; return ModInt(*this);} ModInt inverse()const{ int a=x,b=mod,u=1,v=0,t; while(b>0){ t=a/b; a-=t*b; std::swap(a,b); u-=t*v; std::swap(u,v); } return ModInt(u); } friend std::ostream &operator<<(std::ostream &os,const ModInt &p){ return os<>(std::istream &is,ModInt &a){ long long x; is>>x; a=ModInt(x); return (is); } }; typedef ModInt mint; class UnionFind{ private: vector data; public: UnionFind(int n) : data(n,-1) {} int root(int i) { return data[i] < 0 ? i : data[i] = root(data[i]); } int size(int i) { return -data[root(i)]; } bool same(int x, int y) {return root(x) == root(y); } bool connected() {return size(0) == (int)data.size(); } bool unit(int i,int j){ i = root(i); j = root(j); if(i != j){ data[i] += data[j]; data[j] = i; } return i != j; } }; int n, m, x; vector>> hen; map,int> mp; mint ans = 0; int dfs(int v, int par) { int sum = 0; for(auto i : hen[v]) { if(i.first == par)continue; sum += dfs(i.first, v); } sum++; int exp = mp[{v, par}]; ans += (mint(x)^exp) * (sum) * (n - sum); return sum; } int main() { cin >> n >> m >> x; UnionFind uni(n); hen.resize(n); for(int i = 0; i < m; i++) { int a, b, c; cin >> a >> b >> c; a--, b--; if(uni.same(a, b))continue; uni.unit(a,b); mp[{a,b}] = mp[{b,a}] = c; hen[a].push_back({b,c}); hen[b].push_back({a,c}); } dfs(0, -1); cout << ans << endl; }