import numpy as np

s=eval(input())

n=len(s)

MOD=1000000007

wayA=np.array([0,0,0,0])

wayB=np.array([0,0,0,0])

wayC=np.array([0,0,0,0])

hatena=0

for c in s:

	if c=="A":

		wayA[0]+=1

	if c=="B":

		wayB+=wayA

	if c=='C':

		wayC+=wayB

	if c=='?':

		wayC[1]=wayC[1]+wayB[0]

		wayC[2]=wayC[2]+wayB[1]

		wayC[3]=wayC[3]+wayB[2]

		wayB[1]=wayB[1]+wayA[0]

		wayB[2]=wayB[2]+wayA[1]

		wayA[1]=wayA[1]+1

		hatena+=1

ans=0

for i in range(0,4):

	ans+=pow(3,hatena-i)*wayC[i]

print((int(ans%MOD)))
import numpy as np

s=eval(input())

wayA=np.array([0,0,0,0])

wayB=np.array([0,0,0,0])

wayC=np.array([0,0,0,0])

hatena=0

for c in s:

	if c=="A":

		wayA[0]+=1

	if c=="B":

		wayB+=wayA

	if c=='C':

		wayC+=wayB

	if c=='?':

		wayC[1]+=wayB[0]

		wayC[2]+=wayB[1]

		wayC[3]+=wayB[2]

		wayB[1]+=wayA[0]

		wayB[2]+=wayA[1]

		wayA[1]+=1

		hatena+=1

ans=0

for i in range(4):

	ans+=pow(3,hatena-i)*wayC[i]

print((int(ans%1000000007)))
import numpy as np

s = eval(input())
n = len(s)
MOD = 1000000007
wayA = np.array([0, 0, 0, 0])
wayB = np.array([0, 0, 0, 0])
wayC = np.array([0, 0, 0, 0])
hatena = 0
for c in s:
    if c == "A":
        wayA[0] += 1
    if c == "B":
        wayB += wayA
    if c == "C":
        wayC += wayB
    if c == "?":
        wayC[1] = wayC[1] + wayB[0]
        wayC[2] = wayC[2] + wayB[1]
        wayC[3] = wayC[3] + wayB[2]
        wayB[1] = wayB[1] + wayA[0]
        wayB[2] = wayB[2] + wayA[1]
        wayA[1] = wayA[1] + 1
        hatena += 1
ans = 0
for i in range(0, 4):
    ans += pow(3, hatena - i) * wayC[i]
print((int(ans % MOD)))

import numpy as np

s = eval(input())
wayA = np.array([0, 0, 0, 0])
wayB = np.array([0, 0, 0, 0])
wayC = np.array([0, 0, 0, 0])
hatena = 0
for c in s:
    if c == "A":
        wayA[0] += 1
    if c == "B":
        wayB += wayA
    if c == "C":
        wayC += wayB
    if c == "?":
        wayC[1] += wayB[0]
        wayC[2] += wayB[1]
        wayC[3] += wayB[2]
        wayB[1] += wayA[0]
        wayB[2] += wayA[1]
        wayA[1] += 1
        hatena += 1
ans = 0
for i in range(4):
    ans += pow(3, hatena - i) * wayC[i]
print((int(ans % 1000000007)))

-n = len(s) -MOD = 1000000007 -        wayC[1] = wayC[1] + wayB[0] -        wayC[2] = wayC[2] + wayB[1] -        wayC[3] = wayC[3] + wayB[2] -        wayB[1] = wayB[1] + wayA[0] -        wayB[2] = wayB[2] + wayA[1] -        wayA[1] = wayA[1] + 1 +        wayC[1] += wayB[0] +        wayC[2] += wayB[1] +        wayC[3] += wayB[2] +        wayB[1] += wayA[0] +        wayB[2] += wayA[1] +        wayA[1] += 1 -for i in range(0, 4): +for i in range(4): -print((int(ans % MOD))) +print((int(ans % 1000000007)))