import scala.collection.mutable.{Map => MMap}
object Main extends App {
  val in = new java.util.Scanner(System.in)
  val S = in.next
  val Mod = 1000000007

  val NA = 0
  val A = 1
  val AB = 2
  val ABC = 3

  val ar = Array.fill(4)(0L)
  ar(NA) = 1

  def dp(l: List[Char]): Long = l match {
    case Nil => ar(ABC)
    case x :: xs => x match {
      case 'A' => ar(A) = (ar(A) + ar(NA)) % Mod
      case 'B' => ar(AB) = (ar(AB) + ar(A)) % Mod
      case 'C' => ar(ABC) = (ar(ABC) + ar(AB)) % Mod
      case _ =>
        ar(ABC) = (ar(ABC) * 3 + ar(AB)) % Mod
        ar(AB) = (ar(AB) * 3 + ar(A)) % Mod
        ar(A) = (ar(A) * 3 + ar(NA)) % Mod
        ar(NA) = (ar(NA) * 3) % Mod
      }
      dp(xs)
  }

  println(dp(S.toList))
}