#include //事前処理とUnion-Find using std::cin; using std::cout; using std::endl; using std::vector; using ll=long long; const int MOD=1e9+7; struct Union_Find{ int N; vector par; Union_Find(int n):N(n){ par.resize(N); for(int i=0;i0){ if(Y%2){ ret*=X; ret%=MOD; } Y/=2; X*=X; X%=MOD; } return ret; } void dfs(int N,int X,int now,vector &seen,vector> &edge,vector> &weight,vector &subtree,ll &ans){ seen[now]=1; for(int i=0;i>N>>M>>X; vector> edge(N),weight(N); Union_Find UF(N); //最小全域木を作る for(int i=0;i>x>>y>>z; if(!UF.same(x-1,y-1)){ UF.unite(x-1,y-1); edge[x-1].push_back(y-1); edge[y-1].push_back(x-1); weight[x-1].push_back(z); weight[y-1].push_back(z); } } //dfsで各辺が経路になる回数を数えていく vector subtree(N); vector seen(N); ll ans=0; dfs(N,X,0,seen,edge,weight,subtree,ans); cout<