#!/mnt/c/Users/moiki/bash/env/bin/python

# N,M = map(int, input().split())

S = eval(input())

from collections import defaultdict

from bisect import bisect_left, bisect_right

a = []

b = []

c = []

# a = defaultdict(list)

# b = defaultdict(list)

# c = defaultdict(list)

q = []



for e,s in enumerate(S):

    if s == "A" :

        a.append(e)

    if s == "B" :

        b.append(e)

    if s == "C" :

        c.append(e)

    if s == "?":

        q.append(e)



ans = 0

MOD = int(1e9+7)



for eb in b:

    range_a = bisect_right(a, eb-1)

    # print("eb: {eb}, bisect_right({a}, {eb1}): {bis}".format

            # (eb = eb, a=a, eb1=eb-1, bis=bisect_right(a,eb-1)))

    range_c = len(c) - bisect_left(c, eb+1)

    

    range_cq = len(q) - bisect_left(q, eb+1)

    range_aq = bisect_right(q, eb-1)



    ans += range_a*range_c * pow(3, len(q), MOD) % MOD

    ans %= MOD

    if len(q)>=1:

        ans += range_a*range_cq * pow(3, len(q)-1, MOD)

        ans += range_aq*range_c * pow(3, len(q)-1, MOD)

        ans %= MOD

    if len(q)>=2:

        ans += range_aq*range_cq * pow(3, len(q)-2, MOD)

        ans %= MOD

for eq in q:

    # print("eb: {eb}, bisect_right({a}, {eb1}): {bis}".format

            # (eb = eq, a=a, eb1=eq-1, bis=bisect_right(a,eq-1)))

    range_a = bisect_right(a, eq-1)

    range_c = len(c) - bisect_left(c, eq+1)

    range_cq = len(q) - bisect_left(q, eq+1)

    range_aq = bisect_right(q, eq-1)



    ans += range_a*range_c * pow(3, len(q)-1, MOD)

    ans %= MOD

    if len(q) >= 2:

        ans += range_a*range_cq * pow(3, len(q)-2, MOD)

        ans += range_aq*range_c * pow(3, len(q)-2, MOD)

        ans %= MOD

    if len(q) >= 3:

        ans += range_aq*range_cq * pow(3, len(q)-3, MOD)

        ans %= MOD

    

print((ans % MOD))

    



#!/mnt/c/Users/moiki/bash/env/bin/python

# N,M = map(int, input().split())



MOD = int(1e9+7)

S = eval(input())



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



dp[0][0] = 1

for e,s in enumerate(S,1):



    dp[e][0] = dp[e-1][0] * (3 if s == "?" else 1)



    if s == "A" or s == "?":

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

    dp[e][1] += dp[e-1][1] * (3 if s == "?" else 1) 



    if s == "B" or s == "?":

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

    dp[e][2] += dp[e-1][2] * (3 if s == "?" else 1)



    if s == "C" or s == "?":

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

    dp[e][3] += dp[e-1][3] * (3 if s == "?" else 1)





    for i in range(4):

        dp[e][i] %= MOD







    # dp[i,0] = 1

    # dp[i][1] = dp[i-1][0]* if s == "A" or "?"





print((dp[len(S)][3] % MOD))





#!/mnt/c/Users/moiki/bash/env/bin/python
# N,M = map(int, input().split())
S = eval(input())
from collections import defaultdict
from bisect import bisect_left, bisect_right

a = []
b = []
c = []
# a = defaultdict(list)
# b = defaultdict(list)
# c = defaultdict(list)
q = []
for e, s in enumerate(S):
    if s == "A":
        a.append(e)
    if s == "B":
        b.append(e)
    if s == "C":
        c.append(e)
    if s == "?":
        q.append(e)
ans = 0
MOD = int(1e9 + 7)
for eb in b:
    range_a = bisect_right(a, eb - 1)
    # print("eb: {eb}, bisect_right({a}, {eb1}): {bis}".format
    # (eb = eb, a=a, eb1=eb-1, bis=bisect_right(a,eb-1)))
    range_c = len(c) - bisect_left(c, eb + 1)
    range_cq = len(q) - bisect_left(q, eb + 1)
    range_aq = bisect_right(q, eb - 1)
    ans += range_a * range_c * pow(3, len(q), MOD) % MOD
    ans %= MOD
    if len(q) >= 1:
        ans += range_a * range_cq * pow(3, len(q) - 1, MOD)
        ans += range_aq * range_c * pow(3, len(q) - 1, MOD)
        ans %= MOD
    if len(q) >= 2:
        ans += range_aq * range_cq * pow(3, len(q) - 2, MOD)
        ans %= MOD
for eq in q:
    # print("eb: {eb}, bisect_right({a}, {eb1}): {bis}".format
    # (eb = eq, a=a, eb1=eq-1, bis=bisect_right(a,eq-1)))
    range_a = bisect_right(a, eq - 1)
    range_c = len(c) - bisect_left(c, eq + 1)
    range_cq = len(q) - bisect_left(q, eq + 1)
    range_aq = bisect_right(q, eq - 1)
    ans += range_a * range_c * pow(3, len(q) - 1, MOD)
    ans %= MOD
    if len(q) >= 2:
        ans += range_a * range_cq * pow(3, len(q) - 2, MOD)
        ans += range_aq * range_c * pow(3, len(q) - 2, MOD)
        ans %= MOD
    if len(q) >= 3:
        ans += range_aq * range_cq * pow(3, len(q) - 3, MOD)
        ans %= MOD
print((ans % MOD))

#!/mnt/c/Users/moiki/bash/env/bin/python
# N,M = map(int, input().split())
MOD = int(1e9 + 7)
S = eval(input())
dp = [[0] * 4 for _ in range(len(S) + 1)]
dp[0][0] = 1
for e, s in enumerate(S, 1):
    dp[e][0] = dp[e - 1][0] * (3 if s == "?" else 1)
    if s == "A" or s == "?":
        dp[e][1] += dp[e - 1][0]
    dp[e][1] += dp[e - 1][1] * (3 if s == "?" else 1)
    if s == "B" or s == "?":
        dp[e][2] += dp[e - 1][1]
    dp[e][2] += dp[e - 1][2] * (3 if s == "?" else 1)
    if s == "C" or s == "?":
        dp[e][3] += dp[e - 1][2]
    dp[e][3] += dp[e - 1][3] * (3 if s == "?" else 1)
    for i in range(4):
        dp[e][i] %= MOD
    # dp[i,0] = 1
    # dp[i][1] = dp[i-1][0]* if s == "A" or "?"
print((dp[len(S)][3] % MOD))

+MOD = int(1e9 + 7) -from collections import defaultdict -from bisect import bisect_left, bisect_right - -a = [] -b = [] -c = [] -# a = defaultdict(list) -# b = defaultdict(list) -# c = defaultdict(list) -q = [] -for e, s in enumerate(S): -    if s == "A": -        a.append(e) -    if s == "B": -        b.append(e) -    if s == "C": -        c.append(e) -    if s == "?": -        q.append(e) -ans = 0 -MOD = int(1e9 + 7) -for eb in b: -    range_a = bisect_right(a, eb - 1) -    # print("eb: {eb}, bisect_right({a}, {eb1}): {bis}".format -    # (eb = eb, a=a, eb1=eb-1, bis=bisect_right(a,eb-1))) -    range_c = len(c) - bisect_left(c, eb + 1) -    range_cq = len(q) - bisect_left(q, eb + 1) -    range_aq = bisect_right(q, eb - 1) -    ans += range_a * range_c * pow(3, len(q), MOD) % MOD -    ans %= MOD -    if len(q) >= 1: -        ans += range_a * range_cq * pow(3, len(q) - 1, MOD) -        ans += range_aq * range_c * pow(3, len(q) - 1, MOD) -        ans %= MOD -    if len(q) >= 2: -        ans += range_aq * range_cq * pow(3, len(q) - 2, MOD) -        ans %= MOD -for eq in q: -    # print("eb: {eb}, bisect_right({a}, {eb1}): {bis}".format -    # (eb = eq, a=a, eb1=eq-1, bis=bisect_right(a,eq-1))) -    range_a = bisect_right(a, eq - 1) -    range_c = len(c) - bisect_left(c, eq + 1) -    range_cq = len(q) - bisect_left(q, eq + 1) -    range_aq = bisect_right(q, eq - 1) -    ans += range_a * range_c * pow(3, len(q) - 1, MOD) -    ans %= MOD -    if len(q) >= 2: -        ans += range_a * range_cq * pow(3, len(q) - 2, MOD) -        ans += range_aq * range_c * pow(3, len(q) - 2, MOD) -        ans %= MOD -    if len(q) >= 3: -        ans += range_aq * range_cq * pow(3, len(q) - 3, MOD) -        ans %= MOD -print((ans % MOD)) +dp = [[0] * 4 for _ in range(len(S) + 1)] +dp[0][0] = 1 +for e, s in enumerate(S, 1): +    dp[e][0] = dp[e - 1][0] * (3 if s == "?" else 1) +    if s == "A" or s == "?": +        dp[e][1] += dp[e - 1][0] +    dp[e][1] += dp[e - 1][1] * (3 if s == "?" else 1) +    if s == "B" or s == "?": +        dp[e][2] += dp[e - 1][1] +    dp[e][2] += dp[e - 1][2] * (3 if s == "?" else 1) +    if s == "C" or s == "?": +        dp[e][3] += dp[e - 1][2] +    dp[e][3] += dp[e - 1][3] * (3 if s == "?" else 1) +    for i in range(4): +        dp[e][i] %= MOD +    # dp[i,0] = 1 +    # dp[i][1] = dp[i-1][0]* if s == "A" or "?" +print((dp[len(S)][3] % MOD))