MOD = 10**9 + 7 def add_one(s): s_list = list(s) i = len(s_list) - 1 carry = 1 while i >= 0 and carry: digit = int(s_list[i]) + carry if digit == 10: s_list[i] = '0' carry = 1 else: s_list[i] = str(digit) carry = 0 i -= 1 if carry: s_list = ['1'] + s_list return ''.join(s_list) def str_compare(a, b): if len(a) < len(b): return True elif len(a) > len(b): return False else: return a <= b def mod_str(s, mod): res = 0 for c in s: res = (res * 10 + int(c)) % mod return res T = int(input()) for _ in range(T): C_str, P_str = input().split() # Check if P >= MOD if len(P_str) > 10 or (len(P_str) == 10 and P_str > "1000000007"): print(0) continue p_val = int(P_str) if p_val >= MOD: print(0) continue # Check if 2*p_val <= C + 1 C_plus_1_str = add_one(C_str) two_p = 2 * p_val two_p_str = str(two_p) if not str_compare(two_p_str, C_plus_1_str): print(0) continue # Compute product c_mod = mod_str(C_str, MOD) product = 1 for k in range(p_val): term = (c_mod - (p_val - 1 + k)) % MOD product = (product * term) % MOD print(product)