import Data.List
import qualified Data.Vector.Unboxed as V
main=print.solve.V.unfoldr uncons=<<getLine
solve=(\(_,_,_,c)->c).V.foldl f(1,0,0,0)
f(r,a,b,c)v=case v of
  'A'->(r, g$a+r, b, c)
  'B'->(r, a, g$b+a, c)
  'C'->(r, a, b, g$c+b)
  '?'->(g$r*3, g$a*3+r, g$b*3+a, g$c*3+b)
g x=x`mod`(10^9+7)