import sys read=sys.stdin.buffer.read readline=sys.stdin.buffer.readline readlines=sys.stdin.buffer.readlines t=int(readline()) MOD=10**9+7 MAX=2*(10**6) f=[1]*(MAX+1) invf=[1]*(MAX+1) for i in range(2, MAX+1): f[i]=f[i-1]*i%MOD invf[MAX]=pow(f[MAX], MOD-2, MOD) for i in range(MAX-1, -1, -1): invf[i]=invf[i+1]*(i+1)%MOD def comb(x, y): if not (0<=y and y<=x): return 0 return f[x]*invf[y]*invf[x-y]%MOD import re for _ in range(t): s=readline().decode() v=re.findall(r'\d+', s) x, y=int(v[0]), int(v[1]) if s[0]=='C': print(comb(x, y)) elif s[0]=='P': print(comb(x, y)*f[y]%MOD) else: print(comb(x+y-1, y))