import Control.Monad import qualified Data.Vector as V import Data.List import Data.Int {-- n^p mod m --} powerMod :: Integral a => a -> a -> a -> a powerMod n 0 m = 1 powerMod n 1 m = n `mod` m powerMod n p m = (k * k * (if mod p 2 == 0 then 1 else n)) `mod` m where k = powerMod n (div p 2) m modinv n p = powerMod n (p-2) p factorials n m = scanl (\a b -> (a*b) `mod` m) 1 [1..n] divisor = 10^9+7 numElem = 10^6*2 factorials_ = V.fromList $ factorials numElem divisor invfactorials_ = V.fromList $ reverse $ scanl (\a b -> (a*(b+1))`mod`divisor) (modinv (factorials_ V.! (fromIntegral numElem)) divisor) (reverse [0..numElem-1]) combinationModP_2 n k | n < k || n < 0 || k < 0 = 0 | otherwise = (factorials_ V.! n * invfactorials_ V.! k * invfactorials_ V.! (n-k)) `mod` divisor permutationMod_2 n k | n < k || n < 0 || k < 0 = 0 | otherwise = (factorials_ V.! n * invfactorials_ V.! (n-k)) `mod` divisor hcombinationModP_2 n k | n < 0 || k < 0 = 0 | otherwise = combinationModP_2 (n+k-1) k parse (c:s) = case c of 'C' -> combinationModP_2 n k 'P' -> permutationMod_2 n k 'H' -> hcombinationModP_2 n k where s' = tail . init $ s Just i = elemIndex ',' s' (n,k) = (\(a,b) -> (read a, read $ tail b)) $ splitAt i s' main = do n <- readLn :: IO Int replicateM_ n $ do s <- getLine print $ parse s