import numpy as np

MOD = 10**9 + 7

def solve(s):

    n = len(s)

    dp = np.zeros((n+1, 4), dtype=int)

    dp[0,0] = 1

    for i in range(n):

        t = 2 * int(s[i] == "?") + 1

        dp[i+1] = t * dp[i] % MOD

        if s[i] == "A":

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

        elif s[i] == "B":

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

        elif s[i] == "C":

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

        else:

            dp[i+1,1:] += dp[i,:-1]

    return dp[n][3] % MOD



s = eval(input())

print((solve(s)))
import numpy as np

MOD = 10**9 + 7

def solve(s):

    n = len(s)

    a = np.zeros(4, dtype=int)

    b = a.copy()

    a[0] = 1

    for c in s:

        t = 2 * int(c == "?") + 1

        b = t * a % MOD

        if c == "A":

            b[1] += a[0]

        elif c == "B":

            b[2] += a[1]

        elif c == "C":

            b[3] += a[2]

        else:

            b[1:] += a[:-1]

        a = b

    return a[3] % MOD



s = eval(input())

print((solve(s)))
import numpy as np

MOD = 10**9 + 7


def solve(s):
    n = len(s)
    dp = np.zeros((n + 1, 4), dtype=int)
    dp[0, 0] = 1
    for i in range(n):
        t = 2 * int(s[i] == "?") + 1
        dp[i + 1] = t * dp[i] % MOD
        if s[i] == "A":
            dp[i + 1, 1] += dp[i, 0]
        elif s[i] == "B":
            dp[i + 1, 2] += dp[i, 1]
        elif s[i] == "C":
            dp[i + 1, 3] += dp[i, 2]
        else:
            dp[i + 1, 1:] += dp[i, :-1]
    return dp[n][3] % MOD


s = eval(input())
print((solve(s)))

import numpy as np

MOD = 10**9 + 7


def solve(s):
    n = len(s)
    a = np.zeros(4, dtype=int)
    b = a.copy()
    a[0] = 1
    for c in s:
        t = 2 * int(c == "?") + 1
        b = t * a % MOD
        if c == "A":
            b[1] += a[0]
        elif c == "B":
            b[2] += a[1]
        elif c == "C":
            b[3] += a[2]
        else:
            b[1:] += a[:-1]
        a = b
    return a[3] % MOD


s = eval(input())
print((solve(s)))

-    dp = np.zeros((n + 1, 4), dtype=int) -    dp[0, 0] = 1 -    for i in range(n): -        t = 2 * int(s[i] == "?") + 1 -        dp[i + 1] = t * dp[i] % MOD -        if s[i] == "A": -            dp[i + 1, 1] += dp[i, 0] -        elif s[i] == "B": -            dp[i + 1, 2] += dp[i, 1] -        elif s[i] == "C": -            dp[i + 1, 3] += dp[i, 2] +    a = np.zeros(4, dtype=int) +    b = a.copy() +    a[0] = 1 +    for c in s: +        t = 2 * int(c == "?") + 1 +        b = t * a % MOD +        if c == "A": +            b[1] += a[0] +        elif c == "B": +            b[2] += a[1] +        elif c == "C": +            b[3] += a[2] -            dp[i + 1, 1:] += dp[i, :-1] -    return dp[n][3] % MOD +            b[1:] += a[:-1] +        a = b +    return a[3] % MOD