M = 10**9 + 7

N, A, AB, ABC = 1, 0, 0, 0

idx = {'A': 1, 'B': 2, 'C': 3}

for c in eval(input()):

    if c == '?':

        N, A, AB, ABC = (N * 3) % M, (A * 3 + N) % M, (AB * 3 + A) % M, (ABC * 3 + AB) % M

    else:

        dp = [N, A, AB, ABC]

        dp[idx[c]] += dp[idx[c] - 1]

        N, A, AB, ABC = dp

print((ABC % M))
M = 10**9 + 7

N, A, AB, ABC = 1, 0, 0, 0

idx = {'A': 1, 'B': 2, 'C': 3}

for c in eval(input()):

    if c == '?':

        N, A, AB, ABC = (N * 3) % M, (A * 3 + N) % M, (AB * 3 + A) % M, (ABC * 3 + AB) % M

    elif c == 'A':

        A += N

    elif c == 'B':

        AB += A

    else:

        ABC += AB

print((ABC % M))
M = 10**9 + 7
N, A, AB, ABC = 1, 0, 0, 0
idx = {"A": 1, "B": 2, "C": 3}
for c in eval(input()):
    if c == "?":
        N, A, AB, ABC = (
            (N * 3) % M,
            (A * 3 + N) % M,
            (AB * 3 + A) % M,
            (ABC * 3 + AB) % M,
        )
    else:
        dp = [N, A, AB, ABC]
        dp[idx[c]] += dp[idx[c] - 1]
        N, A, AB, ABC = dp
print((ABC % M))

M = 10**9 + 7
N, A, AB, ABC = 1, 0, 0, 0
idx = {"A": 1, "B": 2, "C": 3}
for c in eval(input()):
    if c == "?":
        N, A, AB, ABC = (
            (N * 3) % M,
            (A * 3 + N) % M,
            (AB * 3 + A) % M,
            (ABC * 3 + AB) % M,
        )
    elif c == "A":
        A += N
    elif c == "B":
        AB += A
    else:
        ABC += AB
print((ABC % M))

+    elif c == "A": +        A += N +    elif c == "B": +        AB += A -        dp = [N, A, AB, ABC] -        dp[idx[c]] += dp[idx[c] - 1] -        N, A, AB, ABC = dp +        ABC += AB