MOD = 10 ** 9 + 7

dp = [0, 0]

ans = 0

t = 1

for c in eval(input()):

    ndp = dp.copy()

    if c in 'A':

        ndp[0] = (dp[0] + t) % MOD

    elif c in 'B':

        ndp[1] = (ndp[1] + dp[0]) % MOD

    elif c in 'C':

        ans = (ans + dp[1]) % MOD

    else:

        ndp[0] = (3 * dp[0] + t) % MOD

        ndp[1] = (3 * dp[1] + dp[0]) % MOD

        ans = (3 * ans + dp[1]) % MOD

        t = (t * 3) % MOD

    #print(*ndp, ans)

    dp = ndp

print(ans)
MOD = 10 ** 9 + 7

s = eval(input())

a = 0

ql = 0

c = s.count('C')

qr = s.count('?')

ans = 0 

for si in s:

    if si == '?':

        qr -= 1

    if si in 'B?':

        ans = (ans + pow(3,ql+qr,MOD)*a*c 

             + (pow(3,ql+qr-1,MOD)*(ql*c+a*qr) if ql + qr - 1 >= 0 else 0)

             + (pow(3,ql+qr-2,MOD)*ql*qr if ql + qr - 2 >= 0 else 0)) % MOD

    if si == '?':

        ql += 1

    elif si == 'A':

        a += 1

    elif si == 'C':

        c -= 1

print(ans)
MOD = 10**9 + 7
dp = [0, 0]
ans = 0
t = 1
for c in eval(input()):
    ndp = dp.copy()
    if c in "A":
        ndp[0] = (dp[0] + t) % MOD
    elif c in "B":
        ndp[1] = (ndp[1] + dp[0]) % MOD
    elif c in "C":
        ans = (ans + dp[1]) % MOD
    else:
        ndp[0] = (3 * dp[0] + t) % MOD
        ndp[1] = (3 * dp[1] + dp[0]) % MOD
        ans = (3 * ans + dp[1]) % MOD
        t = (t * 3) % MOD
    # print(*ndp, ans)
    dp = ndp
print(ans)

MOD = 10**9 + 7
s = eval(input())
a = 0
ql = 0
c = s.count("C")
qr = s.count("?")
ans = 0
for si in s:
    if si == "?":
        qr -= 1
    if si in "B?":
        ans = (
            ans
            + pow(3, ql + qr, MOD) * a * c
            + (pow(3, ql + qr - 1, MOD) * (ql * c + a * qr) if ql + qr - 1 >= 0 else 0)
            + (pow(3, ql + qr - 2, MOD) * ql * qr if ql + qr - 2 >= 0 else 0)
        ) % MOD
    if si == "?":
        ql += 1
    elif si == "A":
        a += 1
    elif si == "C":
        c -= 1
print(ans)

-dp = [0, 0] +s = eval(input()) +a = 0 +ql = 0 +c = s.count("C") +qr = s.count("?") -t = 1 -for c in eval(input()): -    ndp = dp.copy() -    if c in "A": -        ndp[0] = (dp[0] + t) % MOD -    elif c in "B": -        ndp[1] = (ndp[1] + dp[0]) % MOD -    elif c in "C": -        ans = (ans + dp[1]) % MOD -    else: -        ndp[0] = (3 * dp[0] + t) % MOD -        ndp[1] = (3 * dp[1] + dp[0]) % MOD -        ans = (3 * ans + dp[1]) % MOD -        t = (t * 3) % MOD -    # print(*ndp, ans) -    dp = ndp +for si in s: +    if si == "?": +        qr -= 1 +    if si in "B?": +        ans = ( +            ans +            + pow(3, ql + qr, MOD) * a * c +            + (pow(3, ql + qr - 1, MOD) * (ql * c + a * qr) if ql + qr - 1 >= 0 else 0) +            + (pow(3, ql + qr - 2, MOD) * ql * qr if ql + qr - 2 >= 0 else 0) +        ) % MOD +    if si == "?": +        ql += 1 +    elif si == "A": +        a += 1 +    elif si == "C": +        c -= 1