#include using namespace std; typedef long long ll; typedef vector VI; typedef vector VVI; #define REP(i, n) for(int(i)=0;(i)<(n);++(i)) int T; ll fact[2000001]; const int MOD = 1000000000+7; void init(){ fact[0] = fact[1] = 1; for(int i = 2; i <= 2000000; i++){ fact[i] = (fact[i-1] * i) % MOD; } } ll extgcd(ll a,ll b,ll &m,ll &n){ll g=a;m=1;n=0;if(b)g=extgcd(b,a%b,n,m),n-=(a/b)*m;return g;} ll divmod(ll n, ll m, ll mod){ ll a,b; extgcd(m,mod,a,b); return (n * a) % mod; } int c(int n, int r){ if(n < r) return 0; return divmod(fact[n], (fact[r] * fact[n-r])%MOD, MOD); } int p(int n, int r){ if(n < r) return 0; return divmod(fact[n], fact[n-r], MOD); } int h(int n, int r){ if(n == 0 && r == 0) return 1; return c(n+r-1,r); } int main(){ init(); cin >> T; string s; REP(i,T){ int n,m,res = 0; cin >> s; if(sscanf(s.c_str(), "C(%d,%d)", &n, &m) == 2){ res = c(n,m); } if(sscanf(s.c_str(), "P(%d,%d)", &n, &m) == 2){ res = p(n,m); } if(sscanf(s.c_str(), "H(%d,%d)", &n, &m) == 2){ res = h(n,m); } cout << ((res + MOD) % MOD) << endl; } }