# 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()



na, a, ab, abc = 1, 0, 0, 0

for s in S:

    if s == 'A':

        a = (a + na) % MOD

    if s == 'B':

        ab = (ab + a) % MOD

    if s == 'C':

        abc = (abc + ab) % MOD

    if s == '?':

        # abc, ab, a, naの順で計算しないと値が上書きされる

        abc = (abc * 3 + ab) % MOD

        ab = (ab * 3 + a) % MOD

        a = (a * 3 + na) % MOD

        na = (na * 3) % MOD



print(abc)

# 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()
na, a, ab, abc = 1, 0, 0, 0
for s in S:
    if s == "A":
        a = (a + na) % MOD
    if s == "B":
        ab = (ab + a) % MOD
    if s == "C":
        abc = (abc + ab) % MOD
    if s == "?":
        # abc, ab, a, naの順で計算しないと値が上書きされる
        abc = (abc * 3 + ab) % MOD
        ab = (ab * 3 + a) % MOD
        a = (a * 3 + na) % MOD
        na = (na * 3) % MOD
print(abc)

-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])) +na, a, ab, abc = 1, 0, 0, 0 +for s in S: +    if s == "A": +        a = (a + na) % MOD +    if s == "B": +        ab = (ab + a) % MOD +    if s == "C": +        abc = (abc + ab) % MOD +    if s == "?": +        # abc, ab, a, naの順で計算しないと値が上書きされる +        abc = (abc * 3 + ab) % MOD +        ab = (ab * 3 + a) % MOD +        a = (a * 3 + na) % MOD +        na = (na * 3) % MOD +print(abc)