题面
代码
点击查看代码
#include<bits/stdc++.h>
using namespace std;
#define IOS ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define rep(i,a,n) for(int i=a;i<=n;i++)
#define per(i,a,n) for(int i=n;i>=a;i--)
#define first fi
#define seconf se
#define push_back pb
#define endl "\n"
#define O(x) cout<<#x<<":"<<x<<endl;
using ll=long long;
using pii=pair<int,int>;
using pll=pair<ll,ll>;
#define N 200100
#define MOD int(1e9+7)
ll f[N],g[N];
ll ksm(ll a,ll b,ll mod=MOD){
if(b==0) return 1;
if(b==1) return a%mod;
ll mid=ksm(a,b>>1,mod);
if(b&1) return mid*mid%mod*a%mod;
else return mid*mid%mod;
}
ll inv(ll a,ll mod=MOD){
return ksm(a,mod-2,mod);
}
ll om(ll a,ll mod=MOD){
return (a%mod+mod)%mod;
}
void Init(){
f[0]=1;
rep(i,1,200000) f[i]=om(f[i-1]*i);
//rep(i,1,100) O(f[i])
g[200000]=inv(f[200000]);
per(i,0,199999) g[i]=om(g[i+1]*(i+1));
//O(g[1])
}
ll C(ll n,ll m){
if(n<m) return 0;
return om(om(f[n]*g[m])*g[n-m]);
}
void solve()
{
int n,m,k; ll ans1=0;
cin>>n>>m>>k;
vector<ll> f1(m+1);
rep(i,1,m){
int a,b;
cin>>a>>b>>f1[i];
ans1=om(ans1+f1[i]);
}
//O(ans1)
ans1=om(ans1*k),ans1=om(ans1*inv(C(n,2)));
//O(ans1)
ll ans2=0;
rep(i,0,k){
ll v1=C(k,i);
ll d=C(n,2);
ll v2=ksm(inv(d),i);
ll v3=ksm(om(1-inv(d)),k-i);
ll v4=om(om(1ll*i*(i-1))*inv(2));
ans2=om(ans2+om(om(om(v1*v2)*v3)*v4));
}
cout<<om(ans1+om(1ll*m*ans2))<<endl;
}
int main()
{
IOS
Init();
int t=1;cin>>t;
while(t--) {
solve();
//O(t)
}
return 0;
}