S = eval(input())

N = len(S)

mod = 10 ** 9 + 7



# dp[i][j]: i番目までの文字までで、状態jになる組み合わせ数

dp = [[0 for _ in range(4)] for _ in range(N + 1)]

dp[0][0] = 1



for i in range(N):

    if S[i] in ['A', '?']:

        for j in range(4):

            dp[i + 1][j] += dp[i][j]

        dp[i + 1][1] += dp[i][0]

    if S[i] in ['B', '?']:

        for j in range(4):

            dp[i + 1][j] += dp[i][j]

        dp[i + 1][2] += dp[i][1]

    if S[i] in ['C', '?']:

        for j in range(4):

            dp[i + 1][j] += dp[i][j]

        dp[i + 1][3] += dp[i][2]



    for j in range(4):

        dp[i + 1][j] %= mod



print((dp[N][3]))
S = eval(input())



N = len(S)

mod = 10 ** 9 + 7



dp = [[0 for _ in range(4)] for _ in range(N + 1)]

dp [0][0] = 1

for i in range(N):

    if S[i] == 'A':

        for j in range(4):

            dp[i + 1][j] += dp[i][j]

        dp[i + 1][1] += dp[i][0]

    elif S[i] == 'B':

        for j in range(4):

            dp[i + 1][j] += dp[i][j]

        dp[i + 1][2] += dp[i][1]

    elif S[i] == 'C':

        for j in range(4):

            dp[i + 1][j] += dp[i][j]

        dp[i + 1][3] += dp[i][2]

    else:

        for j in range(4):

            dp[i + 1][j] += dp[i][j] * 3

        dp[i + 1][1] += dp[i][0]

        dp[i + 1][2] += dp[i][1]

        dp[i + 1][3] += dp[i][2]



    for j in range(4):

        dp[i + 1][j] %= mod



print((dp[N][3]))
S = eval(input())
N = len(S)
mod = 10**9 + 7
# dp[i][j]: i番目までの文字までで、状態jになる組み合わせ数
dp = [[0 for _ in range(4)] for _ in range(N + 1)]
dp[0][0] = 1
for i in range(N):
    if S[i] in ["A", "?"]:
        for j in range(4):
            dp[i + 1][j] += dp[i][j]
        dp[i + 1][1] += dp[i][0]
    if S[i] in ["B", "?"]:
        for j in range(4):
            dp[i + 1][j] += dp[i][j]
        dp[i + 1][2] += dp[i][1]
    if S[i] in ["C", "?"]:
        for j in range(4):
            dp[i + 1][j] += dp[i][j]
        dp[i + 1][3] += dp[i][2]
    for j in range(4):
        dp[i + 1][j] %= mod
print((dp[N][3]))

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

-# dp[i][j]: i番目までの文字までで、状態jになる組み合わせ数 -    if S[i] in ["A", "?"]: +    if S[i] == "A": -    if S[i] in ["B", "?"]: +    elif S[i] == "B": -    if S[i] in ["C", "?"]: +    elif S[i] == "C": +        dp[i + 1][3] += dp[i][2] +    else: +        for j in range(4): +            dp[i + 1][j] += dp[i][j] * 3 +        dp[i + 1][1] += dp[i][0] +        dp[i + 1][2] += dp[i][1]