#include "bits/stdc++.h" using namespace std; #define FOR(i,a,b) for(int i =(a);i<(b);i++) #define REP(i,n) for(int i=0;i<(n);i++) #define REPm(i,n) for(int i=(n)-1;i>=0;i--) #define REP1(i,n) for(int i=1;i<=(n);i++) #define mp make_pair typedef long long ll; #define MAX_N 100000 const ll mod = 1e9+7; ll factorial[2000000]; ll pow(ll a,ll b){ if(b == 0) return 1; else if(b%2 == 1) return (a*pow(a,b-1))%mod; else{ ll sqr = pow(a,b/2); return (sqr*sqr)%mod; } } ll inv(ll a){ return pow(a,mod-2); } ll nPr(int n,int r){ if(n < r) return 0; else return (factorial[n]*inv(factorial[n-r]))%mod; } ll nCr(int n,int r){ if(n < r) return 0; else{ ll ans = nPr(n,r); return (ans*inv(factorial[r]))%mod; } } int main(){ int T; cin >> T; factorial[0] = 1; REP1(i,2000000-1){ factorial[i] = (i*factorial[i-1])%mod; } REP(i,T){ string S; cin >> S; int ci = 0; while(S[ci] != ',') ci++; int N = stoi(S.substr(2,ci-2)); int K = stoi(S.substr(ci+1,S.length()-ci-2)); ll ans; if(S[0] == 'C'){ ans = nCr(N,K); } else if(S[0] == 'P'){ ans = nPr(N,K); } else{ ans = nCr(N+K-1,K); } cout << ans << endl; } return 0; }