S = eval(input())



ZABC = [[0 for i in range(4)] for j in range(len(S))]

if S[0] == '?':  

    ZABC[0][0] = 3

else:

    ZABC[0][0] = 1

if S[0] == '?' or S[0] == 'A':  

    ZABC[0][1] = 1

else:

    ZABC[0][1] = 0

for i in range(2):

    ZABC[0][i+2] = 0



for i in range(1,len(S)):

    if S[i] == '?':

        q = 3

    else:

        q = 1

    ZABC[i][0] = (ZABC[i-1][0]*q)%1000000007

for i in range(1,len(S)):

    if S[i] == '?':

        q = 3

    else:

        q = 1

    if S[i] == '?' or S[i] == 'A':

        r = 1

    else:

        r = 0

    ZABC[i][1] = (ZABC[i-1][1]*q+ZABC[i-1][0]*r)%1000000007

for i in range(1,len(S)):

    if S[i] == '?':

        q = 3

    else:

        q = 1

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

        r = 1

    else:

        r = 0

    ZABC[i][2] = (ZABC[i-1][2]*q+ZABC[i-1][1]*r)%1000000007

for i in range(1,len(S)):

    if S[i] == '?':

        q = 3

    else:

        q = 1

    if S[i] == '?' or S[i] == 'C':

        r = 1

    else:

        r = 0

    ZABC[i][3] = (ZABC[i-1][3]*q+ZABC[i-1][2]*r)%1000000007

print((ZABC[len(S)-1][3]))
S = input().strip()

mod = 10**9+7

dp = [1] + [0]*3

for s in S:

    dp1 = dp[:]

    if s == 'A':

        dp1[1] += dp[0]

    elif s == 'B':

        dp1[2] += dp[1]

    elif s == 'C':

        dp1[3] += dp[2]

    else:

        dp1[1] += dp[0]

        dp1[2] += dp[1]

        dp1[3] += dp[2]

        dp1[0] += 2*dp[0]

        dp1[1] += 2*dp[1]

        dp1[2] += 2*dp[2]

        dp1[3] += 2*dp[3]

    dp = [d % mod for d in dp1]

print((dp[-1]))
S = eval(input())
ZABC = [[0 for i in range(4)] for j in range(len(S))]
if S[0] == "?":
    ZABC[0][0] = 3
else:
    ZABC[0][0] = 1
if S[0] == "?" or S[0] == "A":
    ZABC[0][1] = 1
else:
    ZABC[0][1] = 0
for i in range(2):
    ZABC[0][i + 2] = 0
for i in range(1, len(S)):
    if S[i] == "?":
        q = 3
    else:
        q = 1
    ZABC[i][0] = (ZABC[i - 1][0] * q) % 1000000007
for i in range(1, len(S)):
    if S[i] == "?":
        q = 3
    else:
        q = 1
    if S[i] == "?" or S[i] == "A":
        r = 1
    else:
        r = 0
    ZABC[i][1] = (ZABC[i - 1][1] * q + ZABC[i - 1][0] * r) % 1000000007
for i in range(1, len(S)):
    if S[i] == "?":
        q = 3
    else:
        q = 1
    if S[i] == "?" or S[i] == "B":
        r = 1
    else:
        r = 0
    ZABC[i][2] = (ZABC[i - 1][2] * q + ZABC[i - 1][1] * r) % 1000000007
for i in range(1, len(S)):
    if S[i] == "?":
        q = 3
    else:
        q = 1
    if S[i] == "?" or S[i] == "C":
        r = 1
    else:
        r = 0
    ZABC[i][3] = (ZABC[i - 1][3] * q + ZABC[i - 1][2] * r) % 1000000007
print((ZABC[len(S) - 1][3]))

S = input().strip()
mod = 10**9 + 7
dp = [1] + [0] * 3
for s in S:
    dp1 = dp[:]
    if s == "A":
        dp1[1] += dp[0]
    elif s == "B":
        dp1[2] += dp[1]
    elif s == "C":
        dp1[3] += dp[2]
    else:
        dp1[1] += dp[0]
        dp1[2] += dp[1]
        dp1[3] += dp[2]
        dp1[0] += 2 * dp[0]
        dp1[1] += 2 * dp[1]
        dp1[2] += 2 * dp[2]
        dp1[3] += 2 * dp[3]
    dp = [d % mod for d in dp1]
print((dp[-1]))

-S = eval(input()) -ZABC = [[0 for i in range(4)] for j in range(len(S))] -if S[0] == "?": -    ZABC[0][0] = 3 -else: -    ZABC[0][0] = 1 -if S[0] == "?" or S[0] == "A": -    ZABC[0][1] = 1 -else: -    ZABC[0][1] = 0 -for i in range(2): -    ZABC[0][i + 2] = 0 -for i in range(1, len(S)): -    if S[i] == "?": -        q = 3 +S = input().strip() +mod = 10**9 + 7 +dp = [1] + [0] * 3 +for s in S: +    dp1 = dp[:] +    if s == "A": +        dp1[1] += dp[0] +    elif s == "B": +        dp1[2] += dp[1] +    elif s == "C": +        dp1[3] += dp[2] -        q = 1 -    ZABC[i][0] = (ZABC[i - 1][0] * q) % 1000000007 -for i in range(1, len(S)): -    if S[i] == "?": -        q = 3 -    else: -        q = 1 -    if S[i] == "?" or S[i] == "A": -        r = 1 -    else: -        r = 0 -    ZABC[i][1] = (ZABC[i - 1][1] * q + ZABC[i - 1][0] * r) % 1000000007 -for i in range(1, len(S)): -    if S[i] == "?": -        q = 3 -    else: -        q = 1 -    if S[i] == "?" or S[i] == "B": -        r = 1 -    else: -        r = 0 -    ZABC[i][2] = (ZABC[i - 1][2] * q + ZABC[i - 1][1] * r) % 1000000007 -for i in range(1, len(S)): -    if S[i] == "?": -        q = 3 -    else: -        q = 1 -    if S[i] == "?" or S[i] == "C": -        r = 1 -    else: -        r = 0 -    ZABC[i][3] = (ZABC[i - 1][3] * q + ZABC[i - 1][2] * r) % 1000000007 -print((ZABC[len(S) - 1][3])) +        dp1[1] += dp[0] +        dp1[2] += dp[1] +        dp1[3] += dp[2] +        dp1[0] += 2 * dp[0] +        dp1[1] += 2 * dp[1] +        dp1[2] += 2 * dp[2] +        dp1[3] += 2 * dp[3] +    dp = [d % mod for d in dp1] +print((dp[-1]))