# 自然数aをn乗してmodを取った数

def fast_pow(a:int, n:int, mod:int) -> int:

    

    res = 1

    

    while n>0:

        if n & 1:

            res = res*a % mod

            

        a = a*a % mod

        n >>= 1

        

    return res





S = eval(input())

N = len(S)

mod = 10**9+7



# 初期設定

a_left = 0

q_left = 0

c_right = S.count("C")

q_right = S.count("?")

ans = 0



# 0回目の更新

if S[0] == "C":

    c_right -= 1

elif S[0] == "?":

    q_right -= 1

    

# 1～N-1回目の更新

for i in range(N-1):

    if S[i] == "A":

        a_left += 1

    elif S[i] == "?":

        q_left += 1

        

    if S[i+1] == "C":

        c_right -= 1

    elif S[i+1] == "?":

        q_right -= 1

        

    if S[i+1] == "B" or S[i+1] == "?":

        ans += ((q_left*fast_pow(3,q_left-1,mod)+a_left*fast_pow(3,q_left,mod))\

                * (q_right*fast_pow(3,q_right-1,mod)+c_right*fast_pow(3,q_right,mod)) % mod)





        

print((ans%mod))
# Input

import sys

stdin = sys.stdin



sys.setrecursionlimit(10 ** 7)



def li(): return list(map(int, stdin.readline().split()))

def li_(): return [int(x) - 1 for x in stdin.readline().split()]

def lf(): return list(map(float, stdin.readline().split()))

def ls(): return stdin.readline().split()

def ns(): return stdin.readline().rstrip()

def lc(): return list(ns())

def ni(): return int(stdin.readline())

def nf(): return float(stdin.readline())



s = ns()

t = len(s)



a__ = [0]*(t+1)

ab_ = [0]*(t+1)

abc = [0]*(t+1)



MOD = 10**9 + 7

q = 0

for i, si in enumerate(s):

    if si == 'A':

        a__[i+1] = a__[i] + pow(3,q,MOD)

        ab_[i+1] = ab_[i]

        abc[i+1] = abc[i]



    elif si == 'B':

        a__[i+1] = a__[i]

        ab_[i+1] = ab_[i] + a__[i]

        abc[i+1] = abc[i]



    elif si == 'C':

        a__[i+1] = a__[i]

        ab_[i+1] = ab_[i]

        abc[i+1] = abc[i] + ab_[i]



    elif si == '?':

        a__[i+1] = 3 * a__[i] + pow(3,q,MOD)

        ab_[i+1] = 3 * ab_[i] + a__[i]

        abc[i+1] = 3 * abc[i] + ab_[i]



        q += 1





    a__[i + 1] %= MOD

    ab_[i + 1] %= MOD

    abc[i + 1] %= MOD



print((abc[t]))
# 自然数aをn乗してmodを取った数
def fast_pow(a: int, n: int, mod: int) -> int:
    res = 1
    while n > 0:
        if n & 1:
            res = res * a % mod
        a = a * a % mod
        n >>= 1
    return res


S = eval(input())
N = len(S)
mod = 10**9 + 7
# 初期設定
a_left = 0
q_left = 0
c_right = S.count("C")
q_right = S.count("?")
ans = 0
# 0回目の更新
if S[0] == "C":
    c_right -= 1
elif S[0] == "?":
    q_right -= 1
# 1～N-1回目の更新
for i in range(N - 1):
    if S[i] == "A":
        a_left += 1
    elif S[i] == "?":
        q_left += 1
    if S[i + 1] == "C":
        c_right -= 1
    elif S[i + 1] == "?":
        q_right -= 1
    if S[i + 1] == "B" or S[i + 1] == "?":
        ans += (
            (q_left * fast_pow(3, q_left - 1, mod) + a_left * fast_pow(3, q_left, mod))
            * (
                q_right * fast_pow(3, q_right - 1, mod)
                + c_right * fast_pow(3, q_right, mod)
            )
            % mod
        )
print((ans % mod))

# Input
import sys

stdin = sys.stdin
sys.setrecursionlimit(10**7)


def li():
    return list(map(int, stdin.readline().split()))


def li_():
    return [int(x) - 1 for x in stdin.readline().split()]


def lf():
    return list(map(float, stdin.readline().split()))


def ls():
    return stdin.readline().split()


def ns():
    return stdin.readline().rstrip()


def lc():
    return list(ns())


def ni():
    return int(stdin.readline())


def nf():
    return float(stdin.readline())


s = ns()
t = len(s)
a__ = [0] * (t + 1)
ab_ = [0] * (t + 1)
abc = [0] * (t + 1)
MOD = 10**9 + 7
q = 0
for i, si in enumerate(s):
    if si == "A":
        a__[i + 1] = a__[i] + pow(3, q, MOD)
        ab_[i + 1] = ab_[i]
        abc[i + 1] = abc[i]
    elif si == "B":
        a__[i + 1] = a__[i]
        ab_[i + 1] = ab_[i] + a__[i]
        abc[i + 1] = abc[i]
    elif si == "C":
        a__[i + 1] = a__[i]
        ab_[i + 1] = ab_[i]
        abc[i + 1] = abc[i] + ab_[i]
    elif si == "?":
        a__[i + 1] = 3 * a__[i] + pow(3, q, MOD)
        ab_[i + 1] = 3 * ab_[i] + a__[i]
        abc[i + 1] = 3 * abc[i] + ab_[i]
        q += 1
    a__[i + 1] %= MOD
    ab_[i + 1] %= MOD
    abc[i + 1] %= MOD
print((abc[t]))

-# 自然数aをn乗してmodを取った数 -def fast_pow(a: int, n: int, mod: int) -> int: -    res = 1 -    while n > 0: -        if n & 1: -            res = res * a % mod -        a = a * a % mod -        n >>= 1 -    return res +# Input +import sys + +stdin = sys.stdin +sys.setrecursionlimit(10**7) -S = eval(input()) -N = len(S) -mod = 10**9 + 7 -# 初期設定 -a_left = 0 -q_left = 0 -c_right = S.count("C") -q_right = S.count("?") -ans = 0 -# 0回目の更新 -if S[0] == "C": -    c_right -= 1 -elif S[0] == "?": -    q_right -= 1 -# 1～N-1回目の更新 -for i in range(N - 1): -    if S[i] == "A": -        a_left += 1 -    elif S[i] == "?": -        q_left += 1 -    if S[i + 1] == "C": -        c_right -= 1 -    elif S[i + 1] == "?": -        q_right -= 1 -    if S[i + 1] == "B" or S[i + 1] == "?": -        ans += ( -            (q_left * fast_pow(3, q_left - 1, mod) + a_left * fast_pow(3, q_left, mod)) -            * ( -                q_right * fast_pow(3, q_right - 1, mod) -                + c_right * fast_pow(3, q_right, mod) -            ) -            % mod -        ) -print((ans % mod)) +def li(): +    return list(map(int, stdin.readline().split())) + + +def li_(): +    return [int(x) - 1 for x in stdin.readline().split()] + + +def lf(): +    return list(map(float, stdin.readline().split())) + + +def ls(): +    return stdin.readline().split() + + +def ns(): +    return stdin.readline().rstrip() + + +def lc(): +    return list(ns()) + + +def ni(): +    return int(stdin.readline()) + + +def nf(): +    return float(stdin.readline()) + + +s = ns() +t = len(s) +a__ = [0] * (t + 1) +ab_ = [0] * (t + 1) +abc = [0] * (t + 1) +MOD = 10**9 + 7 +q = 0 +for i, si in enumerate(s): +    if si == "A": +        a__[i + 1] = a__[i] + pow(3, q, MOD) +        ab_[i + 1] = ab_[i] +        abc[i + 1] = abc[i] +    elif si == "B": +        a__[i + 1] = a__[i] +        ab_[i + 1] = ab_[i] + a__[i] +        abc[i + 1] = abc[i] +    elif si == "C": +        a__[i + 1] = a__[i] +        ab_[i + 1] = ab_[i] +        abc[i + 1] = abc[i] + ab_[i] +    elif si == "?": +        a__[i + 1] = 3 * a__[i] + pow(3, q, MOD) +        ab_[i + 1] = 3 * ab_[i] + a__[i] +        abc[i + 1] = 3 * abc[i] + ab_[i] +        q += 1 +    a__[i + 1] %= MOD +    ab_[i + 1] %= MOD +    abc[i + 1] %= MOD +print((abc[t]))