S = eval(input())

N = len(S)

MOD = 10**9 + 7

mask = 'ABC'



dp = [[0,0,0,0] for _ in range(N+1)]



for i in range(N, -1, -1) :

    for j in range(4) :

        if i == N :

            if j == 3 :

                dp[i][j] = 1

            else :

                dp[i][j] = 0

        else :

            if j == 3 :

                weight = 3 if S[i] == '?' else 1

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

            else :

                weight = 3 if S[i] == '?' else 1

                able = 1 if (S[i] == mask[j] or S[i] == '?') else 0

                dp[i][j] = weight * dp[i+1][j] + able * dp[i+1][j+1]

            dp[i][j] %= MOD





print((dp[0][0] % MOD))
S = eval(input())

MOD = 10**9 + 7



A = 0

AB = 0

Q = 1

ans = 0



for s in S:

    if s == '?':

        ans = (3 * ans + AB) % MOD

        AB = (3 * AB + A) % MOD

        A = (3 * A + Q) % MOD

        Q = Q * 3 % MOD

    elif s == 'A':

        A = (A + Q) % MOD

    elif s == 'B':

        AB = (AB + A) % MOD

    elif s == 'C':

        ans = (ans + AB) % MOD



print(ans)

S = eval(input())
N = len(S)
MOD = 10**9 + 7
mask = "ABC"
dp = [[0, 0, 0, 0] for _ in range(N + 1)]
for i in range(N, -1, -1):
    for j in range(4):
        if i == N:
            if j == 3:
                dp[i][j] = 1
            else:
                dp[i][j] = 0
        else:
            if j == 3:
                weight = 3 if S[i] == "?" else 1
                dp[i][j] = weight * dp[i + 1][j]
            else:
                weight = 3 if S[i] == "?" else 1
                able = 1 if (S[i] == mask[j] or S[i] == "?") else 0
                dp[i][j] = weight * dp[i + 1][j] + able * dp[i + 1][j + 1]
            dp[i][j] %= MOD
print((dp[0][0] % MOD))

S = eval(input())
MOD = 10**9 + 7
A = 0
AB = 0
Q = 1
ans = 0
for s in S:
    if s == "?":
        ans = (3 * ans + AB) % MOD
        AB = (3 * AB + A) % MOD
        A = (3 * A + Q) % MOD
        Q = Q * 3 % MOD
    elif s == "A":
        A = (A + Q) % MOD
    elif s == "B":
        AB = (AB + A) % MOD
    elif s == "C":
        ans = (ans + AB) % MOD
print(ans)

-N = len(S) -mask = "ABC" -dp = [[0, 0, 0, 0] for _ in range(N + 1)] -for i in range(N, -1, -1): -    for j in range(4): -        if i == N: -            if j == 3: -                dp[i][j] = 1 -            else: -                dp[i][j] = 0 -        else: -            if j == 3: -                weight = 3 if S[i] == "?" else 1 -                dp[i][j] = weight * dp[i + 1][j] -            else: -                weight = 3 if S[i] == "?" else 1 -                able = 1 if (S[i] == mask[j] or S[i] == "?") else 0 -                dp[i][j] = weight * dp[i + 1][j] + able * dp[i + 1][j + 1] -            dp[i][j] %= MOD -print((dp[0][0] % MOD)) +A = 0 +AB = 0 +Q = 1 +ans = 0 +for s in S: +    if s == "?": +        ans = (3 * ans + AB) % MOD +        AB = (3 * AB + A) % MOD +        A = (3 * A + Q) % MOD +        Q = Q * 3 % MOD +    elif s == "A": +        A = (A + Q) % MOD +    elif s == "B": +        AB = (AB + A) % MOD +    elif s == "C": +        ans = (ans + AB) % MOD +print(ans)