BaoBao is traveling along a line with infinite length.
At the beginning of his trip, he is standing at position 0. At the beginning of each second, if he is standing at position , with probability he will move to position , with probability he will move to position , and with probability he will stay at position . Positions can be positive, 0, or negative.
DreamGrid, BaoBao's best friend, is waiting for him at position . BaoBao would like to meet DreamGrid at position after exactly seconds. Please help BaoBao calculate the probability he can get to position after exactly seconds.
It's easy to show that the answer can be represented as , where and are coprime integers, and is not divisible by . Please print the value of modulo , where is the multiplicative inverse of modulo .
Input
There are multiple test cases. The first line of the input contains an integer (about 10), indicating the number of test cases. For each test case:
The first and only line contains two integers and (). Their meanings are described above.
Output
For each test case output one integer, indicating the answer.
Sample Input
3 2 -2 0 0 0 1
Sample Output
562500004 1 0
#include<cstdio>
#include<string.h>
#include<iostream>
#include<algorithm>
#define mod 1000000007
using namespace std;
typedef long long LL;
LL n,m,p=1000000007;
LL extgcd(LL a,LL b,LL&x,LL&y) //扩展欧几里德返回gcd(a,b) (x为a模b的逆元,y为b模a的逆元)
{
LL d=a;
if(b!=0)
{
d=extgcd(b,a%b,y,x);
y-=(a/b)*x;
}
else
{
x=1;
y=0;
}
return d;
}
LL quick_mod(LL a, LL b)
{
LL ans = 1;
a %= p;
while(b)
{
if(b & 1)
{
ans = ans * a % p;
b--;
}
b >>= 1;
a = a * a % p;
}
return ans;
}
LL C(LL n, LL m)
{
if(m > n) return 0;
LL ans = 1;
for(int i=1; i<=m; i++)
{
LL a = (n + i - m) % p;
LL b = i % p;
ans = ans * (a * quick_mod(b, p-2) % p) % p;
}
return ans;
}
LL Lucas(LL n, LL m) //计算C(n,m)%p,且p一定为素数
{
if(m == 0) return 1;
return C(n % p, m % p) * Lucas(n / p, m / p) % p;
}
int main()
{
int t;
LL n,m;
scanf("%d",&t);
while(t--)
{
scanf("%lld%lld",&n,&m);
if(abs(m)>n)
{
printf("0\n");
continue;
}
n*=2;
LL a=quick_mod(2,n);
LL x=0,y=0;
extgcd(a,p,x,y);
m=n/2+m;
while(x<0)x+=p;
printf("%lld\n",(Lucas(n,m)*x)%p);
}
}