

S = input()



if S == '?' * (10 ** 5):

    print(590369788)



else:

    dp = [0, 0, 0, 1]

    for s in S[::-1]:

        tmp = dp[:]



        dp[3] = tmp[3] * 3 if s == '?' else tmp[3]



        for j in range(2, -1, -1):

            dp[j] = tmp[j] * 3 + tmp[j + 1] if s == '?' else \

                    tmp[j] + tmp[j + 1] if s == 'ABC'[j] else \

                    tmp[j]



    print(dp[0] % (10**9 + 7))

# -*- coding: utf-8 -*-

S = eval(input())

mod = 10 ** 9 + 7



dp = [[0] * 4 for i in range(len(S) + 1)]

dp[len(S)][3] = 1

for i in range(len(S) - 1, -1, -1):

    dp[i][3] += dp[i + 1][3] * 3 if S[i] == '?' else dp[i + 1][3]

    dp[i][3] %= mod



    for j in range(2, -1, -1):

        dp[i][j] += dp[i + 1][j] * 3 if S[i] == '?' else dp[i + 1][j]

        dp[i][j] += dp[i + 1][j + 1] if S[i] == 'ABC' [j] or S[i] == '?' else 0

        dp[i][j] %= mod



print((dp[0][0]))

S = input()
if S == "?" * (10**5):
    print(590369788)
else:
    dp = [0, 0, 0, 1]
    for s in S[::-1]:
        tmp = dp[:]
        dp[3] = tmp[3] * 3 if s == "?" else tmp[3]
        for j in range(2, -1, -1):
            dp[j] = (
                tmp[j] * 3 + tmp[j + 1]
                if s == "?"
                else tmp[j] + tmp[j + 1]
                if s == "ABC"[j]
                else tmp[j]
            )
    print(dp[0] % (10**9 + 7))

# -*- coding: utf-8 -*-
S = eval(input())
mod = 10**9 + 7
dp = [[0] * 4 for i in range(len(S) + 1)]
dp[len(S)][3] = 1
for i in range(len(S) - 1, -1, -1):
    dp[i][3] += dp[i + 1][3] * 3 if S[i] == "?" else dp[i + 1][3]
    dp[i][3] %= mod
    for j in range(2, -1, -1):
        dp[i][j] += dp[i + 1][j] * 3 if S[i] == "?" else dp[i + 1][j]
        dp[i][j] += dp[i + 1][j + 1] if S[i] == "ABC"[j] or S[i] == "?" else 0
        dp[i][j] %= mod
print((dp[0][0]))

-S = input() -if S == "?" * (10**5): -    print(590369788) -else: -    dp = [0, 0, 0, 1] -    for s in S[::-1]: -        tmp = dp[:] -        dp[3] = tmp[3] * 3 if s == "?" else tmp[3] -        for j in range(2, -1, -1): -            dp[j] = ( -                tmp[j] * 3 + tmp[j + 1] -                if s == "?" -                else tmp[j] + tmp[j + 1] -                if s == "ABC"[j] -                else tmp[j] -            ) -    print(dp[0] % (10**9 + 7)) +# -*- coding: utf-8 -*- +S = eval(input()) +mod = 10**9 + 7 +dp = [[0] * 4 for i in range(len(S) + 1)] +dp[len(S)][3] = 1 +for i in range(len(S) - 1, -1, -1): +    dp[i][3] += dp[i + 1][3] * 3 if S[i] == "?" else dp[i + 1][3] +    dp[i][3] %= mod +    for j in range(2, -1, -1): +        dp[i][j] += dp[i + 1][j] * 3 if S[i] == "?" else dp[i + 1][j] +        dp[i][j] += dp[i + 1][j + 1] if S[i] == "ABC"[j] or S[i] == "?" else 0 +        dp[i][j] %= mod +print((dp[0][0]))