#we love abc

s=eval(input())

mod=10**9+7

dpa=[[0,0] for i in range(len(s)+1)]

dpc=[[0,0] for i in range(len(s)+1)]

power3=[1 for i in range(10**5+100)]

for i in range(10**5+3):

    power3[i+1]=(power3[i]*3)%mod

for i in range(len(s)):

    if s[i]=="A":

        dpa[i+1][0]=(dpa[i][0]+power3[dpa[i][1]])%mod

        dpa[i+1][1]=dpa[i][1]  

    elif s[i]=="?":

        dpa[i+1][0]=(3*dpa[i][0]+power3[dpa[i][1]])%mod

        dpa[i+1][1]=dpa[i][1]+1   

    else:

        dpa[i+1]=dpa[i]

for i in range(len(s)):

    if s[len(s)-1-i]=="C":

        dpc[i+1][0]=(dpc[i][0]+power3[dpc[i][1]])%mod

        dpc[i+1][1]=dpc[i][1]  

    elif s[len(s)-1-i]=="?":

        dpc[i+1][0]=(3*dpc[i][0]+power3[dpc[i][1]])%mod

        dpc[i+1][1]=dpc[i][1]+1   

    else:

        dpc[i+1]=dpc[i]    

ans=0

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

    if s[i]=="B" or s[i]=="?":

        ans+=dpa[i][0]*dpc[len(s)-1-i][0]

        ans=ans%mod

print(ans)
S = list(eval(input()))

n = len(S)

mod = 10**9 + 7



dpa = [0]*(10**5+10)

dpc = [0]*(10**5+10)



hatena_count = 0

pow3 = [1]*(10**5+10)



for i in range(1, 10**5+10):

    pow3[i] = pow3[i-1]*3 % mod



for i in range(n):

    if S[i] == "A":

        dpa[i+1] = (dpa[i] + pow3[hatena_count]) % mod

    elif S[i] == "?":

        dpa[i+1] = (3*dpa[i] + pow3[hatena_count]) % mod

        hatena_count += 1

    else:

        dpa[i+1] = dpa[i]



S.reverse()



hatena_count = 0

for i in range(n):

    if S[i] == "C":

        dpc[i+1] = (dpc[i] + pow3[hatena_count]) % mod

    elif S[i] == "?":

        dpc[i+1] = (3*dpc[i] + pow3[hatena_count]) % mod

        hatena_count += 1

    else:

        dpc[i+1] = dpc[i]



ans = 0

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

    if S[i] == "B" or S[i] == "?":

        ans += dpa[n-1-i]*dpc[i]

        ans %= mod

print(ans)





# we love abc
s = eval(input())
mod = 10**9 + 7
dpa = [[0, 0] for i in range(len(s) + 1)]
dpc = [[0, 0] for i in range(len(s) + 1)]
power3 = [1 for i in range(10**5 + 100)]
for i in range(10**5 + 3):
    power3[i + 1] = (power3[i] * 3) % mod
for i in range(len(s)):
    if s[i] == "A":
        dpa[i + 1][0] = (dpa[i][0] + power3[dpa[i][1]]) % mod
        dpa[i + 1][1] = dpa[i][1]
    elif s[i] == "?":
        dpa[i + 1][0] = (3 * dpa[i][0] + power3[dpa[i][1]]) % mod
        dpa[i + 1][1] = dpa[i][1] + 1
    else:
        dpa[i + 1] = dpa[i]
for i in range(len(s)):
    if s[len(s) - 1 - i] == "C":
        dpc[i + 1][0] = (dpc[i][0] + power3[dpc[i][1]]) % mod
        dpc[i + 1][1] = dpc[i][1]
    elif s[len(s) - 1 - i] == "?":
        dpc[i + 1][0] = (3 * dpc[i][0] + power3[dpc[i][1]]) % mod
        dpc[i + 1][1] = dpc[i][1] + 1
    else:
        dpc[i + 1] = dpc[i]
ans = 0
for i in range(1, len(s) - 1):
    if s[i] == "B" or s[i] == "?":
        ans += dpa[i][0] * dpc[len(s) - 1 - i][0]
        ans = ans % mod
print(ans)

S = list(eval(input()))
n = len(S)
mod = 10**9 + 7
dpa = [0] * (10**5 + 10)
dpc = [0] * (10**5 + 10)
hatena_count = 0
pow3 = [1] * (10**5 + 10)
for i in range(1, 10**5 + 10):
    pow3[i] = pow3[i - 1] * 3 % mod
for i in range(n):
    if S[i] == "A":
        dpa[i + 1] = (dpa[i] + pow3[hatena_count]) % mod
    elif S[i] == "?":
        dpa[i + 1] = (3 * dpa[i] + pow3[hatena_count]) % mod
        hatena_count += 1
    else:
        dpa[i + 1] = dpa[i]
S.reverse()
hatena_count = 0
for i in range(n):
    if S[i] == "C":
        dpc[i + 1] = (dpc[i] + pow3[hatena_count]) % mod
    elif S[i] == "?":
        dpc[i + 1] = (3 * dpc[i] + pow3[hatena_count]) % mod
        hatena_count += 1
    else:
        dpc[i + 1] = dpc[i]
ans = 0
for i in range(1, n - 1):
    if S[i] == "B" or S[i] == "?":
        ans += dpa[n - 1 - i] * dpc[i]
        ans %= mod
print(ans)

-# we love abc -s = eval(input()) +S = list(eval(input())) +n = len(S) -dpa = [[0, 0] for i in range(len(s) + 1)] -dpc = [[0, 0] for i in range(len(s) + 1)] -power3 = [1 for i in range(10**5 + 100)] -for i in range(10**5 + 3): -    power3[i + 1] = (power3[i] * 3) % mod -for i in range(len(s)): -    if s[i] == "A": -        dpa[i + 1][0] = (dpa[i][0] + power3[dpa[i][1]]) % mod -        dpa[i + 1][1] = dpa[i][1] -    elif s[i] == "?": -        dpa[i + 1][0] = (3 * dpa[i][0] + power3[dpa[i][1]]) % mod -        dpa[i + 1][1] = dpa[i][1] + 1 +dpa = [0] * (10**5 + 10) +dpc = [0] * (10**5 + 10) +hatena_count = 0 +pow3 = [1] * (10**5 + 10) +for i in range(1, 10**5 + 10): +    pow3[i] = pow3[i - 1] * 3 % mod +for i in range(n): +    if S[i] == "A": +        dpa[i + 1] = (dpa[i] + pow3[hatena_count]) % mod +    elif S[i] == "?": +        dpa[i + 1] = (3 * dpa[i] + pow3[hatena_count]) % mod +        hatena_count += 1 -for i in range(len(s)): -    if s[len(s) - 1 - i] == "C": -        dpc[i + 1][0] = (dpc[i][0] + power3[dpc[i][1]]) % mod -        dpc[i + 1][1] = dpc[i][1] -    elif s[len(s) - 1 - i] == "?": -        dpc[i + 1][0] = (3 * dpc[i][0] + power3[dpc[i][1]]) % mod -        dpc[i + 1][1] = dpc[i][1] + 1 +S.reverse() +hatena_count = 0 +for i in range(n): +    if S[i] == "C": +        dpc[i + 1] = (dpc[i] + pow3[hatena_count]) % mod +    elif S[i] == "?": +        dpc[i + 1] = (3 * dpc[i] + pow3[hatena_count]) % mod +        hatena_count += 1 -for i in range(1, len(s) - 1): -    if s[i] == "B" or s[i] == "?": -        ans += dpa[i][0] * dpc[len(s) - 1 - i][0] -        ans = ans % mod +for i in range(1, n - 1): +    if S[i] == "B" or S[i] == "?": +        ans += dpa[n - 1 - i] * dpc[i] +        ans %= mod