voiddfs( int u , int fa , int wf ){ int s; if( u != 1 ) dp[u][1] = 1 , s = 1; // currently only 0 in S_u else dp[u][0] = 1 , s = 0; for( auto& t : G[u] ) if( t.fi != fa ) { int v = t.fi; dfs( v , u , t.se / 2 ); for( int i = 0 ; i <= s ; ++ i ) for( int j = 1 ; j <= t.se / 2 ; ++ j ) { // j : the rest after merging ( td[i + j] += 1ll * dp[u][i] * C( t.se / 2 - 1 , t.se / 2 - j ) % P * iJ[j] % P * ( ( t.se / 2 - j & 1 ) ? P - 1 : 1 ) % P ) %= P; } for( int i = 0 ; i <= s + t.se ; ++ i ) dp[u][i] = td[i] , td[i] = 0; s += t.se / 2; } int re = 0; if( u == 1 ) { for( int i = 0 ; i <= s ; ++ i ) ( re += 1ll * J[i] % P * dp[u][i] % P ) %= P; } else { for( int i = wf ; i <= s ; ++ i ) ( re += 1ll * J[i] % P * dp[u][i] % P * C( i - 1 , wf - 1 ) % P ) %= P; } res = 1ll * res * re % P; }
int re = 0; if( u == 1 ) { for( int i = 0 ; i <= s ; ++ i ) ( re += 1ll * J[i] % P * dp[u][i] % P ) %= P; } else { for( int i = wf ; i <= s ; ++ i ) ( re += 1ll * J[i] % P * dp[u][i] % P * C( i - 1 , wf - 1 ) % P ) %= P; } res = 1ll * res * re % P;
#include"iostream" #include"algorithm" #include"cstring" #include"cstdio" #include"cmath" #include"vector" #include"map" #include"set" #include"queue" usingnamespace std; #define MAXN 5006 //#define int long long #define rep(i, a, b) for (int i = (a), i##end = (b); i <= i##end; ++i) #define per(i, a, b) for (int i = (a), i##end = (b); i >= i##end; --i) #define pii pair<int,int> #define fi first #define se second #define mp make_pair #define pb push_back #define eb emplace_back #define vi vector<int> #define all(x) (x).begin() , (x).end() #define mem( a ) memset( a , 0 , sizeof a ) //#define P 1000000007 typedeflonglong ll; int n , P; vector<pii> G[MAXN]; int dp[MAXN][MAXN] , siz[MAXN];
intPow( int x , int a ){ int ret = 1; while( a ) { if( a & 1 ) ret = 1ll * ret * x % P; x = 1ll * x * x % P , a >>= 1; } return ret; }
int J[MAXN] , iJ[MAXN]; intC( int a , int b ){ return1ll * J[a] * iJ[b] % P * iJ[a - b] % P; }
int res = 1; int td[MAXN]; voiddfs( int u , int fa , int wf ){ int s; if( u != 1 ) dp[u][1] = 1 , s = 1; // currently only 0 in S_u else dp[u][0] = 1 , s = 0; for( auto& t : G[u] ) if( t.fi != fa ) { int v = t.fi; dfs( v , u , t.se / 2 ); for( int i = 0 ; i <= s ; ++ i ) for( int j = 1 ; j <= t.se / 2 ; ++ j ) { // j : the rest after merging ( td[i + j] += 1ll * dp[u][i] * C( t.se / 2 - 1 , t.se / 2 - j ) % P * iJ[j] % P * ( ( t.se / 2 - j & 1 ) ? P - 1 : 1 ) % P ) %= P; } for( int i = 0 ; i <= s + t.se ; ++ i ) dp[u][i] = td[i] , td[i] = 0; s += t.se / 2; } int re = 0; if( u == 1 ) { for( int i = 0 ; i <= s ; ++ i ) ( re += 1ll * J[i] % P * dp[u][i] % P ) %= P; } else { for( int i = wf ; i <= s ; ++ i ) ( re += 1ll * J[i] % P * dp[u][i] % P * C( i - 1 , wf - 1 ) % P ) %= P; } res = 1ll * res * re % P; } voidsolve(){ cin >> n >> P; J[0] = iJ[0] = 1; rep( i , 1 , n ) J[i] = 1ll * J[i - 1] * i % P , iJ[i] = Pow( J[i] , P - 2 ); int u , v , w; rep( i , 1 , n - 1 ) scanf("%d%d%d",&u,&v,&w) , G[u].eb( mp( v , w ) ) , G[v].eb( mp( u , w ) ); dfs( 1 , 1 , 0 ); cout << 1ll * res * n % P << endl; } signedmain(){ // freopen("2.in","r",stdin); // freopen("fuckout","w",stdout); // int T;cin >> T;while( T-- ) solve(); solve(); }