local mod = 1000000007
local mfl = math.floor
local function bmul(x, y)
  local x0, y0 = x % 31623, y % 31623
  local x1, y1 = mfl(x / 31623), mfl(y / 31623)
  return (x1 * y1 * 14122 + (x1 * y0 + x0 * y1) * 31623 + x0 * y0) % mod
end

local function badd(x, y)
  return (x + y) % mod
end

local s = io.read()
local n = #s
local a, b, c = {}, {0}, {0}
local sstr = s:sub(1, 1)
local z = 1
if sstr == "A" then
  a[1] = 1
elseif sstr == "?" then
  a[1] = 1
  z = 3
else
  a[1] = 0
end
for i = 2, n do
  sstr = s:sub(i, i)
  if sstr == "A" then
    a[i] = badd(a[i - 1], z)
    b[i] = b[i - 1]
    c[i] = c[i - 1]
  elseif sstr == "B" then
    a[i] = a[i - 1]
    b[i] = badd(b[i - 1], a[i - 1])
    c[i] = c[i - 1]
  elseif sstr == "C" then
    a[i] = a[i - 1]
    b[i] = b[i - 1]
    c[i] = badd(c[i - 1], b[i - 1])
  elseif sstr == "?" then
    a[i] = badd(bmul(3, a[i - 1]), z)
    b[i] = badd(bmul(3, b[i - 1]), a[i - 1])
    c[i] = badd(bmul(3, c[i - 1]), b[i - 1])
    z = bmul(z, 3)
  end
end
print(c[n])
