import numpy as np

s=eval(input())

n=len(s)

MOD=1000000007

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

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

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

MOD2=np.array([MOD,MOD,MOD,MOD],dtype=np.int32)

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

	wayA%=MOD2

	wayB%=MOD2

	wayC%=MOD2

ans=0

for i in range(0,4):

	p3=1

	for k in range(0,hatena-i):

		p3=p3*3

		p3=p3%MOD

	ans=ans+p3*wayC[i]

	ans=ans%MOD

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

s=eval(input())

n=len(s)

MOD=1000000007

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

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

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

MOD2=np.array([MOD,MOD,MOD,MOD],dtype=np.int32)

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

	wayA%=MOD2

	wayB%=MOD2

	wayC%=MOD2

ans=0

for i in range(0,4):

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

	ans=ans%MOD

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

s = eval(input())
n = len(s)
MOD = 1000000007
wayA = np.array([0, 0, 0, 0], dtype=np.int64)
wayB = np.array([0, 0, 0, 0], dtype=np.int64)
wayC = np.array([0, 0, 0, 0], dtype=np.int64)
MOD2 = np.array([MOD, MOD, MOD, MOD], dtype=np.int32)
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
    wayA %= MOD2
    wayB %= MOD2
    wayC %= MOD2
ans = 0
for i in range(0, 4):
    p3 = 1
    for k in range(0, hatena - i):
        p3 = p3 * 3
        p3 = p3 % MOD
    ans = ans + p3 * wayC[i]
    ans = ans % MOD
print((int(ans)))

import numpy as np

s = eval(input())
n = len(s)
MOD = 1000000007
wayA = np.array([0, 0, 0, 0], dtype=np.int64)
wayB = np.array([0, 0, 0, 0], dtype=np.int64)
wayC = np.array([0, 0, 0, 0], dtype=np.int64)
MOD2 = np.array([MOD, MOD, MOD, MOD], dtype=np.int32)
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
    wayA %= MOD2
    wayB %= MOD2
    wayC %= MOD2
ans = 0
for i in range(0, 4):
    ans = ans + pow(3, hatena - i) * wayC[i]
    ans = ans % MOD
print((int(ans)))

-    p3 = 1 -    for k in range(0, hatena - i): -        p3 = p3 * 3 -        p3 = p3 % MOD -    ans = ans + p3 * wayC[i] +    ans = ans + pow(3, hatena - i) * wayC[i]