#include #include #include using namespace atcoder; using mint = modint1000000007; using namespace std; #define rep(i,n) for (int i = 0; i < (n); ++i) #define Inf 1000000000000001 vector get_dis(auto &E){ vector ret(E.size(),-1); queue Q; Q.push(0); ret[0] = 0; while(Q.size()!=0){ int u = Q.front(); Q.pop(); for(int i=0;i assign_all(auto &E,long long fir){ vector ret(E.size(),Inf); queue Q; Q.push(0); ret[0] = fir; while(Q.size()!=0){ int u = Q.front(); Q.pop(); for(int i=0;i v){ long long ret = 0; for(int i=0;i>> E,long long K){ if(n==1)return K; vector parity = get_dis(E); bool is_bin = true; for(int i=0;i V = assign_all(E,1); if(is_bin){ if(get_diff(E,V)!=0){ return 0; } long long L = 0,R = K-1; for(int i=0;iK){ return 0; } } return 1; } } int main(){ int N,M,K; cin>>N>>M>>K; vector X(M),Y(M),Z(M); rep(i,M){ scanf("%d %d %d",&X[i],&Y[i],&Z[i]); X[i]--;Y[i]--; } dsu D(N); rep(i,M){ D.merge(X[i],Y[i]); } vector>>> E; vector> par(N); { auto g = D.groups(); E.resize(g.size()); rep(i,g.size()){ E[i].resize(g[i].size()); rep(j,g[i].size()){ par[g[i][j]] = make_pair(i,j); } } } rep(i,M){ int ind = par[X[i]].first; int a = par[X[i]].second,b = par[Y[i]].second; E[ind][a].emplace_back(b,Z[i]); E[ind][b].emplace_back(a,Z[i]); } mint ans = 0; { mint temp = 1; rep(i,E.size()){ temp *= solve(E[i].size(),0,E[i],K); } ans += temp; } //cout<