# include # include # include # include # include # include # include # include # include # include # include # include # include # include using namespace std; using LL = long long; using ULL = unsigned long long; constexpr long long MOD = 1000000000 + 7; constexpr long long INF = 100000000; const double PI = acos(-1); //組み合わせ(コンビネーション) int fact[100000000]; LL factrial(LL n, LL mod) { if (fact[n] != 0)return fact[n]; if (n == 0)return fact[n] = 1; return fact[n] = factrial(n - 1, mod)*n%mod; } void factrialinitialize(LL n, LL mod) { fact[0] = 1; for (LL i = 1; i < n; i++) { fact[i] = i * fact[i - 1] % mod; } } LL repeatsquaring(LL n, LL p, LL mod) { if (n == 0)return 0; if (p <= 0 || n == 1)return 1; if (p % 2 == 0) { LL t = repeatsquaring(n, p / 2, mod); return t*t%mod; } return n*repeatsquaring(n, p - 1, mod) % mod; } LL permutation(LL n, LL r, LL mod) { if (n < r)return 0; if (n == r)return factrial(n, mod); LL ans = 1; ans *= factrial(n, mod); ans %= mod; ans *= repeatsquaring(factrial(n - r, mod), mod - 2, mod); ans %= mod; return ans; } LL combination(LL n, LL r, LL mod) { if (r == 0) return 1; if (n < r)return 0; LL ans = 1; ans *= factrial(n, mod); ans %= mod; ans *= repeatsquaring(factrial(n - r, mod), mod - 2, mod); ans %= mod; ans *= repeatsquaring(factrial(r, mod), mod - 2, mod); ans %= mod; return ans; } LL CombinationWithRepetition(LL n, LL r, LL mod) { return combination(n + r - 1, r, mod); } int main() { factrialinitialize(100000000, MOD); //(n,r,MOD) int n; cin >> n; int a, b; char c, d; for (int i = 0; i < n; i++) { cin >> c >> d >> a >> d >> b >> d; if (c == 'C')cout << combination(a, b, MOD) << endl; if (c == 'P')cout << permutation(a, b, MOD) << endl; if (c == 'H')cout << CombinationWithRepetition(a, b, MOD) << endl; } }