# coding:utf-8



import sys





input = sys.stdin.readline

INF = float('inf')

MOD = 10 ** 9 + 7





def inpl(): return list(map(int, input().split()))





S = input().rstrip()



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

dp[0][0] = 1

# print(' ', dp[0][:])

for i in range(len(S)):

    # ABCカウントを更新しない

    for j in range(4):

        if S[i] != '?':

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

        else:

            dp[i + 1][j] += dp[i][j] * 3



        dp[i + 1][j] %= MOD



    # ABCカウントを更新する

    if S[i] in ('A', '?'): dp[i + 1][1] += dp[i][0]

    if S[i] in ('B', '?'): dp[i + 1][2] += dp[i][1]

    if S[i] in ('C', '?'): dp[i + 1][3] += dp[i][2]



    for j in range(1, 4):

        dp[i + 1][j] %= MOD



    # print(S[i], dp[i + 1][:])



print((dp[-1][-1]))

# coding:utf-8



import sys





input = sys.stdin.readline

INF = float('inf')

MOD = 10 ** 9 + 7





def inpl(): return list(map(int, input().split()))





S = input().rstrip()



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

dp[0][0] = 1

for i in range(len(S)):

    if S[i] == '?':

        dp[i + 1][0] = (dp[i][0] * 3) % MOD

        dp[i + 1][1] = (dp[i][1] * 3 + dp[i][0]) % MOD

        dp[i + 1][2] = (dp[i][2] * 3 + dp[i][1]) % MOD

        dp[i + 1][3] = (dp[i][3] * 3 + dp[i][2]) % MOD

    else:

        for j in range(4):

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



        if S[i] == 'A':

            dp[i + 1][1] = (dp[i + 1][1] + dp[i][0]) % MOD

        if S[i] == 'B':

            dp[i + 1][2] = (dp[i + 1][2] + dp[i][1]) % MOD

        if S[i] == 'C':

            dp[i + 1][3] = (dp[i + 1][3] + dp[i][2]) % MOD



print((dp[-1][-1]))

# coding:utf-8
import sys

input = sys.stdin.readline
INF = float("inf")
MOD = 10**9 + 7


def inpl():
    return list(map(int, input().split()))


S = input().rstrip()
dp = [[0] * 4 for _ in range(len(S) + 1)]
dp[0][0] = 1
# print(' ', dp[0][:])
for i in range(len(S)):
    # ABCカウントを更新しない
    for j in range(4):
        if S[i] != "?":
            dp[i + 1][j] += dp[i][j]
        else:
            dp[i + 1][j] += dp[i][j] * 3
        dp[i + 1][j] %= MOD
    # ABCカウントを更新する
    if S[i] in ("A", "?"):
        dp[i + 1][1] += dp[i][0]
    if S[i] in ("B", "?"):
        dp[i + 1][2] += dp[i][1]
    if S[i] in ("C", "?"):
        dp[i + 1][3] += dp[i][2]
    for j in range(1, 4):
        dp[i + 1][j] %= MOD
    # print(S[i], dp[i + 1][:])
print((dp[-1][-1]))

# coding:utf-8
import sys

input = sys.stdin.readline
INF = float("inf")
MOD = 10**9 + 7


def inpl():
    return list(map(int, input().split()))


S = input().rstrip()
dp = [[0] * 4 for _ in range(len(S) + 1)]
dp[0][0] = 1
for i in range(len(S)):
    if S[i] == "?":
        dp[i + 1][0] = (dp[i][0] * 3) % MOD
        dp[i + 1][1] = (dp[i][1] * 3 + dp[i][0]) % MOD
        dp[i + 1][2] = (dp[i][2] * 3 + dp[i][1]) % MOD
        dp[i + 1][3] = (dp[i][3] * 3 + dp[i][2]) % MOD
    else:
        for j in range(4):
            dp[i + 1][j] = dp[i][j]
        if S[i] == "A":
            dp[i + 1][1] = (dp[i + 1][1] + dp[i][0]) % MOD
        if S[i] == "B":
            dp[i + 1][2] = (dp[i + 1][2] + dp[i][1]) % MOD
        if S[i] == "C":
            dp[i + 1][3] = (dp[i + 1][3] + dp[i][2]) % MOD
print((dp[-1][-1]))

-# print(' ', dp[0][:]) -    # ABCカウントを更新しない -    for j in range(4): -        if S[i] != "?": -            dp[i + 1][j] += dp[i][j] -        else: -            dp[i + 1][j] += dp[i][j] * 3 -        dp[i + 1][j] %= MOD -    # ABCカウントを更新する -    if S[i] in ("A", "?"): -        dp[i + 1][1] += dp[i][0] -    if S[i] in ("B", "?"): -        dp[i + 1][2] += dp[i][1] -    if S[i] in ("C", "?"): -        dp[i + 1][3] += dp[i][2] -    for j in range(1, 4): -        dp[i + 1][j] %= MOD -    # print(S[i], dp[i + 1][:]) +    if S[i] == "?": +        dp[i + 1][0] = (dp[i][0] * 3) % MOD +        dp[i + 1][1] = (dp[i][1] * 3 + dp[i][0]) % MOD +        dp[i + 1][2] = (dp[i][2] * 3 + dp[i][1]) % MOD +        dp[i + 1][3] = (dp[i][3] * 3 + dp[i][2]) % MOD +    else: +        for j in range(4): +            dp[i + 1][j] = dp[i][j] +        if S[i] == "A": +            dp[i + 1][1] = (dp[i + 1][1] + dp[i][0]) % MOD +        if S[i] == "B": +            dp[i + 1][2] = (dp[i + 1][2] + dp[i][1]) % MOD +        if S[i] == "C": +            dp[i + 1][3] = (dp[i + 1][3] + dp[i][2]) % MOD