定理很有用
C(n,m)=C(n%mod,m%mod)*C(n/mod,m/mod);
#include<iostream>
#include<cstdio>
using namespace std;
#define ll long long
const ll int MAX_N = 1e5 + 10;
#define bug(x) cout<<#x<<" == "<<x<<endl;
ll mod ;
ll a[MAX_N]= {0};
ll epow(ll t)
{
ll b = mod-2;
ll ans = 1;
while(b)
{
if(b&1)
ans *= t;
t *= t;
ans %= mod;
t %= mod;
b >>= 1;
}
return ans;
}
ll C(ll n,ll m)
{
if(m-n<0)
return 0;
return a[m]*epow(a[n])%mod*epow(a[m-n])%mod;
}
ll lucas(ll n,ll m)
{
if(m<mod)
{
return C(n,m);
}
else
{
return C(n%mod,m%mod)%mod*lucas(n/mod,m/mod)%mod;
}
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
ll n,m;
scanf("%lld %lld %lld",&n,&m,&mod);
a[0] = 1;
for(ll i=1; i<=100003; i++)
{
a[i] = a[i-1]*i;
a[i] %= mod;
}
ll ans=lucas(n,n+m);
printf("%lld\n",ans%mod);
}
}