Good question qwq
Not very comfortable to write 1k
Own expectations and the ability to count or almost
First we look at this problem we found a * n!
This is how it goes
Because each node would add a small but a position location so 2 more optional position number is added every time i
And then we look at this question has some good properties, such as binary tree
First, based on common routines we can calculate the contribution of an edge is szi * (n-szi)
So in the end we only need to discuss how many edges connected to the node is szi
We observed that only 2,000 n n ^ 2 can prompt us
So we can enumerate a re-enumeration sz x of x
That must be back again x sz-1 point in selected sub-tree long x's and because of this sub-tree which must be smaller than the father son so generated with the way the subject is the same as described
This contribution is C (nx, sz-1) * sz!
We continue to consider out a total of n-sz sub-tree nodes consider the previous node x total of x! Kinds of ways to generate
Consider other nodes after a total of x (nx-sz + 1) th to the inside can not be long x is (x + 1-2) * (x + 2-2) * (n-sz + 1-2) kinds of ways to generate
The above two bit is combined (n-sz-1)! I (i-1)
So we finished it
The final contribution formula is to multiply the above and then adding
$\sum_{x=2}^n\sum_{sz=1}^{n-x+1}(n-sz-1)!i(i-1)sz!C(n-x,sz-1)sz(n-sz)$
Step by step we can push the launch da
Due to the modulo a prime number so we did not seek to ensure the C and Pascal's Triangle factorial normal requirements in accordance with it qwq
//Love and Freedom. #include<cstdio> #include<cmath> #include<algorithm> #include<cstring> #define ll long long #define inf 20021225 #define N 2001 using namespace std; int C[N][N],fac[N],n,m; void pre() { C[0][0]=C[1][0]=C[1][1]=1; fac[1]=fac[0]=1; for(int i=2;i<=n;i++) { fac[i]=1ll*fac[i-1]*i%m; C[i][0]=C[i][i]=1; for(int j=1;j<n;j++) C[i][j]=(C[i-1][j-1]+C[i-1][j])%m; } } int main() { scanf("%d%d",&n,&m); pre(); int ans=0; for(int x=2;x<=n;x++) for(int sz=1;sz<=n-x+1;sz++) { int tmp=1ll*fac[sz]*C[n-x][sz-1]%m*x%m*(x-1)%m*fac[n-sz-1]%m*sz%m*(n-sz)%m; ans=(ans+tmp)%m; } printf("%d\n",ans); return 0; }