from collections import defaultdict

import sys,heapq,bisect,math,itertools,string,queue,datetime

sys.setrecursionlimit(10**8)

INF = float('inf')

mod = 10**9+7

eps = 10**-7

AtoZ = [chr(i) for i in range(65,65+26)]

atoz = [chr(i) for i in range(97,97+26)]



def inpl(): return list(map(int, input().split()))

def inpl_str(): return list(input().split())





def ex_square(n,k): #n^k

	if k == 0:

		ans = 1

	elif k % 2 == 0:

		ans = ex_square(n, k//2) ** 2

	else:

		ans = ex_square(n,k-1) * n

	ans %= mod

	return ans



S = list(eval(input()))



aa = [0]

bb = [0]

cc = [0]

qq = [0]

Sa = Sb =Sc = Sq = 0

for s in S:

	if s == 'A':

		Sa += 1

	elif s == 'B':

		Sb += 1

	elif s == 'C':

		Sc += 1

	elif s == '?':

		Sq += 1

	aa.append(Sa)

	bb.append(Sb)

	cc.append(Sc)

	qq.append(Sq)



ans = 0

for i,s in enumerate(S):

	if s == 'B':

		SUMq = Sq

	elif s == '?':

		SUMq = Sq-1

	elif s == 'A' or s == 'C':

		continue



	Lq = qq[i]

	Rq = SUMq-Lq

	La = aa[i]

	Rc = Sc - cc[i]



	if SUMq == 0:

		ex = 1

		exx = exxx = 0

	elif SUMq == 1:

		ex = 3

		exx = 1

		exxx = 0

	else:

		exxx= ex_square(3,SUMq-2)

		exx = exxx*3%mod

		ex = exx*3%mod



	ans1 = (ex  * (La*Rc)%mod)%mod

	ans2 = (exx * (Lq*Rc)%mod)%mod

	ans3 = (exx * (La*Rq)%mod)%mod

	ans4 = (exxx * (Lq*Rq)%mod)%mod

	ans += (ans1+ans2+ans3+ans4)%mod

	ans %= mod



print(ans)
from collections import defaultdict

import sys,heapq,bisect,math,itertools,string,queue,datetime

sys.setrecursionlimit(10**8)

INF = float('inf')

mod = 10**9+7

eps = 10**-7

AtoZ = [chr(i) for i in range(65,65+26)]

atoz = [chr(i) for i in range(97,97+26)]



def inpl(): return list(map(int, input().split()))

def inpl_str(): return list(input().split())





def ex_square(n,k): #n^k

	if k <= 0:

		return 1

	elif k % 2 == 0:

		ans = ex_square(n, k//2) ** 2

	else:

		ans = ex_square(n,k-1) * n

	return ans%mod



S = list(eval(input()))



aa = [0]

bb = [0]

cc = [0]

qq = [0]

Sa = Sb =Sc = Sq = 0

for s in S:

	if s == 'A':

		Sa += 1

	elif s == 'B':

		Sb += 1

	elif s == 'C':

		Sc += 1

	elif s == '?':

		Sq += 1

	aa.append(Sa)

	bb.append(Sb)

	cc.append(Sc)

	qq.append(Sq)



ans = 0

ex = [ex_square(3,Sq),ex_square(3,Sq-1),ex_square(3,Sq-2),ex_square(3,Sq-3)]



for i,s in enumerate(S):

	if s == 'B':

		k = 0

	elif s == '?':

		k = 1

	elif s == 'A' or s == 'C':

		continue



	Lq = qq[i]

	Rq = Sq-Lq-k

	La = aa[i]

	Rc = Sc - cc[i]



	ans1 = (ex[0 + k] * (La*Rc)%mod)%mod

	ans2 = (ex[1 + k] * (Lq*Rc)%mod)%mod

	ans3 = (ex[1 + k] * (La*Rq)%mod)%mod

	ans4 = (ex[2 + k] * (Lq*Rq)%mod)%mod

	ans += (ans1+ans2+ans3+ans4)%mod

	ans %= mod



print(ans)

from collections import defaultdict
import sys, heapq, bisect, math, itertools, string, queue, datetime

sys.setrecursionlimit(10**8)
INF = float("inf")
mod = 10**9 + 7
eps = 10**-7
AtoZ = [chr(i) for i in range(65, 65 + 26)]
atoz = [chr(i) for i in range(97, 97 + 26)]


def inpl():
    return list(map(int, input().split()))


def inpl_str():
    return list(input().split())


def ex_square(n, k):  # n^k
    if k == 0:
        ans = 1
    elif k % 2 == 0:
        ans = ex_square(n, k // 2) ** 2
    else:
        ans = ex_square(n, k - 1) * n
    ans %= mod
    return ans


S = list(eval(input()))
aa = [0]
bb = [0]
cc = [0]
qq = [0]
Sa = Sb = Sc = Sq = 0
for s in S:
    if s == "A":
        Sa += 1
    elif s == "B":
        Sb += 1
    elif s == "C":
        Sc += 1
    elif s == "?":
        Sq += 1
    aa.append(Sa)
    bb.append(Sb)
    cc.append(Sc)
    qq.append(Sq)
ans = 0
for i, s in enumerate(S):
    if s == "B":
        SUMq = Sq
    elif s == "?":
        SUMq = Sq - 1
    elif s == "A" or s == "C":
        continue
    Lq = qq[i]
    Rq = SUMq - Lq
    La = aa[i]
    Rc = Sc - cc[i]
    if SUMq == 0:
        ex = 1
        exx = exxx = 0
    elif SUMq == 1:
        ex = 3
        exx = 1
        exxx = 0
    else:
        exxx = ex_square(3, SUMq - 2)
        exx = exxx * 3 % mod
        ex = exx * 3 % mod
    ans1 = (ex * (La * Rc) % mod) % mod
    ans2 = (exx * (Lq * Rc) % mod) % mod
    ans3 = (exx * (La * Rq) % mod) % mod
    ans4 = (exxx * (Lq * Rq) % mod) % mod
    ans += (ans1 + ans2 + ans3 + ans4) % mod
    ans %= mod
print(ans)

from collections import defaultdict
import sys, heapq, bisect, math, itertools, string, queue, datetime

sys.setrecursionlimit(10**8)
INF = float("inf")
mod = 10**9 + 7
eps = 10**-7
AtoZ = [chr(i) for i in range(65, 65 + 26)]
atoz = [chr(i) for i in range(97, 97 + 26)]


def inpl():
    return list(map(int, input().split()))


def inpl_str():
    return list(input().split())


def ex_square(n, k):  # n^k
    if k <= 0:
        return 1
    elif k % 2 == 0:
        ans = ex_square(n, k // 2) ** 2
    else:
        ans = ex_square(n, k - 1) * n
    return ans % mod


S = list(eval(input()))
aa = [0]
bb = [0]
cc = [0]
qq = [0]
Sa = Sb = Sc = Sq = 0
for s in S:
    if s == "A":
        Sa += 1
    elif s == "B":
        Sb += 1
    elif s == "C":
        Sc += 1
    elif s == "?":
        Sq += 1
    aa.append(Sa)
    bb.append(Sb)
    cc.append(Sc)
    qq.append(Sq)
ans = 0
ex = [
    ex_square(3, Sq),
    ex_square(3, Sq - 1),
    ex_square(3, Sq - 2),
    ex_square(3, Sq - 3),
]
for i, s in enumerate(S):
    if s == "B":
        k = 0
    elif s == "?":
        k = 1
    elif s == "A" or s == "C":
        continue
    Lq = qq[i]
    Rq = Sq - Lq - k
    La = aa[i]
    Rc = Sc - cc[i]
    ans1 = (ex[0 + k] * (La * Rc) % mod) % mod
    ans2 = (ex[1 + k] * (Lq * Rc) % mod) % mod
    ans3 = (ex[1 + k] * (La * Rq) % mod) % mod
    ans4 = (ex[2 + k] * (Lq * Rq) % mod) % mod
    ans += (ans1 + ans2 + ans3 + ans4) % mod
    ans %= mod
print(ans)

-    if k == 0: -        ans = 1 +    if k <= 0: +        return 1 -    ans %= mod -    return ans +    return ans % mod +ex = [ +    ex_square(3, Sq), +    ex_square(3, Sq - 1), +    ex_square(3, Sq - 2), +    ex_square(3, Sq - 3), +] -        SUMq = Sq +        k = 0 -        SUMq = Sq - 1 +        k = 1 -    Rq = SUMq - Lq +    Rq = Sq - Lq - k -    if SUMq == 0: -        ex = 1 -        exx = exxx = 0 -    elif SUMq == 1: -        ex = 3 -        exx = 1 -        exxx = 0 -    else: -        exxx = ex_square(3, SUMq - 2) -        exx = exxx * 3 % mod -        ex = exx * 3 % mod -    ans1 = (ex * (La * Rc) % mod) % mod -    ans2 = (exx * (Lq * Rc) % mod) % mod -    ans3 = (exx * (La * Rq) % mod) % mod -    ans4 = (exxx * (Lq * Rq) % mod) % mod +    ans1 = (ex[0 + k] * (La * Rc) % mod) % mod +    ans2 = (ex[1 + k] * (Lq * Rc) % mod) % mod +    ans3 = (ex[1 + k] * (La * Rq) % mod) % mod +    ans4 = (ex[2 + k] * (Lq * Rq) % mod) % mod