先处理最大值的逆元倒着初始化效率更快
#include <iostream>
using namespace std;
typedef long long ll;
const int maxn = 1e5 + 10;
const ll mod = 1e9 + 7;
ll n;
ll a,b;
ll fact[maxn];
ll inv[maxn];
ll quickpow(ll a, ll b)
{
ll ans = 1 % mod;
while(b)
{
if(b & 1)
ans = ans * a % mod;
a = a * a % mod;
b >>= 1;
}
return ans;
}
void init(){
fact[0] = 1;
for(int i = 1;i <= maxn ;i ++)
fact[i] = fact[i - 1] * i % mod;
inv[maxn] = quickpow(fact[maxn], mod - 2);
for(int i = maxn - 1; i >= 0; i --)
inv[i] = inv[i + 1] * (i + 1) % mod;//1/(i-1)!=i/(i)!
}
ll C(ll x, ll y){
if(x < y)
return 0;
return fact[x] % mod * inv[y] % mod * inv[x - y] % mod;
}
int main()
{
scanf("%lld",&n);
init();
while(n --)
{
scanf("%lld%lld",&a,&b);
printf("%lld\n",C(a, b)%mod);
}
return 0;
}