## implementation

#include <cstdio>
#include <vector>
#include <cassert>
#define repeat(i,n) for (int i = 0; (i) < (n); ++(i))
#define repeat_from(i,m,n) for (int i = (m); (i) < (n); ++(i))
typedef long long ll;
using namespace std;

ll powi(ll x, ll y, ll p) { // O(log y)
assert (y >= 0);
x = (x % p + p) % p;
ll z = 1;
for (ll i = 1; i <= y; i <<= 1) {
if (y & i) z = z * x % p;
x = x * x % p;
}
return z;
}
ll inv(ll x, ll p) { // p must be a prime, O(log p)
assert ((x % p + p) % p != 0);
return powi(x, p-2, p);
}
const int mod = 1e9+7;
int fact(int n) {
static vector<int> memo(1,1);
if (memo.size() <= n) {
int l = memo.size();
memo.resize(n+1);
repeat_from (i,l,n+1) memo[i] = memo[i-1] *(ll) i % mod;
}
return memo[n];
}
int choose(int n, int r) { // O(n) at first time, otherwise O(1)
if (n < r) return 0;
r = min(r, n - r);
return fact(n) *(ll) inv(fact(n-r), mod) % mod *(ll) inv(fact(r), mod) % mod;
}
int permute(int n, int r) {
if (n < r) return 0;
return fact(n) *(ll) inv(fact(n-r), mod) % mod;
}
int multichoose(int n, int r) {
if (n == 0 and r == 0) return 1;
return choose(n+r-1, r);
}

int main() {
int n; scanf("%d", &n);
while (n --) {
char f; int n, k; scanf(" %c(%d,%d)", &f, &n, &k);
printf("%d\n",
f == 'C' ? choose(n,k) :
f == 'P' ? permute(n,k) :
f == 'H' ? multichoose(n,k) : -1);
}
return 0;
}