import std.bigint,std.conv,std.stdio,std.string;int M=10^^9+7,S=4300000,q;long y,z,a,b,e;void i(long n){y=1;for(e=M-2;e;e/=2,n=n*n%M)if(e&1)y=y*n%M;}void f(int n){if(n>M/2){f(M-1-n);i(z);z=n%2?y:M-y;}else{z=1;if(n%S>S/2){for(;n%S;)z=z*++n%M;i(z);}else{for(;n%S;)z=z*n--%M;y=z;}z=0;foreach(j;0..5)z=z<<6|` ! AME9%W.!FNQZ3)EVNKXA9GVL!S!M\-$2+VQ3- 8QW,#:1TZ\BW D]5BK7D(U_V0YA@P:TH*&N>/IH52L$]M)_,T[VG!AB0Q81E+T)*^I6&VHJ_MK>H$U#,MCOF%T7 F^''TP<.!!)*C:!U0NFYSAM#Y%#KOG1;SNK"+FXATT)/84T7&K:3(DN2C7%/'AGPG^X) J(Q!/+O:M02XA-FW2)'M(VEPYJ'AX/JED#O=MX;DLW!PTAC1"$484IIGJ:,*U,2U@>KY!,3$(6#FGE=F"#!"^B0<2QHR V!)=81:KVY9T^ZMH+*D`[n/S*5+j]-32;z=z*y%M;}}void main(){foreach(t;0..readln.chomp.to!int){auto s=readln.split;BigInt C=s[0],P=s[1],r=C+1-P,l=r-P;a=0;if(l>=0&&l/M==r/M){f(r%M);a=z;f(l%M);i(z);a=a*y%M;}a.writeln;}}