{-# OPTIONS_GHC -O2 -funbox-strict-fields #-}

main :: IO ()
main = do
  s <- getLine
  print $ solve s

solve :: String -> Int
solve s = ans where
  (_, _, _, ans) = foldl f (1, 0, 0, 0) s
  f (n, a, ab, abc) 'A' = (n, n `add` a, ab, abc)
  f (n, a, ab, abc) 'B' = (n, a, ab `add` a, abc)
  f (n, a, ab, abc) 'C' = (n, a, ab, abc `add` ab)
  f (n, a, ab, abc) '?' = (mul3 n, mul3 a `add` n, mul3 ab `add` a, mul3 abc `add` ab)
  f _ _ = undefined

add :: Int -> Int -> Int
add x y = (x + y) `mod` modN

mul3 :: Int -> Int
mul3 x = (x * 3) `mod` modN

modN :: Int
modN = 10 ^ 9 + 7
