///you should init the facmod[] before typec factorialMod(typec n, typec &pcnt, typec p, typec t = 1) { typec pt = power(p, t), res = 1; typec stepCnt = 0; while(n) { res *= facmod[n % pt], res %= pt; stepCnt += n / pt, n /= p, pcnt += n; } res *= powerMod(facmod[pt - 1], stepCnt, pt); return res %= pt; }
template<class T> inline T powerMod(T p,int e,T m)//NOTES:powerMod( {if(e==0)return 1%m;else if(e%2==0){T t=powerMod(p,e/2,m);return multiplyMod(t,t,m);}else return multiplyMod(powerMod(p,e-1,m),p,m);}