#include using namespace std; typedef signed long long ll; #undef _P #define _P(...) (void)printf(__VA_ARGS__) #define FOR(x,to) for(x=0;x<(to);x++) #define FORR(x,arr) for(auto& x:arr) #define ITR(x,c) for(__typeof(c.begin()) x=c.begin();x!=c.end();x++) #define ALL(a) (a.begin()),(a.end()) #define ZERO(a) memset(a,0,sizeof(a)) #define MINUS(a) memset(a,0xff,sizeof(a)) //------------------------------------------------------- const ll mo=1000000007; ll modpow(ll a, ll n = mo-2) { ll r=1;a%=mo; while(n) r=r*((n%2)?a:1)%mo,a=a*a%mo,n>>=1; return r; } template class UF { public: vector par,rank,cnt; UF() {par=rank=vector(um,0); cnt=vector(um,1); for(int i=0;irank[y]) return par[x]=y; rank[x]+=rank[x]==rank[y]; return par[y]=x; } }; UF<202020> uf; int N,M,X; vector> E[202020]; int C[202020]; ll ret; int dfs(int cur,int pre) { C[cur]=1; FORR(e,E[cur]) if(e.first!=pre) { C[cur]+=dfs(e.first,cur); } FORR(e,E[cur]) if(e.first==pre) { ret+=C[cur]*(N-C[cur])%mo*modpow(X,e.second)%mo; } return C[cur]; } void solve() { int i,j,k,l,r,x,y; string s; cin>>N>>M>>X; FOR(i,M) { cin>>x>>y>>r; x--,y--; if(uf[x]!=uf[y]) { E[x].push_back({y,r}); E[y].push_back({x,r}); uf(x,y); } } dfs(0,0); cout<