main = interact $ pureMain

pureMain :: String -> String
pureMain s = (\ (_, _, _, ans) -> show ans) $ foldl f (1, 0, 0, 0) s

f :: (Int, Int, Int, Int) -> Char -> (Int, Int, Int, Int)
f (m, a, b, c) 'A' = (m, madd m a, b, c)
f (m, a, b, c) 'B' = (m, a, madd a b, c)
f (m, a, b, c) 'C' = (m, a, b, madd b c)
f (m, a, b, c) '?' = (mmul m 3, madd m (mmul 3 a), madd a (mmul 3 b), madd b (mmul 3 c))

mmul :: Int -> Int -> Int
mmul x y = (x * y) `mod` 1000000007

madd :: Int -> Int -> Int
madd x y = (x + y) `mod` 1000000007