代码如下
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int maxn=1e5+10;
int p;
LL fac[maxn],inv[maxn];
LL fpow(LL a,LL b,LL mod){
LL ret=1%mod;
for(;b;b>>=1,a=a*a%mod)if(b&1)ret=ret*a%mod;
return ret;
}
LL C(int n,int m){
if(m>n)return 0;
return fac[n]*inv[n-m]%p*inv[m]%p;
}
LL Lucas(int n,int m){
if(m==0)return 1;
return Lucas(n/p,m/p)*C(n%p,m%p)%p;
}
int main(){
int T;scanf("%d",&T);
while(T--){
int n,m;
scanf("%d%d%d",&n,&m,&p);
fac[0]=1;
for(int i=1;i<=p;i++)fac[i]=fac[i-1]*i%p;
inv[p-1]=fpow(fac[p-1],p-2,p);
for(int i=p-1;i>=1;i--)inv[i-1]=inv[i]*i%p;
printf("%lld
",Lucas(n+m,m));
}
return 0;
}