{-# LANGUAGE BangPatterns #-}
import Control.Monad
import Data.List
import Data.Maybe

m = 10 ^ 9 + 7 :: Integer

solve :: String -> Int -> Integer
solve !s !n =
  (\(_, _, _, x, _) -> x) $
    foldl'
      ( \(!none, !a, !ab, !abc, !s) _ ->
          case head s of
            'A' -> (none, a + none, ab, abc, tail s)
            'B' -> (none, a, ab + a, abc, tail s)
            'C' -> (none, a, ab, abc + ab, tail s)
            _ -> (none * 3, a * 3 + none, ab * 3 + a, abc * 3 + ab, tail s)
      )
      (1, 0, 0, 0, s)
      [1 .. n]

main = do
  s <- getLine
  print $ (`mod` m) . solve s $ length s