Mod = 10**9+7

S = eval(input())

N = len(S)

O = 1

A = [0]*N

AB = [0]*N

ABC = [0]*N



for i in range(N):

    A[i] = A[i-1]

    AB[i] = AB[i-1]

    ABC[i] = ABC[i-1]

    if S[i] == 'A':

        A[i] += O

    if S[i] == 'B':

        AB[i] += A[i-1]

    if S[i] == 'C':

        ABC[i] += AB[i-1]

    if S[i] == '?':

        A[i] *= 3

        A[i] += O

        AB[i] *= 3

        AB[i] += A[i-1]

        ABC[i] *= 3

        ABC[i] += AB[i-1]

        O *= 3

    A[i] %= Mod

    AB[i] %= Mod

    ABC[i] %= Mod

    O %= Mod

print((ABC[-1]))
mod = 10**9+7

S = eval(input())

A = 0

AB = 0

ABC = 0



N = 1

for i in range(len(S)):

    if S[i] == 'A':

        A += N

    elif S[i] == 'B':

        AB += A

    elif S[i] == 'C':

        ABC += AB

    else:

        ABC = 3*ABC + AB

        AB = 3*AB + A 

        A = 3*A + N

        N *= 3

    A %= mod

    AB %= mod

    ABC %= mod

    N %= mod



print(ABC)
Mod = 10**9 + 7
S = eval(input())
N = len(S)
O = 1
A = [0] * N
AB = [0] * N
ABC = [0] * N
for i in range(N):
    A[i] = A[i - 1]
    AB[i] = AB[i - 1]
    ABC[i] = ABC[i - 1]
    if S[i] == "A":
        A[i] += O
    if S[i] == "B":
        AB[i] += A[i - 1]
    if S[i] == "C":
        ABC[i] += AB[i - 1]
    if S[i] == "?":
        A[i] *= 3
        A[i] += O
        AB[i] *= 3
        AB[i] += A[i - 1]
        ABC[i] *= 3
        ABC[i] += AB[i - 1]
        O *= 3
    A[i] %= Mod
    AB[i] %= Mod
    ABC[i] %= Mod
    O %= Mod
print((ABC[-1]))

mod = 10**9 + 7
S = eval(input())
A = 0
AB = 0
ABC = 0
N = 1
for i in range(len(S)):
    if S[i] == "A":
        A += N
    elif S[i] == "B":
        AB += A
    elif S[i] == "C":
        ABC += AB
    else:
        ABC = 3 * ABC + AB
        AB = 3 * AB + A
        A = 3 * A + N
        N *= 3
    A %= mod
    AB %= mod
    ABC %= mod
    N %= mod
print(ABC)

-Mod = 10**9 + 7 +mod = 10**9 + 7 -N = len(S) -O = 1 -A = [0] * N -AB = [0] * N -ABC = [0] * N -for i in range(N): -    A[i] = A[i - 1] -    AB[i] = AB[i - 1] -    ABC[i] = ABC[i - 1] +A = 0 +AB = 0 +ABC = 0 +N = 1 +for i in range(len(S)): -        A[i] += O -    if S[i] == "B": -        AB[i] += A[i - 1] -    if S[i] == "C": -        ABC[i] += AB[i - 1] -    if S[i] == "?": -        A[i] *= 3 -        A[i] += O -        AB[i] *= 3 -        AB[i] += A[i - 1] -        ABC[i] *= 3 -        ABC[i] += AB[i - 1] -        O *= 3 -    A[i] %= Mod -    AB[i] %= Mod -    ABC[i] %= Mod -    O %= Mod -print((ABC[-1])) +        A += N +    elif S[i] == "B": +        AB += A +    elif S[i] == "C": +        ABC += AB +    else: +        ABC = 3 * ABC + AB +        AB = 3 * AB + A +        A = 3 * A + N +        N *= 3 +    A %= mod +    AB %= mod +    ABC %= mod +    N %= mod +print(ABC)