#include "bits/stdc++.h" using namespace std; #define REP(i, n) for(int i=0; i<(n); i++) #define MAX 2222222 #define FOR(i, a, b) for(int i=(a);i<(b);i++) #define RFOR(i, a, b) for(int i=(b-1);i>=(a);i--) #define int long long int T; int fact[MAX]; int inv[MAX]; signed main() { int mod = (int)1e9 + 7; // n!をO(N)で求める fact[0] = fact[1] = 1; FOR(i,2,MAX) (fact[i] = i * fact[i-1]) %= mod; // n!の逆元をO(logP+N)で求める int k = 0; int ret = 1; int tmp = fact[MAX-1];; int e = mod - 2; while(1 << k <= e) { if (e & (1 << k)) { ret *= tmp; ret %= mod; } tmp *= tmp; // 2乗していく tmp %= mod; k++; } inv[MAX-1] = ret; // (MAX-1)!の逆元が求まった // O(N)で順番に逆元を埋めていく inv[0] = inv[1] = 1; RFOR(i,2,MAX-1) { inv[i] = inv[i+1] * (i+1) % mod; } cin >> T; char c; int n; vector ans; REP(i,T) { scanf("\n%c(%lld,%lld)", &c, &n, &k); int a = 0; if (c == 'P') { if (n < k) a = 0; else a = fact[n] * inv[n-k] % mod; } else if (c == 'C') { if (n < k) a = 0; else a = fact[n] * inv[n-k] * inv[k] % mod; } else { if (n == 0 && k == 0) a = 1; else if (n < 1) a = 0; else (a = fact[n+k-1] * inv[n-1] * inv[k]) %= mod; } ans.push_back(a); } for (int a : ans) { cout << a << endl; } return 0; }