#include "bits/stdc++.h" using namespace std; #define ll long long int #define rep(i,n) for( int i = 0; i < n; i++ ) #define rrep(i,n) for( int i = n; i >= 0; i-- ) #define REP(i,s,t) for( int i = s; i <= t; i++ ) #define RREP(i,s,t) for( int i = s; i >= t; i-- ) #define dump(x) cerr << #x << " = " << (x) << endl; #define INF 2000000000 #define mod 1000000007 #define INF2 1000000000000000000 #define int long long int F[1100010]; int Mul(int a, int b) { return ((a % mod) * (b % mod)) % mod; } int fact(void) { F[0] = 1; F[1] = 1; for (int i = 2; i <= 1100000; i++) { F[i] = Mul(i, F[i - 1]); } } int square(int x) { return (x * x) % mod; } int power(int x, int y) { if (y == 0) return 1; else if (y == 1) return x % mod; else if (y % 2 == 0) return square(power(x, y / 2)) % mod; else return square(power(x, y / 2)) * x % mod; } int Div(int a, int b) { return Mul(a, power(b, mod - 2)); } int P(int n, int r) { if (n < r) return 0; return Div(F[n], F[n - r]); } int C(int n, int r) { if (n < r) return 0; int ret; ret = Div(F[n], Mul(F[n - r], F[r])); return ret; } int H(int n, int r) { return C(n + r - 1, r); } signed main(void) { cin.tie(0); ios::sync_with_stdio(false); fact(); int T; cin >> T; rep(j, T) { string S; cin >> S; char kind = S[0]; int i = 2; string tmp1, tmp2; while (S[i] != ',') { tmp1 += S[i++]; } i++; while (S[i] != ')') { tmp2 += S[i++]; } int left, right; left = stoi(tmp1); right = stoi(tmp2); if (kind == 'C') { cout << C(left, right) << endl; } else if (kind == 'P') { cout << P(left, right) << endl; } else { cout << H(left, right) << endl; } } return 0; }