h="H";c="C";M=10**9+7;U=2000001;F=[1];FI=[0]*U for i in xrange(1,U):F+=[(i*F[i-1])%M] FI[U-1]=pow(F[U-1],M-2,M) for i in xrange(U-1,0,-1):FI[i-1]=i*FI[i]%M for i in xrange(input()): q=raw_input().split(",");Q=q[0][0];N=int(q[0][2:]);K=int(q[1][:-1]) if Q == h: print ((N!=0)*F[N-1+K]*FI[N-1]%M*FI[K]%M)**(K!=0) else: print (N>=K)*FI[K]**(Q=='C')*F[N]%M*FI[N-K]%M