// nCk mod p ; O(log_p n) // nCk = n! / (k!(n-k)!) int mod_comb(int n, int k, int p){ if (n<0 || k<0 || n<k) return 0; int e1, e2, e3; int a1 = mod_fact(n, p, e1), a2 = mod_fact(k, p, e2), a3 = mod_fact(n-k, p, e3); if (e1 > e2+e3) return 0; // nCk is divided by p return a1 * mod_inverse(a2 * a3 % p, p) % p; }
int mod_fact(int n, int m) { if(n < m) return 0; if(n >= mod) return (LL)mod_fact(n / mod, m / mod) * mod_fact(n % mod, m % mod) % mod; return (LL)fact[n] * inv[fact[m]] * inv[fact[n - m]] % mod; }
int main() { fact[0] = fact[1] = inv[1] = 1; for(int i = 2; i < mod; ++i) { fact[i] = (LL)fact[i - 1] * i % mod; inv[i] = mod - mod / i * (LL)inv[mod % i] % mod; } scanf("%d", &t); while(t--) { scanf("%d%d%d", &n, &l, &r); ans = mod_fact(r - l + 1 + n, n) - 1; if(ans < 0) ans += mod; printf("%d\n", ans); } return 0; }
// n! = a X p^e // compute a mod p; O(log_p n) int mod_fact(int n, int p, int& e){ e = 0; if (n == 0) return 1; // compute for multiples of p (p, 2p, 3p, ...) int res = mod_fact(n / p, p, e); e += n / p; // (p-1)! = -1 (mod p) by Wilson's. => (p-1)!^(n/p) depends on odd/even of n/p // needs pre-compute fact[] if (n/p % 2 != 0) return res * (p - fact[n % p]) % p; return res * fact[n % p] % p; // no pre-compute fact [] //int mod_fact_np =1; //for(int i=1; i <= n % p; i++){ // mod_fact_np = mod_fact_np * i % p; //} // if (n/p % 2 != 0) return res * (p - mod_fact_np) % p; // return res * mod_fact_np % p; }