Consider directly selecting n different numbers to easily design a DP
F[i][j] = f[i-1][j] + i f[i-1][j-1]
The inductive method can prove that F (I,J) is a 2j polynomial about I can be i=1...2n+1 by getting the value of the answer violence DP time complexity O (n^2)
the time complexity of the violent insertion of a polynomial is O (n^3) but we only need a single point to apply Lagrange interpolation to the time complexity O (n) but here because the front is O (n^2) can be fully O (n^2) to the violent calculation of the following equation.
Total time complexity O (n^2)
What's the future of writing this <1k question all day ...
#include "bits/stdc++.h"
using namespace std;
const int n=505;
typedef long long LL;
int f[2*n][n],x,n,p,ans,m;
int inv (int a,int t=p-2) {
int r=1;
while (t) {
if (t&1) r= (LL) r*a%p;
A= (LL) a*a%p;t>>=1;
}
return r;
}
int main () {
cin>>x>>n>>p;
f[0][0]=1;m=2*n+1;
for (int i=1;i<=m;i++) for (int j=f[i][0]=1;j<=n;j++) f[i][j]= (f[i-1][j]+ (LL) i*f[i-1][j-1])%P;
for (int i=1;i<=m;i++) {
int a=f[i][n],b=1;
for (int j=1;j<=m;j++) if (i!=j) a= (LL) A * (X-J)%p,b= (LL) b* (i-j)%P;
A= (LL) A*INV (b)%P;
Ans= (Ans+a)%P;
}
for (int i=1;i<=n;i++) ans= (LL) ans*i%p;
cout<< (ans+p)%P;
return 0;
}