let s = read_line () in
let n = String.length s in
let m = 1_000_000_007 in
let ( *@) a b = (a * b) mod m in
let ( +@) a b = (a + b) mod m in

let rec loop i k a b acc =
    if i = n then acc else
        match s.[i] with
        | 'A' -> loop (i + 1) k (a +@ k) b acc
        | 'B' -> loop (i + 1) k a (b +@ a) acc
        | 'C' -> loop (i + 1) k a b (acc +@ b)
        | _   -> loop (i + 1) (k *@ 3) (a *@ 3 +@ k) (b *@ 3 +@ a) (acc *@ 3 +@ b)
in
loop 0 1 0 0 0 |> Printf.printf "%d\n"