Problem:
Score : 300 points

Problem Statement

There are N people. The name of the i-th person is S_i.

We would like to choose three people so that the following conditions are met:

The name of every chosen person begins with M, A, R, C or H.

There are no multiple people whose names begin with the same letter.

How many such ways are there to choose three people, disregarding order?

Note that the answer may not fit into a 32-bit integer type.

Constraints

1 \leq N \leq 10^5

S_i consists of uppercase English letters.

1 \leq |S_i| \leq 10

S_i \neq S_j (i \neq j)

Input

Input is given from Standard Input in the following format:

N
S_1
:
S_N

Output

If there are x ways to choose three people so that the given conditions are met, print x.

Sample Input 1

5
MASHIKE
RUMOI
OBIRA
HABORO
HOROKANAI

Sample Output 1

2

We can choose three people with the following names:

MASHIKE, RUMOI, HABORO

MASHIKE, RUMOI, HOROKANAI

Thus, we have two ways.

Sample Input 2

4
ZZ
ZZZ
Z
ZZZZZZZZZZ

Sample Output 2

0

Note that there may be no ways to choose three people so that the given conditions are met.

Sample Input 3

5
CHOKUDAI
RNG
MAKOTO
AOKI
RINGO

Sample Output 3

7

Solution in Haskell (GHC 7.10.3) (judge verdict: Wrong Answer)

import qualified Data.Vector.Unboxed as VU
import qualified Data.Vector.Unboxed.Mutable as VUM
import qualified Data.ByteString.Char8 as B
import Data.List
import Control.Monad

add vec xs
    | h == 'M' = VUM.modify vec (+1) 0
    | h == 'A' = VUM.modify vec (+1) 1
    | h == 'R' = VUM.modify vec (+1) 2
    | h == 'C' = VUM.modify vec (+1) 3
    | h == 'H' = VUM.modify vec (+1) 4
    | otherwise = VUM.modify vec (+1) 5
    where
        h = B.head xs

toBit n acc l
    | n <= 1 =
        let
            res = ((n `rem` 2):acc)
            l' = length res
        in
            if l' < l
                then reverse $  res ++ (replicate (l-l') 0)
                else reverse res
    | otherwise = toBit (n `quot` 2) ((n `rem` 2):acc) l

solve :: [Int] -> Int -> Int -> [Int] 
solve xs n l
    | n < 0 = []
    | ones == 3 = (loop xs bs) : solve xs (n-1) l
    | otherwise = solve xs (n-1) l
    where
        ones = length $ filter (==1) bs
        bs = toBit n [] l
        loop [] [] = 1
        loop (x:xs) (b:bs)
            | b == 1 = x * loop xs bs
            | otherwise = loop xs bs

main = do
    n <- readLn :: IO Int
    tbl <- VUM.replicate 6 0
    forM_ [1..n] $ \_ -> do
        bs <- B.getLine
        add tbl bs
    tbl' <- VU.freeze tbl
    print tbl'
    let f = VU.toList $ VU.filter (/=0) (VU.init tbl')
    let l = length f
    print $ 
        if l < 3
            then 0
            else if l == 3 
                then product f
                else sum $ solve f (2^l-1) l