def pow(a,b,mod=1) r=1 while b>0 r*=a if b&1>0 r%=mod if r>=mod a*=a a%=mod if a>=mod b>>=1 end r end def nCr(n,r,mod) return 0 if n