#include using namespace std; const int MOD7 = ( int ) 1e9 + 7; const int MAXN = ( int ) 1e6; const int MAXK = ( int ) 1e6; int fact[ MAXN + MAXK ], inv_fact[ MAXN + MAXK ]; int int_pow( int x, int p ){ int res = 1; while( p ){ if( p & 1 ) res = 1LL * res * x % MOD7; p >>= 1; x = 1LL * x * x % MOD7; } return res; } int inv( int x ){ return int_pow( x, MOD7 - 2 ); } signed main(){ for( int i = fact[ 0 ] = inv_fact[ 0 ] = 1; i < MAXN + MAXK; ++i ) fact[ i ] = 1LL * fact[ i - 1 ] * i % MOD7, inv_fact[ i ] = 1LL * inv_fact[ i - 1 ] * inv( i ) % MOD7; int T; cin >> T; for( int kase = 0; kase < T; ++kase ){ string S; cin >> S; char type = S[ 0 ]; int N = 0, K = 0; for( int i = 2; i < S.size(); ++i ){ if( S[ i ] == ',' ){ for( int j = i + 1; S[ j ] != ')'; ++j ) K = K * 10 + S[ j ] - '0'; break; } N = N * 10 + S[ i ] - '0'; } if( N == 0 and K == 0 ) cout << 1 << endl; else if( N == 0 ) cout << 0 << endl; else if( ( type == 'C' or type == 'P' ) and N < K ) cout << 0 << endl; else if( type == 'C' ) cout << 1LL * fact[ N ] * inv_fact[ K ] % MOD7 * inv_fact[ N - K ] % MOD7 << endl; else if( type == 'P' ) cout << 1LL * fact[ N ] * inv_fact[ N - K ] % MOD7 << endl; else cout << 1LL * fact[ N + K - 1 ] * inv_fact[ K ] % MOD7 * inv_fact[ N - 1 ] % MOD7 << endl; } return 0; }