MOD = pow(10, 9) + 7

s = eval(input())

n = len(s)

dp = [[0 for _ in range(4)] for _ in range(n + 1)]



dp[0][0] = 1

for i in range(n):

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

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

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

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

    if s[i] == 'A':

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

    elif s[i] == 'B':

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

    elif s[i] == 'C':

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

    else:

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

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

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

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

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

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

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

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

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

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

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



print((dp[n][3]))

s = input().rstrip()

n = len(s)

mod = 10**9 + 7

dp = [0] * 4

dp[0] = 1



for i in range(n):

    next = [0] * 4

    if s[i] == 'A':

        next[1] += dp[0]

    elif s[i] == 'B':

        next[2] += dp[1]

    elif s[i] == 'C':

        next[3] += dp[2]

    else:

        next[1] += dp[0]

        next[2] += dp[1]

        next[3] += dp[2]

        next[0] += dp[0] * 2

        next[1] += dp[1] * 2

        next[2] += dp[2] * 2

        next[3] += dp[3] * 2

    next[0] += dp[0]

    next[1] += dp[1]

    next[2] += dp[2]

    next[3] += dp[3]



    for j in range(4):

        next[j] %= mod

    dp = next

print((dp[3]))

MOD = pow(10, 9) + 7
s = eval(input())
n = len(s)
dp = [[0 for _ in range(4)] for _ in range(n + 1)]
dp[0][0] = 1
for i in range(n):
    dp[i + 1][0] = dp[i][0]
    dp[i + 1][1] = dp[i][1]
    dp[i + 1][2] = dp[i][2]
    dp[i + 1][3] = dp[i][3]
    if s[i] == "A":
        dp[i + 1][1] += dp[i][0]
    elif s[i] == "B":
        dp[i + 1][2] += dp[i][1]
    elif s[i] == "C":
        dp[i + 1][3] += dp[i][2]
    else:
        dp[i + 1][0] = dp[i][0] * 3
        dp[i + 1][1] = dp[i][1] * 3
        dp[i + 1][2] = dp[i][2] * 3
        dp[i + 1][3] = dp[i][3] * 3
        dp[i + 1][1] += dp[i][0]
        dp[i + 1][2] += dp[i][1]
        dp[i + 1][3] += dp[i][2]
    dp[i + 1][0] %= MOD
    dp[i + 1][1] %= MOD
    dp[i + 1][2] %= MOD
    dp[i + 1][3] %= MOD
print((dp[n][3]))

s = input().rstrip()
n = len(s)
mod = 10**9 + 7
dp = [0] * 4
dp[0] = 1
for i in range(n):
    next = [0] * 4
    if s[i] == "A":
        next[1] += dp[0]
    elif s[i] == "B":
        next[2] += dp[1]
    elif s[i] == "C":
        next[3] += dp[2]
    else:
        next[1] += dp[0]
        next[2] += dp[1]
        next[3] += dp[2]
        next[0] += dp[0] * 2
        next[1] += dp[1] * 2
        next[2] += dp[2] * 2
        next[3] += dp[3] * 2
    next[0] += dp[0]
    next[1] += dp[1]
    next[2] += dp[2]
    next[3] += dp[3]
    for j in range(4):
        next[j] %= mod
    dp = next
print((dp[3]))

-MOD = pow(10, 9) + 7 -s = eval(input()) +s = input().rstrip() -dp = [[0 for _ in range(4)] for _ in range(n + 1)] -dp[0][0] = 1 +mod = 10**9 + 7 +dp = [0] * 4 +dp[0] = 1 -    dp[i + 1][0] = dp[i][0] -    dp[i + 1][1] = dp[i][1] -    dp[i + 1][2] = dp[i][2] -    dp[i + 1][3] = dp[i][3] +    next = [0] * 4 -        dp[i + 1][1] += dp[i][0] +        next[1] += dp[0] -        dp[i + 1][2] += dp[i][1] +        next[2] += dp[1] -        dp[i + 1][3] += dp[i][2] +        next[3] += dp[2] -        dp[i + 1][0] = dp[i][0] * 3 -        dp[i + 1][1] = dp[i][1] * 3 -        dp[i + 1][2] = dp[i][2] * 3 -        dp[i + 1][3] = dp[i][3] * 3 -        dp[i + 1][1] += dp[i][0] -        dp[i + 1][2] += dp[i][1] -        dp[i + 1][3] += dp[i][2] -    dp[i + 1][0] %= MOD -    dp[i + 1][1] %= MOD -    dp[i + 1][2] %= MOD -    dp[i + 1][3] %= MOD -print((dp[n][3])) +        next[1] += dp[0] +        next[2] += dp[1] +        next[3] += dp[2] +        next[0] += dp[0] * 2 +        next[1] += dp[1] * 2 +        next[2] += dp[2] * 2 +        next[3] += dp[3] * 2 +    next[0] += dp[0] +    next[1] += dp[1] +    next[2] += dp[2] +    next[3] += dp[3] +    for j in range(4): +        next[j] %= mod +    dp = next +print((dp[3]))