#include #define FOR(i,a,b) for (int i=(a);i<(b);i++) #define FORR(i,a,b) for (int i=(a);i>=(b);i--) #define pb push_back #define pcnt __builtin_popcount #define show(x) cout<<#x<<" = "< pii; typedef vector vi; typedef vector vvi; typedef vector vpii; typedef set si; typedef pair pll; typedef vector vl; typedef vector vvl; typedef vector vpll; typedef set sl; templatestring join(vector&v) {stringstream s;FOR(i,0,sz(v))s<<' '<b)swap(a,b);for(;a>0;b%=a,swap(a,b));return b;} int modpow(ll a,ll n,int m){if(a==0)return a;ll p=1;for(;n>0;n/=2,a=a*a%m)if(n&1)p=p*a%m;return(int)p;} void dout(double d){printf("%.12f\n",d);} const int iinf = 1e9; const ll linf = 1e18; const int mod = 1e9+7; const double pi = acos(-1); const double eps = 1e-10; ll fac[2000001], rfac[2000001], ans; int t, a[2], b; string s; main(){ cin.tie(0); ios::sync_with_stdio(false); fac[0] = 1; rfac[0] = 1; FOR(i, 1, 2000001){ fac[i] = fac[i-1] * i % mod; rfac[i] = modpow(fac[i], mod-2, mod); } cin >> t; FOR(i, 0, t){ cin >> s; b = 0; a[0] = 0; a[1] = 0; FOR(j, 2, sz(s)){ switch(s[j]){ case ')': break; case ',': b++; break; default: a[b] *= 10; a[b] += (s[j] - '0'); } } switch(s[0]){ case 'C': if (a[0] < a[1]){ ans = 0; }else{ ans = fac[a[0]] * rfac[a[1]] % mod * rfac[a[0]-a[1]] % mod; } break; case 'P': if (a[0] < a[1]){ ans = 0; }else{ ans = fac[a[0]] * rfac[a[0]-a[1]] % mod; } break; case 'H': if(a[0] <= 0){ ans = 0; }else{ ans = fac[a[0]+a[1]-1] * rfac[a[0]-1] % mod * rfac[a[1]] % mod; } default: break; } cout << ans << "\n"; } return 0; }