def ary(r, c, v): return [[v for _ in range(c)] for _ in range(r)]



s = eval(input())

ls = len(s)

dp = ary(ls + 1, 4, 0)

dp[0][0] = 1

mod = 10 ** 9 + 7



for i in range(1, ls + 1):

    for j in range(4):

        #'?'のとき'ABC'の3通りに分岐させ、いずれも取らないので3倍

        dp[i][j] += dp[i - 1][j] * (3 if s[i - 1] == '?' else 1)



       #ほしい文字に変えた場合　または　ほしい文字の場合で取ったとき

        if j > 0:

            if s[i - 1] == '?' or s[i - 1] == 'ABC'[j - 1]:

                dp[i][j] += dp[i - 1][j - 1]

        dp[i][j] %= mod

print((dp[ls][3]))
s = eval(input())



MOD = 10 ** 9 + 7



# dp[0 .. len(s)][0..3] = その状態の数

dp = [[0 for _ in range(3 + 1)] for _ in range(len(s) + 1)]

dp[0][0] = 1



for i in range(1, len(s) + 1):

    # s[i]を取らない

    dp[i][0] += dp[i - 1][0] * (3 if s[i - 1] == '?' else 1)

    dp[i][1] += dp[i - 1][1] * (3 if s[i - 1] == '?' else 1)

    dp[i][2] += dp[i - 1][2] * (3 if s[i - 1] == '?' else 1)

    dp[i][3] += dp[i - 1][3] * (3 if s[i - 1] == '?' else 1)



    # s[i]を取る

    if s[i - 1] == 'A':

        dp[i][1] += dp[i - 1][0]

    elif s[i - 1] == 'B':

        dp[i][2] += dp[i - 1][1]

    elif s[i - 1] == 'C':

        dp[i][3] += dp[i - 1][2]

    elif s[i - 1] == '?':

        dp[i][1] += dp[i - 1][0]

        dp[i][2] += dp[i - 1][1]

        dp[i][3] += dp[i - 1][2]



    for j in range(3 + 1):

        dp[i][j] %= MOD



print((dp[len(s)][3]))
def ary(r, c, v):
    return [[v for _ in range(c)] for _ in range(r)]


s = eval(input())
ls = len(s)
dp = ary(ls + 1, 4, 0)
dp[0][0] = 1
mod = 10**9 + 7
for i in range(1, ls + 1):
    for j in range(4):
        #'?'のとき'ABC'の3通りに分岐させ、いずれも取らないので3倍
        dp[i][j] += dp[i - 1][j] * (3 if s[i - 1] == "?" else 1)
        # ほしい文字に変えた場合　または　ほしい文字の場合で取ったとき
        if j > 0:
            if s[i - 1] == "?" or s[i - 1] == "ABC"[j - 1]:
                dp[i][j] += dp[i - 1][j - 1]
        dp[i][j] %= mod
print((dp[ls][3]))

s = eval(input())
MOD = 10**9 + 7
# dp[0 .. len(s)][0..3] = その状態の数
dp = [[0 for _ in range(3 + 1)] for _ in range(len(s) + 1)]
dp[0][0] = 1
for i in range(1, len(s) + 1):
    # s[i]を取らない
    dp[i][0] += dp[i - 1][0] * (3 if s[i - 1] == "?" else 1)
    dp[i][1] += dp[i - 1][1] * (3 if s[i - 1] == "?" else 1)
    dp[i][2] += dp[i - 1][2] * (3 if s[i - 1] == "?" else 1)
    dp[i][3] += dp[i - 1][3] * (3 if s[i - 1] == "?" else 1)
    # s[i]を取る
    if s[i - 1] == "A":
        dp[i][1] += dp[i - 1][0]
    elif s[i - 1] == "B":
        dp[i][2] += dp[i - 1][1]
    elif s[i - 1] == "C":
        dp[i][3] += dp[i - 1][2]
    elif s[i - 1] == "?":
        dp[i][1] += dp[i - 1][0]
        dp[i][2] += dp[i - 1][1]
        dp[i][3] += dp[i - 1][2]
    for j in range(3 + 1):
        dp[i][j] %= MOD
print((dp[len(s)][3]))

-def ary(r, c, v): -    return [[v for _ in range(c)] for _ in range(r)] - - -ls = len(s) -dp = ary(ls + 1, 4, 0) +MOD = 10**9 + 7 +# dp[0 .. len(s)][0..3] = その状態の数 +dp = [[0 for _ in range(3 + 1)] for _ in range(len(s) + 1)] -mod = 10**9 + 7 -for i in range(1, ls + 1): -    for j in range(4): -        #'?'のとき'ABC'の3通りに分岐させ、いずれも取らないので3倍 -        dp[i][j] += dp[i - 1][j] * (3 if s[i - 1] == "?" else 1) -        # ほしい文字に変えた場合　または　ほしい文字の場合で取ったとき -        if j > 0: -            if s[i - 1] == "?" or s[i - 1] == "ABC"[j - 1]: -                dp[i][j] += dp[i - 1][j - 1] -        dp[i][j] %= mod -print((dp[ls][3])) +for i in range(1, len(s) + 1): +    # s[i]を取らない +    dp[i][0] += dp[i - 1][0] * (3 if s[i - 1] == "?" else 1) +    dp[i][1] += dp[i - 1][1] * (3 if s[i - 1] == "?" else 1) +    dp[i][2] += dp[i - 1][2] * (3 if s[i - 1] == "?" else 1) +    dp[i][3] += dp[i - 1][3] * (3 if s[i - 1] == "?" else 1) +    # s[i]を取る +    if s[i - 1] == "A": +        dp[i][1] += dp[i - 1][0] +    elif s[i - 1] == "B": +        dp[i][2] += dp[i - 1][1] +    elif s[i - 1] == "C": +        dp[i][3] += dp[i - 1][2] +    elif s[i - 1] == "?": +        dp[i][1] += dp[i - 1][0] +        dp[i][2] += dp[i - 1][1] +        dp[i][3] += dp[i - 1][2] +    for j in range(3 + 1): +        dp[i][j] %= MOD +print((dp[len(s)][3]))