M = 10**9 + 7

dp = [0] * 4

dp[0] = 1

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

m = lambda x: x % M if x > M else x

for c in eval(input()):

    if c == '?':

        N, A, AB, ABC = dp

        dp = [m(N * 3), m(A * 3 + N), m(AB * 3 + A), m(ABC * 3 + AB)]

    else:

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

print((dp[3] % 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
dp = [0] * 4
dp[0] = 1
idx = {"A": 1, "B": 2, "C": 3}
m = lambda x: x % M if x > M else x
for c in eval(input()):
    if c == "?":
        N, A, AB, ABC = dp
        dp = [m(N * 3), m(A * 3 + N), m(AB * 3 + A), m(ABC * 3 + AB)]
    else:
        dp[idx[c]] += dp[idx[c] - 1]
print((dp[3] % 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))

-dp = [0] * 4 -dp[0] = 1 +N, A, AB, ABC = 1, 0, 0, 0 -m = lambda x: x % M if x > M else x +        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] -        dp = [m(N * 3), m(A * 3 + N), m(AB * 3 + A), m(ABC * 3 + AB)] -    else: -        dp[idx[c]] += dp[idx[c] - 1] -print((dp[3] % M)) +print((ABC % M))