import Control.Monad import Data.Array 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_ = listArray (0,numElem) $ factorials numElem divisor invfactorials_ = listArray (0,numElem) $ reverse $ scanl (\a b -> (a*(b+1))`mod`divisor) (modinv (factorials_!numElem) divisor) (reverse [0..numElem-1]) combinationModP_2 n k | n < k || n < 0 || k < 0 = 0 | otherwise = (factorials_!n * invfactorials_!k * invfactorials_!(n-k)) `mod` divisor permutationMod_2 n k | n < k || n < 0 || k < 0 = 0 | otherwise = (factorials_!n * invfactorials_!(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