import sys
def I(): return int(sys.stdin.readline().rstrip())
def S(): return sys.stdin.readline().rstrip()


mod = 10**9+7

fac = [0]*(2*10**6)
fac[0] = 1
for i in range(1,2*10**6):
    fac[i] = (fac[i-1]*i) % mod
a = pow(fac[-1],mod-2,mod)
fac_inv = [0]*(2*10**6)
for i in range(2*10**6-1,-1,-1):
    fac_inv[i] = a
    a = (a*i) % mod


def nCk(n,k):
    if n < k:
        return 0
    return (fac[n]*fac_inv[k]*fac_inv[n-k]) % mod


def nPk(n,k):
    if n < k:
        return 0
    return (fac[n]*fac_inv[n-k]) % mod


def nHk(n,k):
    if n == 0:
        if k == 0:
            return 1
        return 0
    return (fac[n+k-1]*fac_inv[k]*fac_inv[n-1]) % mod


N = I()
for _ in range(N):
    X = S()
    i = X.index(',')
    N = int(X[2:i])
    K = int(X[i+1:-1])
    if X[0] == 'C':
        print(nCk(N,K))
    elif X[0] == 'P':
        print(nPk(N,K))
    else:
        print(nHk(N,K))