p x y = (x+y) `mod` (10^9+7)
h (a,b,c) (d,e,f) = (a`p`d,b`p`e,c`p`f)
g n (x,y,z) 'A' = (x`p`n,y,z)
g n (x,y,z) 'B' = (x,y`p`x,z)
g n (x,y,z) 'C' = (x,y,z`p`y)
f _ (_,_,x) [] = x 
f n t ('?':s) = f (p 0 $ n*3) (g n t 'A' `h` g n t 'B' `h` g n t 'C') s
f n t (c:s) = f n (g n t c) s
main = do
  s <- getLine
  print $ f 1 (0,0,0) s