program main
  implicit none
  integer(8) :: n, i, j, q, r, cnt, m
  integer(8), allocatable :: ca(:), cc(:), cq(:), pt(:)
  character(10 ** 5) :: s
  m = 10 ** 9 + 7
  read *, s
  n = len_trim(s)
  allocate (ca(0:n), cc(0:n), cq(0:n), pt(-3:n))
  ca = 0
  cc = 0
  cq = 0
  do i = 1, n
    if (s(i:i) == "A") ca(i) = 1
    if (s(i:i) == "C") cc(i) = 1
    if (s(i:i) == "?") cq(i) = 1
    ca(i) = ca(i) + ca(i - 1)
    cc(i) = cc(i) + cc(i - 1)
    cq(i) = cq(i) + cq(i - 1)
  end do
  q = cq(n)

  call prep_pt()

  cnt = 0
  do j = 2, n - 1
    if (s(j:j) == "A" .or. s(j:j) == "C") cycle
    if (s(j:j) == "?") then
      r = q - 1
    else if (s(j:j) == "B") then
      r = q
    end if
    cnt = cnt + ca(j - 1) * (cc(n) - cc(j)) * pt(r)
    cnt = modulo(cnt, m)
    cnt = cnt + cq(j - 1) * (cc(n) - cc(j)) * pt(r - 1)
    cnt = modulo(cnt, m)
    cnt = cnt + ca(j - 1) * (cq(n) - cq(j)) * pt(r - 1)
    cnt = modulo(cnt, m)
    cnt = cnt + cq(j - 1) * (cq(n) - cq(j)) * pt(r - 2)
    cnt = modulo(cnt, m)
  end do
  print "(i0)", cnt

  contains

  subroutine prep_pt()
    integer(8) :: i
    pt(:) = 0
    pt(0) = 1
    do i = 1, n
      pt(i) = modulo(pt(i - 1) * 3, m)
    end do
  end subroutine prep_pt
end program main
