def main():

    MOD = 10 ** 9 + 7



    s = eval(input())

    n = len(s)



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

    dp[0][0] = 1



    for i, c in enumerate(s, start=1):

        for j in range(4):

            dp[i][j] = dp[i - 1][j]

        if c == 'A':

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

        elif c == 'B':

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

        elif c == 'C':

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

        else:

            for j in range(1, 4):

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

            for j in range(4):

                dp[i][j] += dp[i - 1][j] * 2

        for j in range(4):

            dp[i][j] %= MOD



    print((dp[n][3]))





if __name__ == '__main__':

    main()

def main():

    MOD = 10 ** 9 + 7



    s = eval(input())



    x, a, b, c = 1, 0, 0, 0



    for char in s:

        if char == 'A':

            a = (a + x) % MOD

        elif char == 'B':

            b = (b + a) % MOD

        elif char == 'C':

            c = (c + b) % MOD

        else:

            x, a, b, c = 3 * x % MOD, (3 * a + x) % MOD, (3 * b + a) % MOD, (3 * c + b) % MOD



    print(c)





if __name__ == '__main__':

    main()

def main():
    MOD = 10**9 + 7
    s = eval(input())
    n = len(s)
    dp = [[0] * 4 for _ in range(n + 1)]
    dp[0][0] = 1
    for i, c in enumerate(s, start=1):
        for j in range(4):
            dp[i][j] = dp[i - 1][j]
        if c == "A":
            dp[i][1] += dp[i - 1][0]
        elif c == "B":
            dp[i][2] += dp[i - 1][1]
        elif c == "C":
            dp[i][3] += dp[i - 1][2]
        else:
            for j in range(1, 4):
                dp[i][j] += dp[i - 1][j - 1]
            for j in range(4):
                dp[i][j] += dp[i - 1][j] * 2
        for j in range(4):
            dp[i][j] %= MOD
    print((dp[n][3]))


if __name__ == "__main__":
    main()

def main():
    MOD = 10**9 + 7
    s = eval(input())
    x, a, b, c = 1, 0, 0, 0
    for char in s:
        if char == "A":
            a = (a + x) % MOD
        elif char == "B":
            b = (b + a) % MOD
        elif char == "C":
            c = (c + b) % MOD
        else:
            x, a, b, c = (
                3 * x % MOD,
                (3 * a + x) % MOD,
                (3 * b + a) % MOD,
                (3 * c + b) % MOD,
            )
    print(c)


if __name__ == "__main__":
    main()

-    n = len(s) -    dp = [[0] * 4 for _ in range(n + 1)] -    dp[0][0] = 1 -    for i, c in enumerate(s, start=1): -        for j in range(4): -            dp[i][j] = dp[i - 1][j] -        if c == "A": -            dp[i][1] += dp[i - 1][0] -        elif c == "B": -            dp[i][2] += dp[i - 1][1] -        elif c == "C": -            dp[i][3] += dp[i - 1][2] +    x, a, b, c = 1, 0, 0, 0 +    for char in s: +        if char == "A": +            a = (a + x) % MOD +        elif char == "B": +            b = (b + a) % MOD +        elif char == "C": +            c = (c + b) % MOD -            for j in range(1, 4): -                dp[i][j] += dp[i - 1][j - 1] -            for j in range(4): -                dp[i][j] += dp[i - 1][j] * 2 -        for j in range(4): -            dp[i][j] %= MOD -    print((dp[n][3])) +            x, a, b, c = ( +                3 * x % MOD, +                (3 * a + x) % MOD, +                (3 * b + a) % MOD, +                (3 * c + b) % MOD, +            ) +    print(c)