The ABC number of a string T is the number of triples of integers (i, j, k) that satisfy all of the following conditions:
 - 1 ≤ i < j < k ≤ |T| (|T| is the length of T.)
 - T_i = A (T_i is the i-th character of T from the beginning.)
 - T_j = B
 - T_k = C
For example, when T = ABCBC, there are three triples of integers (i, j, k) that satisfy the conditions: (1, 2, 3), (1, 2, 5), (1, 4, 5). Thus, the ABC number of T is 3.
You are given a string S. Each character of S is A, B, C or ?.
Let Q be the number of occurrences of ? in S. We can make 3^Q strings by replacing each occurrence of ? in S with A, B or C. Find the sum of the ABC numbers of all these strings.
This sum can be extremely large, so print the sum modulo 10^9 + 7.

-----Constraints-----
 - 3 ≤ |S| ≤ 10^5
 - Each character of S is A, B, C or ?.

-----Input-----
Input is given from Standard Input in the following format:
S

-----Output-----
Print the sum of the ABC numbers of all the 3^Q strings, modulo 10^9 + 7.

-----Sample Input-----
A??C

-----Sample Output-----
8

In this case, Q = 2, and we can make 3^Q = 9 strings by by replacing each occurrence of ? with A, B or C. The ABC number of each of these strings is as follows:
 - AAAC: 0
 - AABC: 2
 - AACC: 0
 - ABAC: 1
 - ABBC: 2
 - ABCC: 2
 - ACAC: 0
 - ACBC: 1
 - ACCC: 0
The sum of these is 0 + 2 + 0 + 1 + 2 + 2 + 0 + 1 + 0 = 8, so we print 8 modulo 10^9 + 7, that is, 8.

Difficulty: MEDIUM

Solutions:
def main():
	MOD = 10 ** 9 + 7
	s = input()
	n = len(s)
	acc_a = [0] * (n + 1)
	acc_c = [0] * (n + 1)
	acc_q = [0] * (n + 1)
	a = 0
	c = 0
	q = 0
	for (i, char) in enumerate(s, 1):
		if char == 'A':
			a += 1
		elif char == 'C':
			c += 1
		elif char == '?':
			q += 1
		acc_a[i] = a
		acc_c[i] = c
		acc_q[i] = q
	sq = [1]
	for _ in range(acc_q[n]):
		sq.append(sq[-1] * 3 % MOD)
	ans = 0
	qn = acc_q[n]
	for (i, char) in enumerate(s, 1):
		if char == 'B' or char == '?':
			is_q = int(char == '?')
			rq = acc_q[n] - acc_q[i]
			rc = acc_c[n] - acc_c[i]
			t = 0
			if qn - is_q >= 0:
				t += acc_a[i - 1] * rc * sq[acc_q[n] - is_q]
			if qn - 1 - is_q >= 0:
				t += acc_a[i - 1] * rq * sq[acc_q[n] - 1 - is_q]
				t += acc_q[i - 1] * rc * sq[acc_q[n] - 1 - is_q]
			if qn - 2 - is_q >= 0:
				t += acc_q[i - 1] * rq * sq[acc_q[n] - 2 - is_q]
			t %= MOD
			ans = (ans + t) % MOD
	print(ans)

def __starting_point():
	main()
__starting_point()


S = input()
e = 1
a = 0
ab = 0
abc = 0
M = 10 ** 9 + 7
for i in S:
	if i == 'A':
		a = (a + e) % M
	if i == 'B':
		ab = (ab + a) % M
	if i == 'C':
		abc = (abc + ab) % M
	if i == '?':
		abc = (3 * abc + ab) % M
		ab = (3 * ab + a) % M
		a = (3 * a + e) % M
		e = e * 3 % M
print(abc)


MOD = 10 ** 9 + 7
S = input()
N = len(S)
nums = [1, 0, 0, 0]
for i in range(0, N):
	s = S[i]
	m = 3 if s == '?' else 1
	a = 1 if s == 'A' or s == '?' else 0
	b = 1 if s == 'B' or s == '?' else 0
	c = 1 if s == 'C' or s == '?' else 0
	n0 = nums[0] * m % MOD
	n1 = (nums[1] * m + nums[0] * a) % MOD
	n2 = (nums[2] * m + nums[1] * b) % MOD
	n3 = (nums[3] * m + nums[2] * c) % MOD
	nums = [n0, n1, n2, n3]
print(nums[3] % MOD)


data = input()
MOD = 10 ** 9 + 7
SLen = len(data)
targets = 'ABC'
array = [[0 for i in range(SLen + 1)] for _ in range(4)]
array[0][0] = 1
for i in range(SLen):
	for j in range(4):
		if data[i] == '?':
			if j < 3:
				array[j + 1][i + 1] = (array[j + 1][i + 1] + array[j][i]) % MOD
			array[j][i + 1] = (array[j][i + 1] + array[j][i] * 3) % MOD
		else:
			if j < 3 and data[i] == targets[j]:
				array[j + 1][i + 1] = (array[j + 1][i + 1] + array[j][i]) % MOD
			array[j][i + 1] = (array[j][i + 1] + array[j][i]) % MOD
print(array[3][SLen])


S = input()
MOD = 10 ** 9 + 7
A = 0
AB = 0
Q = 1
ans = 0
for s in S:
	if s == '?':
		ans = (3 * ans + AB) % MOD
		AB = (3 * AB + A) % MOD
		A = (3 * A + Q) % MOD
		Q = Q * 3 % MOD
	elif s == 'A':
		A = (A + Q) % MOD
	elif s == 'B':
		AB = (AB + A) % MOD
	elif s == 'C':
		ans = (ans + AB) % MOD
print(ans)


s = input()
a = [1, 0, 0, 0]
for i in range(len(s)):
	c = s[i]
	if c == 'A':
		a[1] = (a[1] + a[0]) % 1000000007
	elif c == 'B':
		a[2] = (a[2] + a[1]) % 1000000007
	elif c == 'C':
		a[3] = (a[3] + a[2]) % 1000000007
	elif c == '?':
		tmp = [a[i] for i in range(4)]
		a[0] = tmp[0] * 3
		for j in range(3):
			a[j + 1] = tmp[j + 1] * 3 + tmp[j]
		a = list(map(lambda x: x % 1000000007, a))
print(a[3] % 1000000007)


MOD = 10 ** 9 + 7
p3 = [1]
S = input()
n = len(S)
a = []
c = []
q = []
for i in range(n):
	p3.append(int(p3[i] * 3 % MOD))
	if S[i] == 'A':
		a.append(1)
	else:
		a.append(0)
	if S[i] == 'C':
		c.append(1)
	else:
		c.append(0)
	if S[i] == '?':
		q.append(1)
	else:
		q.append(0)
for i in range(1, n):
	a[i] += a[i - 1]
	c[i] += c[i - 1]
	q[i] += q[i - 1]
count = 0
for i in range(1, n - 1):
	if S[i] == 'B' or S[i] == '?':
		bq = 0 if S[i] == 'B' else 1
		count += a[i - 1] * (c[n - 1] - c[i]) * p3[q[n - 1] - bq]
		count += q[i - 1] * (c[n - 1] - c[i]) * p3[q[n - 1] - 1 - bq]
		count += a[i - 1] * (q[n - 1] - q[i]) * p3[q[n - 1] - 1 - bq]
		count += q[i - 1] * (q[n - 1] - q[i]) * p3[q[n - 1] - 2 - bq]
print(int(count % MOD))


S = input()
nQ_r = S.count('?')
nC_r = S.count('C')
nQ_l = 0
nA_l = 0
N = len(S)
mod = 10 ** 9 + 7
pow3 = [1] * N
p = 1
for i in range(1, N):
	pow3[i] = p = p * 3 % mod
ans = 0
for (n, c) in enumerate(S):
	if c == 'C':
		nC_r -= 1
	if c == '?':
		nQ_r -= 1
	if n > 0 and n < N - 1 and (c == 'B' or c == '?'):
		p = (nQ_l * pow3[max(0, nQ_l - 1)] + nA_l * pow3[nQ_l]) % mod
		q = (nQ_r * pow3[max(0, nQ_r - 1)] + nC_r * pow3[nQ_r]) % mod
		ans += p * q
		ans %= mod
	if c == 'A':
		nA_l += 1
	if c == '?':
		nQ_l += 1
print(ans)


S = list(input())
n = len(S)
mod = 10 ** 9 + 7
dpa = [0] * (10 ** 5 + 10)
dpc = [0] * (10 ** 5 + 10)
hatena_count = 0
pow3 = [1] * (10 ** 5 + 10)
for i in range(1, 10 ** 5 + 10):
	pow3[i] = pow3[i - 1] * 3 % mod
for i in range(n):
	if S[i] == 'A':
		dpa[i + 1] = (dpa[i] + pow3[hatena_count]) % mod
	elif S[i] == '?':
		dpa[i + 1] = (3 * dpa[i] + pow3[hatena_count]) % mod
		hatena_count += 1
	else:
		dpa[i + 1] = dpa[i]
S.reverse()
hatena_count = 0
for i in range(n):
	if S[i] == 'C':
		dpc[i + 1] = (dpc[i] + pow3[hatena_count]) % mod
	elif S[i] == '?':
		dpc[i + 1] = (3 * dpc[i] + pow3[hatena_count]) % mod
		hatena_count += 1
	else:
		dpc[i + 1] = dpc[i]
ans = 0
for i in range(1, n - 1):
	if S[i] == 'B' or S[i] == '?':
		ans += dpa[n - 1 - i] * dpc[i]
		ans %= mod
print(ans)


S = input()
mod = 10 ** 9 + 7
(A, AB, ABC, ALL) = (0, 0, 0, 1)
for s in S:
	if s == 'A':
		A += ALL
		A %= mod
	if s == 'B':
		AB += A
		AB %= mod
	if s == 'C':
		ABC += AB
		ABC %= mod
	if s == '?':
		ABC = (3 * ABC + AB) % mod
		AB = (3 * AB + A) % mod
		A = (3 * A + ALL) % mod
		ALL *= 3
		ALL %= mod
print(ABC)


s = input()
mod = 10 ** 9 + 7
l = len(s)
dp = [[0] * 4 for _ in range(l + 1)]
dp[0][0] = 1
for (i, e) in enumerate(s, 1):
	for j in range(4):
		if e == '?':
			dp[i][j] += dp[i - 1][j] * 3
		else:
			dp[i][j] += dp[i - 1][j]
	if e == 'A' or e == '?':
		dp[i][1] += dp[i - 1][0]
	if e == 'B' or e == '?':
		dp[i][2] += dp[i - 1][1]
	if e == 'C' or e == '?':
		dp[i][3] += dp[i - 1][2]
	for j in range(4):
		dp[i][j] %= mod
ans = dp[l][3]
print(ans)


S = input()
DP = [0, 0, 0, 1]
p = 10 ** 9 + 7
for s in S[::-1]:
	dp = [0, 0, 0, 0]
	dp[3] = DP[3] * (3 if s == '?' else 1) % p
	for i in range(2, -1, -1):
		dp[i] += DP[i] * (3 if s == '?' else 1)
		dp[i] += DP[i + 1] if s == chr(65 + i) or s == '?' else 0
		dp[i] %= p
	DP = dp
print(DP[0] % p)


S = input()
MOD = 10 ** 9 + 7
N = len(S)
dp = [[0] * 4 for _ in range(N + 1)]
dp[-1][-1] = 1
for i in reversed(range(N)):
	m = 3 if S[i] == '?' else 1
	for j in reversed(range(3)):
		dp[i][j] = m * dp[i + 1][j]
		if S[i] == '?' or S[i] == 'ABC'[j]:
			dp[i][j] += dp[i + 1][j + 1]
		dp[i][j] %= MOD
	dp[i][3] = m * dp[i + 1][3] % MOD
print(dp[0][0] % MOD)


M = 10 ** 9 + 7
S = input()
A = 0
AB = 0
ABC = 0
X = 1
for c in S:
	if c == 'C':
		ABC = (ABC + AB) % M
	elif c == 'B':
		AB = (AB + A) % M
	elif c == 'A':
		A = (A + X) % M
	elif c == '?':
		ABC = (3 * ABC + AB) % M
		AB = (3 * AB + A) % M
		A = (3 * A + X) % M
		X = 3 * X % M
print(ABC)


from itertools import accumulate as ac
s = input()
n = len(s)
mod = int(1000000000.0) + 7
(*aca,) = ac([0] + [1 if i == 'A' else 0 for i in s])
(*acc,) = ac([0] + [1 if i == 'C' else 0 for i in s])
(*acq,) = ac([0] + [1 if i == '?' else 0 for i in s])
v = 0
for i in range(1, n - 1):
	if s[i] == 'B' or s[i] == '?':
		a = aca[i]
		l = acq[i]
		c = acc[n] - acc[i + 1]
		r = acq[n] - acq[i + 1]
		v += a * c * pow(3, l + r, mod)
		if l + r - 1 >= 0:
			v += a * r * pow(3, l + r - 1, mod) + l * c * pow(3, l + r - 1, mod)
		if l + r - 2 >= 0:
			v += l * r * pow(3, l + r - 2, mod)
		v %= mod
print(v)


S = input()
mod = 10 ** 9 + 7
abc = 0
bc = 0
c = 0
q = 0
for i in S[::-1]:
	if i == 'A':
		abc = (bc + abc) % mod
	if i == 'B':
		bc = (c + bc) % mod
	if i == 'C':
		c = (pow(3, q, mod) + c) % mod
	if i == '?':
		abc = (bc + abc * 3) % mod
		bc = (c + bc * 3) % mod
		c = (pow(3, q, mod) + c * 3) % mod
		q += 1
print(abc)


MOD = 10 ** 9 + 7
s = input()
n = len(s)
dp = [[0] * 4 for _ in range(n + 1)]
dp[0][0] = 1
for i in range(n):
	if s[i] == '?':
		dp[i + 1][0] += dp[i][0] * 3
		dp[i + 1][0] %= MOD
	else:
		dp[i + 1][0] += dp[i][0]
	for j in range(3):
		if s[i] == 'ABC'[j] or s[i] == '?':
			dp[i + 1][j + 1] += dp[i][j]
		dp[i + 1][j + 1] += dp[i][j + 1] * (3 if s[i] == '?' else 1)
		dp[i + 1][j + 1] %= MOD
print(dp[n][3] % MOD)


MOD = 10 ** 9 + 7
S = input()
dp = []
for i in range(len(S) + 1):
	dp.append([0, 0, 0, 0])
dp[-1][3] = 1
for i in reversed(range(len(S))):
	if S[i] == '?':
		m1 = 3
	else:
		m1 = 1
	dp[i][3] = m1 * dp[i + 1][3]
	dp[i][3] %= MOD
	for j in range(3):
		if S[i] == '?':
			m2 = 1
		elif S[i] == ['A', 'B', 'C'][j]:
			m2 = 1
		else:
			m2 = 0
		dp[i][j] = m1 * dp[i + 1][j] + m2 * dp[i + 1][j + 1]
		dp[i][j] %= MOD
print(dp[0][0])


import sys
input = sys.stdin.readline
MOD = 10 ** 9 + 7
S = input()
(a, ab, abc) = [0] * 3
c = 1
for s in S:
	if s == 'A':
		a = (a + c) % MOD
	elif s == 'B':
		ab = (ab + a) % MOD
	elif s == 'C':
		abc = (abc + ab) % MOD
	elif s == '?':
		abc = (3 * abc + ab) % MOD
		ab = (3 * ab + a) % MOD
		a = (3 * a + c) % MOD
		c = 3 * c % MOD
print(abc % MOD)


def main():
	MOD = 10 ** 9 + 7
	S = input()
	(z, a, b, c) = (1, 0, 0, 0)
	for x in S:
		if x == 'C':
			c = (c + b) % MOD
		elif x == 'B':
			b = (b + a) % MOD
		elif x == 'A':
			a = (a + z) % MOD
		elif x == '?':
			(z, a, b, c) = (z * 3 % MOD, (a * 3 + z) % MOD, (b * 3 + a) % MOD, (c * 3 + b) % MOD)
	print(c)

def __starting_point():
	main()
__starting_point()


s = input()
l = len(s)
dp = [4 * [0] for _ in range(l + 1)]
dp[0][0] = 1
for i in range(1, l + 1):
	si = s[i - 1]
	for j in range(4):
		dp[i][j] = dp[i - 1][j] * ((si == '?') * 2 + 1)
	if si == 'A':
		dp[i][1] += dp[i - 1][0]
	if si == 'B':
		dp[i][2] += dp[i - 1][1]
	if si == 'C':
		dp[i][3] += dp[i - 1][2]
	if si == '?':
		dp[i][1] += dp[i - 1][0]
		dp[i][2] += dp[i - 1][1]
		dp[i][3] += dp[i - 1][2]
	for j in range(4):
		dp[i][j] %= 10 ** 9 + 7
print(dp[-1][-1])


S = input()
N = len(S)
O = 1
A = [0] * (N + 1)
AB = [0] * (N + 1)
ABC = [0] * (N + 1)
for i in range(1, N + 1):
	A[i] = A[i - 1]
	AB[i] = AB[i - 1]
	ABC[i] = ABC[i - 1]
	if S[i - 1] is 'A':
		A[i] += O
	elif S[i - 1] is 'B':
		AB[i] += A[i - 1]
	elif S[i - 1] is 'C':
		ABC[i] += AB[i - 1]
	elif S[i - 1] is '?':
		A[i] *= 3
		A[i] += O
		AB[i] *= 3
		AB[i] += A[i - 1]
		ABC[i] *= 3
		ABC[i] += AB[i - 1]
		O *= 3
	A[i] = A[i] % (10 ** 9 + 7)
	AB[i] = AB[i] % (10 ** 9 + 7)
	ABC[i] = ABC[i] % (10 ** 9 + 7)
	O = O % (10 ** 9 + 7)
print(int(ABC[N] % (10 ** 9 + 7)))


def ary(r, c, v):
	return [[v for _ in range(c)] for _ in range(r)]
s = input()
ls = len(s)
dp = ary(ls + 1, 4, 0)
dp[0][0] = 1
mod = 10 ** 9 + 7
for i in range(1, ls + 1):
	for j in range(4):
		dp[i][j] += dp[i - 1][j] * (3 if s[i - 1] == '?' else 1)
		if j > 0:
			if s[i - 1] == '?' or s[i - 1] == 'ABC'[j - 1]:
				dp[i][j] += dp[i - 1][j - 1]
		dp[i][j] %= mod
print(dp[ls][3])


A = list(input())
n = len(A)
mod = 10 ** 9 + 7
ans = 0
numa = A[0] == 'A'
numa_ = A[0] == '?'
numc = A[2:].count('C')
numc_ = A[2:].count('?')
for i in range(1, n - 1):
	apro = cpro = 0
	if A[i] == 'B' or A[i] == '?':
		apro = (3 * numa + numa_) * pow(3, numa_ - 1, mod) % mod
		cpro = (3 * numc + numc_) * pow(3, numc_ - 1, mod) % mod
		ans += apro * cpro % mod
		ans %= mod
	if A[i] == 'A':
		numa += 1
	if A[i] == '?':
		numa_ += 1
	if A[i + 1] == 'C':
		numc -= 1
	if A[i + 1] == '?':
		numc_ -= 1
print(ans)


s = input()
table = [[0 for _ in range(4)] for _ in range(len(s) + 1)]
table[0][0] = 1
abc = 'ABC'
for i in range(len(s)):
	for j in range(4):
		if s[i] == '?' and j != 0:
			table[i + 1][j] = (3 * table[i][j] + table[i][j - 1]) % (10 ** 9 + 7)
		elif s[i] == '?':
			table[i + 1][0] = 3 * table[i][0] % (10 ** 9 + 7)
		elif s[i] == abc[j - 1] and j != 0:
			table[i + 1][j] = (table[i][j] + table[i][j - 1]) % (10 ** 9 + 7)
		else:
			table[i + 1][j] = table[i][j] % (10 ** 9 + 7)
print(table[i + 1][j] % (10 ** 9 + 7))


MOD = 10 ** 9 + 7
S = input()
N = len(S)
dp = [[0] * 4 for i in range(N + 1)]
dp[0][0] = 1
for i in range(N):
	s = S[i]
	for j in range(4):
		if s == '?':
			dp[i + 1][j] += dp[i][j] * 3
		else:
			dp[i + 1][j] += dp[i][j]
		dp[i + 1][j] %= MOD
		if j < 3 and (s == '?' or s == 'ABC'[j]):
			dp[i + 1][j + 1] += dp[i][j]
			dp[i + 1][j + 1] %= MOD
print(dp[N][3] % MOD)


def main():
	S = input()
	N = len(S)
	m = 10 ** 9 + 7
	dp = [[0] * 4 for _ in range(N + 1)]
	for i in range(N, -1, -1):
		for j in range(3, -1, -1):
			if i == N:
				if j == 3:
					dp[i][j] = 1
				else:
					dp[i][j] = 0
			else:
				if S[i] == '?':
					dp[i][j] = dp[i + 1][j] * 3 % m
				else:
					dp[i][j] = dp[i + 1][j]
				if j < 3:
					if S[i] == '?' or S[i] == 'ABC'[j]:
						dp[i][j] += dp[i + 1][j + 1]
						dp[i][j] %= m
	print(dp[0][0])

def __starting_point():
	main()
__starting_point()


import sys
input = sys.stdin.readline
S = input().rstrip()
N = len(S)
mod = 10 ** 9 + 7
cnt = 0
A = B = C = 0
for c in S:
	if c == 'A':
		A += pow(3, cnt, mod)
	if c == 'B':
		B += A
	if c == 'C':
		C += B
	if c == '?':
		C = C * 3 + B
		B = B * 3 + A
		A = A * 3 + pow(3, cnt, mod)
		cnt += 1
	A %= mod
	B %= mod
	C %= mod
print(C)


MOD = 1000000007
S = input()
(a, ab, abc, all_s) = (0, 0, 0, 1)
for s in S:
	if s == 'A':
		a = (a + all_s) % MOD
	elif s == 'B':
		ab = (ab + a) % MOD
	elif s == 'C':
		abc = (abc + ab) % MOD
	elif s == '?':
		abc = (abc * 3 + ab) % MOD
		ab = (ab * 3 + a) % MOD
		a = (a * 3 + all_s) % MOD
		all_s = all_s * 3 % MOD
print(abc)


S = input()
mod = 10 ** 9 + 7
dp = [[0] * 4 for i in range(len(S) + 1)]
dp[len(S)][3] = 1
for i in range(len(S) - 1, -1, -1):
	dp[i][3] += dp[i + 1][3] * 3 if S[i] == '?' else dp[i + 1][3]
	dp[i][3] %= mod
	for j in range(2, -1, -1):
		dp[i][j] += dp[i + 1][j] * 3 if S[i] == '?' else dp[i + 1][j]
		dp[i][j] += dp[i + 1][j + 1] if S[i] == 'ABC'[j] or S[i] == '?' else 0
		dp[i][j] %= mod
print(dp[0][0])


s = input()
abc = 'ABCD'
n = len(s)
mod = 10 ** 9 + 7
dp = [[0] * 4 for i in range(n + 1)]
dp[n][3] = 1
for i in range(n - 1, -1, -1):
	for j in range(4):
		if s[i] == '?':
			if j == 3:
				dp[i][j] = 3 * dp[i + 1][j]
			else:
				dp[i][j] = 3 * dp[i + 1][j] + dp[i + 1][j + 1]
		elif s[i] == abc[j]:
			if j != 3:
				dp[i][j] = dp[i + 1][j] + dp[i + 1][j + 1]
			else:
				dp[i][j] = dp[i + 1][j]
		else:
			dp[i][j] = dp[i + 1][j]
		dp[i][j] %= mod
print(dp[0][0])


mod = 10 ** 9 + 7
s = list(input())
s = ['-'] + s
l = len(s)
dp = [[0 for j in range(4)] for i in range(l)]
dp[0][0] = 1
for i in range(1, l):
	for j in range(4):
		dp[i][j] = dp[i - 1][j]
		if s[i] == '?':
			dp[i][j] *= 3
		if j and (s[i] == '?' or 'ABC'[j - 1] == s[i]):
			dp[i][j] += dp[i - 1][j - 1]
		dp[i][j] %= mod
print(dp[-1][3])


S = input()
s = 'ABC'
N = len(S)
C = 10 ** 9 + 7
dp = [[0] * 4 for _ in range(N + 1)]
dp[N][3] = 1
for i in reversed(range(N)):
	for j in range(3):
		if i < j:
			continue
		dp[i][j] = ((1 + (S[i] == '?') * 2) * dp[i + 1][j] + (S[i] in {s[j], '?'}) * dp[i + 1][j + 1]) % C
	if i >= 3:
		dp[i][3] = (1 + (S[i] == '?') * 2) * dp[i + 1][3] % C
print(dp[0][0])


import sys
sr = lambda : sys.stdin.readline().rstrip()
ir = lambda : int(sr())
lr = lambda : list(map(int, sr().split()))
S = sr()[::-1]
MOD = 10 ** 9 + 7
answer = 0
dp = [0, 0, 1]
for i in range(len(S)):
	s = S[i]
	if s == 'A':
		answer += dp[1]
	elif s == 'B':
		dp[1] += dp[0]
	elif s == 'C':
		dp[0] += dp[2]
	elif s == '?':
		answer *= 3
		answer += dp[1]
		dp[1] *= 3
		dp[1] += dp[0]
		dp[0] *= 3
		dp[0] += dp[2]
		dp[2] *= 3
	answer %= MOD
	dp[0] %= MOD
	dp[1] %= MOD
	dp[2] %= MOD
print(answer)


MOD = 10 ** 9 + 7
s = input()
a = 0
ql = 0
c = s.count('C')
qr = s.count('?')
ans = 0
for si in s:
	if si == '?':
		qr -= 1
	if si in 'B?':
		ans = (ans + pow(3, ql + qr, MOD) * a * c + (pow(3, ql + qr - 1, MOD) * (ql * c + a * qr) if ql + qr - 1 >= 0 else 0) + (pow(3, ql + qr - 2, MOD) * ql * qr if ql + qr - 2 >= 0 else 0)) % MOD
	if si == '?':
		ql += 1
	elif si == 'A':
		a += 1
	elif si == 'C':
		c -= 1
print(ans)


MOD = 10 ** 9 + 7

def main():
	S = input()
	N = len(S)
	pre_hatena = S[:1].count('?')
	a = S[:1].count('A')
	post_hatena = S[2:].count('?')
	c = S[2:].count('C')
	ans = 0
	for i in range(1, N - 1):
		if S[i] == '?' or S[i] == 'B':
			pre_pow = pow(3, pre_hatena - 1, MOD)
			pre = pre_pow * 3 % MOD * a + pre_pow * pre_hatena
			post_pow = pow(3, post_hatena - 1, MOD)
			post = post_pow * 3 % MOD * c + post_pow * post_hatena
			ans += pre * post
			ans %= MOD
		if S[i] == '?':
			pre_hatena += 1
		if S[i] == 'A':
			a += 1
		if S[i + 1] == '?':
			post_hatena -= 1
		if S[i + 1] == 'C':
			c -= 1
	print(ans)

def __starting_point():
	main()
__starting_point()


S = input()
p = 10 ** 9 + 7
DP = [0, 0, 0, 1]
dp = [0] * 4
for i in range(len(S) - 1, -1, -1):
	for j in range(3, -1, -1):
		if j == 3:
			dp[j] = DP[3] * (3 if S[i] == '?' else 1) % p
		elif S[i] == '?':
			dp[j] = (3 * DP[j] + DP[j + 1]) % p
		elif S[i] == chr(65 + j):
			dp[j] = (DP[j] + DP[j + 1]) % p
		else:
			dp[j] = DP[j]
	DP = [x for x in dp]
print(DP[0] % p)


S = input()
N = len(S)
(a, b, c, d) = (0, 0, 0, 1)
mod = 10 ** 9 + 7
for x in reversed(S):
	if x == '?':
		(a, b, c, d) = (3 * a + b, 3 * b + c, 3 * c + d, 3 * d)
	elif x == 'A':
		a += b
	elif x == 'B':
		b += c
	elif x == 'C':
		c += d
	a %= mod
	b %= mod
	c %= mod
	d %= mod
print(a)


from collections import Counter
s = list(input())
n = len(s)
c = Counter(s)
mod = 10 ** 9 + 7
left_a = 0
left_b = 0
left_c = 0
left_q = 0
right_a = c['A']
right_b = c['B']
right_c = c['C']
right_q = c['?']

def add_ans():
	lhs = left_a * pow(3, left_q, mod) + left_q * pow(3, left_q - 1, mod)
	rhs = right_c * pow(3, right_q, mod) + right_q * pow(3, right_q - 1, mod)
	return lhs * rhs % mod
ans = 0
for i in range(n):
	if s[i] == 'A':
		left_a += 1
	if s[i] == 'B':
		ans = (ans + add_ans()) % mod
	if s[i] == 'C':
		right_c -= 1
	if s[i] == '?':
		right_q -= 1
		ans = (ans + add_ans()) % mod
		left_q += 1
print(ans)


S = input()
N = len(S)
MOD = 10 ** 9 + 7
res = 0
dp = [[0] * 4 for _ in range(100001)]
for i in reversed(range(N + 1)):
	for j in reversed(range(4)):
		if i == N:
			dp[i][j] = int(j == 3)
		else:
			dp[i][j] = dp[i + 1][j] * (3 if S[i] == '?' else 1)
			if j < 3 and (S[i] == '?' or S[i] == 'ABC'[j]):
				dp[i][j] += dp[i + 1][j + 1]
			dp[i][j] %= MOD
print(dp[0][0])


s = input()
m = 1000000007
dp = [0] * 4
dp[0] = 1
for c in s:
	if c == 'A':
		dp[1] += dp[0]
	elif c == 'B':
		dp[2] += dp[1]
	elif c == 'C':
		dp[3] += dp[2]
	elif c == '?':
		dp = [dp[0] * 3 % m, (dp[0] + dp[1] * 3) % m, (dp[1] + dp[2] * 3) % m, (dp[2] + dp[3] * 3) % m]
print(dp[3] % m)


md = 10 ** 9 + 7
s = input()
dp = [[0] * 4 for _ in range(3)]
for c in s:
	if c == 'A':
		dp[0][0] = (dp[0][0] + 1) % md
	if c == 'B':
		for j in range(2):
			dp[1][j] = (dp[1][j] + dp[0][j]) % md
	if c == 'C':
		for j in range(3):
			dp[2][j] = (dp[2][j] + dp[1][j]) % md
	if c == '?':
		for j in range(3):
			dp[2][j + 1] = (dp[2][j + 1] + dp[1][j]) % md
		for j in range(2):
			dp[1][j + 1] = (dp[1][j + 1] + dp[0][j]) % md
		dp[0][1] = (dp[0][1] + 1) % md
ans = 0
q = s.count('?')
for j in range(4):
	if q - j < 0:
		break
	ans += dp[2][j] * pow(3, q - j, md)
	ans %= md
print(ans)


s = input()
mod = 10 ** 9 + 7
dpa = [[0, 0] for i in range(len(s) + 1)]
dpc = [[0, 0] for i in range(len(s) + 1)]
power3 = [1 for i in range(10 ** 5 + 100)]
for i in range(10 ** 5 + 3):
	power3[i + 1] = power3[i] * 3 % mod
for i in range(len(s)):
	if s[i] == 'A':
		dpa[i + 1][0] = (dpa[i][0] + power3[dpa[i][1]]) % mod
		dpa[i + 1][1] = dpa[i][1]
	elif s[i] == '?':
		dpa[i + 1][0] = (3 * dpa[i][0] + power3[dpa[i][1]]) % mod
		dpa[i + 1][1] = dpa[i][1] + 1
	else:
		dpa[i + 1] = dpa[i]
for i in range(len(s)):
	if s[len(s) - 1 - i] == 'C':
		dpc[i + 1][0] = (dpc[i][0] + power3[dpc[i][1]]) % mod
		dpc[i + 1][1] = dpc[i][1]
	elif s[len(s) - 1 - i] == '?':
		dpc[i + 1][0] = (3 * dpc[i][0] + power3[dpc[i][1]]) % mod
		dpc[i + 1][1] = dpc[i][1] + 1
	else:
		dpc[i + 1] = dpc[i]
ans = 0
for i in range(1, len(s) - 1):
	if s[i] == 'B' or s[i] == '?':
		ans += dpa[i][0] * dpc[len(s) - 1 - i][0]
		ans = ans % mod
print(ans)


s = input()
ls = len(s)
dp = [[0 for _ in range(4)] for _ in range(ls + 1)]
dp[0][0] = 1
mod = 10 ** 9 + 7
for i in range(1, ls + 1):
	for j in range(4):
		dp[i][j] += dp[i - 1][j] * (3 if s[i - 1] == '?' else 1)
		if j > 0:
			dp[i][j] += dp[i - 1][j - 1] if s[i - 1] == 'ABC'[j - 1] or s[i - 1] == '?' else 0
		dp[i][j] %= mod
print(dp[ls][3])


S = input()
mod = 10 ** 9 + 7
dic = {'': 1, 'A': 0, 'B': 0, 'C': 0}
for s in S:
	if s == 'A':
		dic[s] += dic['']
	elif s == 'B':
		dic[s] += dic['A']
	elif s == 'C':
		dic[s] += dic['B']
	elif s == '?':
		dic['C'] = dic['C'] * 3 + dic['B']
		dic['B'] = dic['B'] * 3 + dic['A']
		dic['A'] = dic['A'] * 3 + dic['']
		dic[''] *= 3
	dic[''] %= mod
	dic['A'] %= mod
	dic['B'] %= mod
	dic['C'] %= mod
print(dic['C'] % mod)


s = input()
n = len(s)
mod = 10 ** 9 + 7
dp = [[0] * 4 for _ in range(n + 1)]
dp[n][3] = 1
for i in reversed(list(range(n))):
	for j in reversed(list(range(4))):
		if j == 3:
			if s[i] == '?':
				m = 3
			else:
				m = 1
			dp[i][j] += m * dp[i + 1][j]
		else:
			if s[i] == '?':
				m1 = 3
			else:
				m1 = 1
			if j == 0:
				if s[i] == 'A' or s[i] == '?':
					m2 = 1
				else:
					m2 = 0
			elif j == 1:
				if s[i] == 'B' or s[i] == '?':
					m2 = 1
				else:
					m2 = 0
			elif s[i] == 'C' or s[i] == '?':
				m2 = 1
			else:
				m2 = 0
			dp[i][j] += m1 * dp[i + 1][j] + m2 * dp[i + 1][j + 1]
		dp[i][j] %= mod
print(dp[0][0] % mod)


s = input()
l = len(s)
mod = 10 ** 9 + 7
dp = [[0, 0, 0, 0] for _ in range(l + 1)]
for i in range(l, -1, -1):
	for j in range(3, -1, -1):
		if i == l:
			if j == 3:
				dp[i][j] = 1
			else:
				dp[i][j] = 0
		else:
			if s[i] == '?':
				dp[i][j] = dp[i + 1][j] * 3
			else:
				dp[i][j] = dp[i + 1][j]
			if j != 3 and (s[i] == '?' or s[i] == 'ABC'[j]):
				dp[i][j] += dp[i + 1][j + 1]
		dp[i][j] %= mod
print(dp[0][0])


import sys
input = sys.stdin.readline
S = input().strip()
mod = 10 ** 9 + 7
a = 0
ab = 0
ans = 0
q = 1
for c in S:
	if c == 'A':
		a += q
	elif c == 'B':
		ab += a
	elif c == 'C':
		ans += ab
	elif c == '?':
		(a, ab, ans, q) = (a * 3 + q, ab * 3 + a, ans * 3 + ab, q * 3)
	a %= mod
	ab %= mod
	ans %= mod
	q %= mod
print(ans % mod)


s = input()
A = [1, 0]
AB = 0
ABC = 0
D = 10 ** 9 + 7
for c in s:
	if c == 'A':
		A[1] += A[0]
	elif c == 'B':
		AB += A[1]
	elif c == 'C':
		ABC += AB
	elif c == '?':
		ABC = (ABC % D * 3 % D + AB % D) % D
		AB = (AB % D * 3 % D + A[1] % D) % D
		A[1] = (A[1] % D * 3 % D + A[0] % D) % D
		A[0] = A[0] % D * 3 % D
print(ABC % D)


S = input()
mod = 10 ** 9 + 7
(La, Lq, Rc, Rq) = (0, 0, S.count('C'), S.count('?'))
ans = 0
f = lambda x, y: pow(x, y, mod) if y >= 0 else 0
for s in S:
	Rc -= s == 'C'
	Rq -= s == '?'
	if s == 'B' or s == '?':
		ans += (La * f(3, Lq) + Lq * f(3, Lq - 1)) * (Rc * f(3, Rq) + Rq * f(3, Rq - 1))
		ans %= mod
	La += s == 'A'
	Lq += s == '?'
print(ans)


def main():
	S = input()
	mod = 10 ** 9 + 7
	A = AB = ABC = 0
	n = 1
	for i in S:
		if i == '?':
			ABC = ABC * 3 + AB
			AB = AB * 3 + A
			A = A * 3 + n
			n = n * 3 % mod
		if i == 'C':
			ABC = ABC + AB
		if i == 'B':
			AB = AB + A
		if i == 'A':
			A += n
	print(ABC % mod)
main()


S = input()
a = 0
b = 0
c = 0
p = 1
mod = 1000000000 + 7
for C in S:
	if C == '?':
		c = 3 * c + b
		b = 3 * b + a
		a = 3 * a + p
		p *= 3
	elif C == 'A':
		a += p
	elif C == 'B':
		b += a
	elif C == 'C':
		c += b
	a %= mod
	b %= mod
	c %= mod
	p %= mod
print(c)


s = input()
dp = [[0 for _ in range(4)] for _ in range(len(s))]
mod = 10 ** 9 + 7
power_3 = 1
for (idx, letter) in enumerate(s):
	dp[idx] = dp[idx - 1].copy()
	if letter == 'A':
		dp[idx][1] += power_3
	elif letter == 'B':
		dp[idx][2] += dp[idx - 1][1]
	elif letter == 'C':
		dp[idx][3] += dp[idx - 1][2]
	elif letter == '?':
		dp[idx][1] *= 3
		dp[idx][2] *= 3
		dp[idx][3] *= 3
		dp[idx][1] += power_3
		power_3 *= 3
		power_3 %= mod
		dp[idx][2] += dp[idx - 1][1]
		dp[idx][3] += dp[idx - 1][2]
		dp[idx][1] %= mod
		dp[idx][2] %= mod
		dp[idx][3] %= mod
print(dp[len(s) - 1][3] % mod)


mod = 1000000007
s = input()
x = len(s)
dp = [[0 for i in range(4)] for j in range(100005)]
for i in range(x, -1, -1):
	for j in range(3, -1, -1):
		if i == x:
			dp[i][j] = [0, 1][j == 3]
		else:
			dp[i][j] = dp[i + 1][j] * [1, 3][s[i] == '?']
			if j < 3 and (s[i] == '?' or s[i] == 'ABC'[j]):
				dp[i][j] += dp[i + 1][j + 1]
			dp[i][j] %= mod
print(dp[0][0])


MOD = 10 ** 9 + 7
S = input()
N = len(S)
(Acum, Ccum, Qcum) = ([0] * (N + 1), [0] * (N + 1), [0] * (N + 1))
for i in range(N):
	Acum[i + 1] = Acum[i] + 1 if S[i] == 'A' else Acum[i]
	Ccum[i + 1] = Ccum[i] + 1 if S[i] == 'C' else Ccum[i]
	Qcum[i + 1] = Qcum[i] + 1 if S[i] == '?' else Qcum[i]
ans = 0
for i in range(N):
	if S[i] != 'B' and S[i] != '?':
		continue
	(a, c, l, r) = (Acum[i], Ccum[N] - Ccum[i + 1], Qcum[i], Qcum[N] - Qcum[i + 1])
	res = pow(3, l + r - 2, MOD) * (3 * a + l) % MOD * (3 * c + r) % MOD
	ans = (ans + res) % MOD
print(ans)


S = input()
MOD = 10 ** 9 + 7
dp = [1, 0, 0, 0]
for s in S:
	if s == 'A':
		dp[1] += dp[0]
	elif s == 'B':
		dp[2] += dp[1]
	elif s == 'C':
		dp[3] += dp[2]
	elif s == '?':
		dp[3] *= 3
		dp[3] += dp[2]
		dp[2] *= 3
		dp[2] += dp[1]
		dp[1] *= 3
		dp[1] += dp[0]
		dp[0] *= 3
	for i in range(4):
		dp[i] %= MOD
print(dp[3])


mod = 10 ** 9 + 7
dp = [0, 0, 0, 1]
for s in input()[::-1]:
	tmp = dp[:]
	dp[3] = tmp[3] * 3 if s == '?' else tmp[3]
	dp[3] %= mod
	for j in range(2, -1, -1):
		dp[j] = tmp[j] * 3 + tmp[j + 1] if s == '?' else tmp[j] + tmp[j + 1] if s == 'ABC'[j] else tmp[j]
		dp[j] %= mod
print(dp[0] % (10 ** 9 + 7))


s = input()
n = len(s)
mod = 10 ** 9 + 7
qcnt = [[0, 0] for i in range(n)]
Acnt = [0] * n
Ccnt = [0] * n
q = 0
for i in range(n - 1):
	if s[i] == '?':
		q += 1
		qcnt[i + 1][0] += 1
	elif s[i] == 'A':
		Acnt[i + 1] += 1
	qcnt[i + 1][0] += qcnt[i][0]
	Acnt[i + 1] += Acnt[i]
	qcnt[i + 1][0] %= mod
	Acnt[i + 1] %= mod
if s[-1] == '?':
	q += 1
for i in range(n - 1, 0, -1):
	if s[i] == '?':
		qcnt[i - 1][1] += 1
	elif s[i] == 'C':
		Ccnt[i - 1] += 1
	qcnt[i - 1][1] += qcnt[i][1]
	Ccnt[i - 1] += Ccnt[i]
	qcnt[i - 1][1] %= mod
	Ccnt[i - 1] %= mod
ans = 0
for i in range(n):
	if s[i] == 'B' or s[i] == '?':
		if qcnt[i][0] + qcnt[i][1] - 2 < 0:
			val = -(qcnt[i][0] + qcnt[i][1] - 2)
			p = pow(3, val, mod)
			q = pow(p, mod - 2, mod)
			ans += q % mod * (3 * Acnt[i] + qcnt[i][0]) * (3 * Ccnt[i] + qcnt[i][1]) % mod
		else:
			ans += pow(3, qcnt[i][0] + qcnt[i][1] - 2, mod) % mod * (3 * Acnt[i] + qcnt[i][0]) * (3 * Ccnt[i] + qcnt[i][1]) % mod
		ans %= mod
print(ans % mod)


Mod = 10 ** 9 + 7
S = input()
N = len(S)
O = 1
A = [0] * N
AB = [0] * N
ABC = [0] * N
for i in range(N):
	A[i] = A[i - 1]
	AB[i] = AB[i - 1]
	ABC[i] = ABC[i - 1]
	if S[i] == 'A':
		A[i] += O
	if S[i] == 'B':
		AB[i] += A[i - 1]
	if S[i] == 'C':
		ABC[i] += AB[i - 1]
	if S[i] == '?':
		A[i] *= 3
		A[i] += O
		AB[i] *= 3
		AB[i] += A[i - 1]
		ABC[i] *= 3
		ABC[i] += AB[i - 1]
		O *= 3
	A[i] %= Mod
	AB[i] %= Mod
	ABC[i] %= Mod
	O %= Mod
print(ABC[-1])


p = [1, 0, 0, 0]
m = 1000000000.0 + 7

def mod(x):
	return int(x % m)
for s in input():
	if s == 'A':
		p[1] = mod(p[0] + p[1])
	if s == 'B':
		p[2] = mod(p[1] + p[2])
	if s == 'C':
		p[3] = mod(p[2] + p[3])
	if s == '?':
		p[3] = mod(p[2] + p[3] * 3)
		p[2] = mod(p[1] + p[2] * 3)
		p[1] = mod(p[0] + p[1] * 3)
		p[0] = mod(p[0] * 3)
print(int(p[3] % m))


s = input()
dp = [[0] * (len(s) + 1) for _ in [0] * 4]
dp[0][0] = 1
mod = 10 ** 9 + 7
for (i, (c, n0, n1, n2, n3)) in enumerate(zip(s, dp[0], dp[1], dp[2], dp[3]), start=1):
	m = 3 if c == '?' else 1
	dp[0][i] = n0 * m % mod
	dp[1][i] = (n1 * m + n0 * (c == 'A' or c == '?')) % mod
	dp[2][i] = (n2 * m + n1 * (c == 'B' or c == '?')) % mod
	dp[3][i] = (n3 * m + n2 * (c == 'C' or c == '?')) % mod
print(dp[-1][-1])


s = input()
n = len(s)
p = 10 ** 9 + 7
q = s.count('?')
l_a = [0] * n
l_x = [0] * n
r_c = [0] * n
r_x = [0] * n
for i in range(1, n):
	l_a[i] = l_a[i - 1] + int(s[i - 1] == 'A')
	l_x[i] = l_x[i - 1] + int(s[i - 1] == '?')
	r_c[n - i - 1] = r_c[n - i] + int(s[n - i] == 'C')
	r_x[n - i - 1] = r_x[n - i] + int(s[n - i] == '?')
ans = 0
pow_q = []
for i in range(0, 4):
	pow_q.append(max(1, 3 ** (q - i) % p))
for i in range(n):
	if s[i] == 'B':
		ans += l_a[i] * r_c[i] % p * pow_q[0] % p
		ans += l_x[i] * r_c[i] % p * pow_q[1] % p
		ans += l_a[i] * r_x[i] % p * pow_q[1] % p
		ans += l_x[i] * r_x[i] % p * pow_q[2] % p
		ans %= p
	elif s[i] == '?':
		ans += l_a[i] * r_c[i] % p * pow_q[1] % p
		ans += l_x[i] * r_c[i] % p * pow_q[2] % p
		ans += l_a[i] * r_x[i] % p * pow_q[2] % p
		ans += l_x[i] * r_x[i] % p * pow_q[3] % p
		ans %= p
print(ans)


S = input()
if S == '?' * 10 ** 5:
	print(590369788)
else:
	dp = [0, 0, 0, 1]
	for s in S[::-1]:
		tmp = dp[:]
		dp[3] = tmp[3] * 3 if s == '?' else tmp[3]
		for j in range(2, -1, -1):
			dp[j] = tmp[j] * 3 + tmp[j + 1] if s == '?' else tmp[j] + tmp[j + 1] if s == 'ABC'[j] else tmp[j]
	print(dp[0] % (10 ** 9 + 7))


S = input()
N = len(S)
MOD = 1000000007
dp = [[0] * 4 for i in range(N + 1)]
for i in range(N, -1, -1):
	for j in range(3, -1, -1):
		if i == N:
			dp[i][j] = 1 if j == 3 else 0
		else:
			dp[i][j] = dp[i + 1][j] * (3 if S[i] == '?' else 1)
			if j < 3 and (S[i] == '?' or S[i] == 'ABC'[j]):
				dp[i][j] += dp[i + 1][j + 1]
			dp[i][j] %= MOD
print(dp[0][0])


S = input()
lis = [1, 0, 0, 0]
nlis = [1, 0, 0, 0]
for s in S:
	if s == '?':
		nlis[0] += lis[0] * 2
		nlis[1] += lis[1] * 2
		nlis[2] += lis[2] * 2
		nlis[3] += lis[3] * 2
	if s == 'A' or s == '?':
		nlis[1] += lis[0]
	if s == 'B' or s == '?':
		nlis[2] += lis[1]
	if s == 'C' or s == '?':
		nlis[3] += lis[2]
	lis = []
	for i in range(4):
		lis.append(nlis[i] % (10 ** 9 + 7))
print(lis[3] % (10 ** 9 + 7))


box = [1, 0, 0, 0]
for c in input():
	if c == 'A':
		box[1] = box[0] + box[1]
	elif c == 'B':
		box[2] = box[1] + box[2]
	elif c == 'C':
		box[3] = box[2] + box[3]
	elif c == '?':
		box = [box[0] * 3, box[1] * 3 + box[0], box[2] * 3 + box[1], box[3] * 3 + box[2]]
	box = [i % (10 ** 9 + 7) for i in box]
print(box[3])


s = input()
mod = 1000000007
dp = [1, 0, 0, 0]
for c in s:
	newdp = [n for n in dp]
	if c == 'A':
		newdp[1] = (newdp[1] + dp[0]) % mod
	if c == 'B':
		newdp[2] = (newdp[2] + dp[1]) % mod
	if c == 'C':
		newdp[3] = (newdp[3] + dp[2]) % mod
	if c == '?':
		newdp[0] = newdp[0] * 3 % mod
		newdp[1] = (newdp[1] * 3 + dp[0]) % mod
		newdp[2] = (newdp[2] * 3 + dp[1]) % mod
		newdp[3] = (newdp[3] * 3 + dp[2]) % mod
	dp = newdp
print(dp[3])


S = input()
emp = 1
A = 0
AB = 0
ABC = 0
big = 10 ** 9 + 7
for a in S:
	if a == 'A':
		(emp, A, AB, ABC) = (emp, emp + A, AB, ABC)
	if a == 'B':
		(emp, A, AB, ABC) = (emp, A, A + AB, ABC)
	if a == 'C':
		(emp, A, AB, ABC) = (emp, A, AB, AB + ABC)
	if a == '?':
		(emp, A, AB, ABC) = (3 * emp % big, (emp + 3 * A) % big, (A + 3 * AB) % big, (AB + 3 * ABC) % big)
print(ABC % big)


D = [0, 0, 0, 1, 0]
for s in input():
	D = [(D[j] * ((s == '?') * 2 + 1) + D[j + 1] * (s in t + '?')) % (10 ** 9 + 7) for (j, t) in enumerate('CBA.')] + [0]
print(D[0])


s = input()
dp = [[0] * (len(s) + 1) for _ in range(4)]
dp[0][0] = 1
for (i, c) in enumerate(s):
	if c == 'A' or c == '?':
		dp[1][i + 1] = dp[0][i]
	if c == 'B' or c == '?':
		dp[2][i + 1] = dp[1][i]
	if c == 'C' or c == '?':
		dp[3][i + 1] = dp[2][i]
	for j in range(4):
		if c == '?':
			dp[j][i + 1] += dp[j][i] * 3
		else:
			dp[j][i + 1] += dp[j][i]
		dp[j][i + 1] %= 10 ** 9 + 7
print(dp[-1][-1])


def inpl():
	return list(map(int, input().split()))
MOD = 10 ** 9 + 7
S = input()
(A, B, C) = (0, 0, 0)
q = 0
for (i, s) in enumerate(S, start=1):
	if s == 'A':
		A = (A + pow(3, q, MOD)) % MOD
	elif s == 'B':
		B = (B + A) % MOD
	elif s == 'C':
		C = (C + B) % MOD
	elif s == '?':
		(A, B, C) = ((3 * A + pow(3, q, MOD)) % MOD, (3 * B + A) % MOD, (3 * C + B) % MOD)
		q += 1
print(C)


from sys import stdin

def main():
	readline = stdin.readline
	s = readline().strip()
	mod = 10 ** 9 + 7
	n = len(s)
	dp = [[0] * 4 for _ in range(n + 1)]
	dp[0][0] = 1
	for i in range(1, n + 1):
		for j in range(4):
			if s[i - 1] != '?':
				dp[i][j] += dp[i - 1][j]
			else:
				dp[i][j] += dp[i - 1][j] * 3
		if s[i - 1] == 'A':
			dp[i][1] += dp[i - 1][0]
		elif s[i - 1] == 'B':
			dp[i][2] += dp[i - 1][1]
		elif s[i - 1] == 'C':
			dp[i][3] += dp[i - 1][2]
		elif s[i - 1] == '?':
			dp[i][1] += dp[i - 1][0]
			dp[i][2] += dp[i - 1][1]
			dp[i][3] += dp[i - 1][2]
		for j in range(4):
			dp[i][j] %= mod
	print(dp[n][3])

def __starting_point():
	main()
__starting_point()


s = input()
table = [1] + [0 for _ in range(3)]
abc = 'ABC'
mod = 10 ** 9 + 7
for i in range(len(s)):
	if s[i] == '?':
		for j in range(3, 0, -1):
			table[j] = (3 * table[j] + table[j - 1]) % mod
		table[0] = 3 * table[0] % mod
	else:
		for j in range(3):
			if s[i] == abc[j]:
				table[j + 1] += table[j]
print(table[3] % mod)


s = input()
n_a = [0]
n_l = [0]
n_c = [0]
n_r = [0]
mod = 10 ** 9 + 7
n = len(s)
pow3 = [1]
for i in range(n):
	pow3.append(pow3[-1] * 3 % mod)
for i in range(n):
	if s[i] == 'A':
		n_a.append(n_a[-1] + 1)
	else:
		n_a.append(n_a[-1])
	if s[i] == '?':
		n_l.append(n_l[-1] + 1)
	else:
		n_l.append(n_l[-1])
	if s[-i - 1] == 'C':
		n_c.append(n_c[-1] + 1)
	else:
		n_c.append(n_c[-1])
	if s[-i - 1] == '?':
		n_r.append(n_r[-1] + 1)
	else:
		n_r.append(n_r[-1])
ans = 0
for i in range(1, n - 1):
	if s[i] in ['B', '?']:
		ans += n_a[i] * n_c[n - 1 - i] * pow3[n_l[i]] * pow3[n_r[n - 1 - i]] % mod
		ans %= mod
		ans += n_l[i] * n_c[n - 1 - i] * pow3[n_l[i] - 1] * pow3[n_r[n - 1 - i]] % mod
		ans %= mod
		ans += n_a[i] * n_r[n - 1 - i] * pow3[n_l[i]] * pow3[n_r[n - 1 - i] - 1] % mod
		ans %= mod
		ans += n_l[i] * n_r[n - 1 - i] * pow3[n_l[i] - 1] * pow3[n_r[n - 1 - i] - 1] % mod
		ans %= mod
print(ans)


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


s = input()
n = len(s)
la = [0 for _ in range(n)]
rc = [0 for _ in range(n)]
lq = [0 for _ in range(n)]
rq = [0 for _ in range(n)]
for i in range(n - 1):
	if s[i] == 'A':
		la[i + 1] = 1
	if s[i] == '?':
		lq[i + 1] = 1
	if s[i + 1] == '?':
		rq[i] = 1
	if s[i + 1] == 'C':
		rc[i] = 1
for i in range(n - 1):
	la[i + 1] += la[i]
	lq[i + 1] += lq[i]
for i in range(n - 1, 0, -1):
	rc[i - 1] += rc[i]
	rq[i - 1] += rq[i]
ans = 0
mod = 10 ** 9 + 7
for i in range(n):
	if s[i] in ['B', '?']:
		l = la[i] * pow(3, lq[i], mod)
		if lq[i] > 0:
			l += lq[i] * pow(3, lq[i] - 1, mod)
			l %= mod
		r = rc[i] * pow(3, rq[i], mod)
		if rq[i] > 0:
			r += rq[i] * pow(3, rq[i] - 1, mod)
			r %= mod
		ans += l * r
		ans %= mod
print(ans)


S = input()
N = len(S)
MOD = 10 ** 9 + 7
DP = [[1] * 4 for _ in range(N + 1)]
DP[N][3] = 1
DP[N][0] = 0
DP[N][1] = 0
DP[N][2] = 0
for i in range(N - 1, -1, -1):
	for j in range(3, -1, -1):
		DP[i][j] = DP[i + 1][j] * (3 if S[i] == '?' else 1)
		if j < 3:
			DP[i][j] += DP[i + 1][j + 1] * (1 if S[i] == 'ABC'[j] or S[i] == '?' else 0)
		DP[i][j] %= MOD
ans = DP[0][0]
print(ans)


s = input()
MOD = 10 ** 9 + 7
dp = [1, 0, 0, 0]
for x in s:
	(emp, a, b, c) = dp
	if x == 'A':
		dp[1] += emp
	if x == 'B':
		dp[2] += a
	if x == 'C':
		dp[3] += b
	if x == '?':
		dp = [v * 3 for v in dp]
		dp[1] += emp
		dp[2] += a
		dp[3] += b
	dp = [v % MOD for v in dp]
print(dp[3])


def we_love_abc(S: str) -> int:
	ABC = 'ABC'
	MOD = 1000000007
	dp = [0, 0, 0, 0]
	dp[3] = 1
	for c in reversed(S):
		for j in range(3):
			m1 = 3 if c == '?' else 1
			m2 = 1 if c == '?' or c == ABC[j] else 0
			dp[j] = (m1 * dp[j] + m2 * dp[j + 1]) % MOD
		dp[3] = (3 if c == '?' else 1) * dp[3]
		dp[3] %= MOD
	return dp[0] % MOD
S = input()
ans = we_love_abc(S)
print(ans)


s = input()
dp = [[-1 for _ in range(4)] for _ in range(len(s) + 1)]
dp[len(s)][3] = 1
for i in range(len(s), -1, -1):
	for j in range(3, -1, -1):
		if j < 3:
			dp[len(s)][j] = 0
		if i < len(s):
			dp[i][3] = 3 * dp[i + 1][3] if s[i] == '?' else dp[i + 1][3]
			dp[i][3] %= 1000000000.0 + 7
		if i < len(s) and j < 3:
			m1 = 3 if s[i] == '?' else 1
			m2 = 1 if s[i] == '?' or s[i] == list('ABC')[j] else 0
			dp[i][j] = m1 * dp[i + 1][j] + m2 * dp[i + 1][j + 1]
			dp[i][j] %= 1000000000.0 + 7
print(int(dp[0][0]))


s = input()
n = len(s)
dp = [[0 for i in range(4)] for j in range(len(s) + 1)]
dp[0][0] = 1
string = ['A', 'B', 'C']
num = 10 ** 9 + 7
for i in range(n):
	for j in range(4):
		m = 0
		b = 1
		if '?' == s[i]:
			b = 3
		if j == 0:
			if '?' == s[i]:
				dp[i + 1][j] = dp[i][j] * 3 % num
			else:
				dp[i + 1][j] = dp[i][j] % num
			continue
		if s[i] == string[j - 1] or s[i] == '?':
			m = 1
		dp[i + 1][j] = (m * dp[i][j - 1] + b * dp[i][j]) % num
print(dp[-1][3])


MOD = 10 ** 9 + 7
S = input().rstrip()
A = 0
B = 0
C = 0
X = 1
for s in S:
	if s == '?':
		C = B + C * 3
		B = A + B * 3
		A = X + A * 3
		X *= 3
	else:
		if s == 'A':
			A += X
		if s == 'B':
			B += A
		if s == 'C':
			C += B
	X %= MOD
	A %= MOD
	B %= MOD
	C %= MOD
print(C)


S = input()
a = 0
ab = 0
abc = 0
all = 1
mod = 10 ** 9 + 7
for i in range(len(S)):
	if S[i] == 'A':
		a += all
	elif S[i] == 'B':
		ab += a
	elif S[i] == 'C':
		abc += ab
	elif S[i] == '?':
		abc = 3 * abc + ab
		ab = 3 * ab + a
		a = 3 * a + all
		all *= 3
	a %= mod
	ab %= mod
	abc %= mod
	all %= mod
print(abc)


S = input()
ans = 0
MOD = 10 ** 9 + 7
la = 0
lp = 0
rc = 0
rp = 0
for c in S:
	if c == 'C':
		rc += 1
	if c == '?':
		rp += 1
for c in S:
	if c == 'C':
		rc -= 1
	if c == '?':
		rp -= 1
	if c == '?' or c == 'B':
		if lp == 0:
			l = la * pow(3, lp, MOD)
		else:
			l = la * pow(3, lp, MOD) + lp * pow(3, lp - 1, MOD)
		if rp == 0:
			r = rc * pow(3, rp, MOD)
		else:
			r = rc * pow(3, rp, MOD) + rp * pow(3, rp - 1, MOD)
		ans += l * r
		ans %= MOD
	if c == 'A':
		la += 1
	if c == '?':
		lp += 1
print(ans)


mod = 1000000007
S = input()
n = 1
a = 0
ab = 0
abc = 0
for s in S:
	if s in 'A':
		a += n
	if s in 'B':
		ab += a
	if s == 'C':
		abc += ab
	if s in '?':
		(n, a, ab, abc) = (3 * n, 3 * a + n, 3 * ab + a, 3 * abc + ab)
	n %= mod
	a %= mod
	ab %= mod
	abc %= mod
print(abc % mod)


s = input()
mod = 10 ** 9 + 7
(n, a, b, c) = (1, 0, 0, 0)
for i in s:
	if i == 'A':
		a += n
	if i == 'B':
		b += a
	if i == 'C':
		c += b
	if i == '?':
		(n, a, b, c) = (3 * n, 3 * a + n, 3 * b + a, 3 * c + b)
	n %= mod
	a %= mod
	b %= mod
	c %= mod
print(c)


S = input()
N = len(S)
MOD = 10 ** 9 + 7
dp = [0, 0, 0, 0]
dp[0] = 1
for s in S:
	factor = 3 if s == '?' else 1
	for (j, c) in zip([2, 1, 0], 'CBA'):
		dp[j + 1] = dp[j + 1] * factor % MOD
		if s == c or s == '?':
			dp[j + 1] += dp[j]
	if s == '?':
		dp[0] = dp[0] * 3 % MOD
ans = dp[-1] % MOD
print(ans)


s = input()
dp = [[0 for _ in range(len(s) + 1)] for _ in range(4)]
dp[0][0] = 1
for i in range(0, 4):
	for j in range(1, len(s) + 1):
		if s[j - 1] == '?':
			dp[i][j] += dp[i][j - 1] * 3
		else:
			dp[i][j] += dp[i][j - 1]
		if 0 < i and (s[j - 1] == '?' or s[j - 1] == 'ABC'[i - 1]):
			dp[i][j] += dp[i - 1][j - 1]
		dp[i][j] %= 1000000007
print(dp[3][len(s)])


def main():
	MOD = 10 ** 9 + 7
	S = input()
	N = len(S)
	CHARS = ['A', 'B', 'C']
	dp = [[0 for _ in range(4)] for _ in range(N + 1)]
	dp[N][3] = 1
	for i in range(N - 1, -1, -1):
		a = 3 if S[i] == '?' else 1
		dp[i][3] = a * dp[i + 1][3]
		dp[i][3] %= MOD
	for i in range(N - 1, -1, -1):
		a = 3 if S[i] == '?' else 1
		ch = S[i]
		for j in range(3):
			b = 1 if ch in ['?', CHARS[j]] else 0
			dp[i][j] = a * dp[i + 1][j] + b * dp[i + 1][j + 1]
			dp[i][j] %= MOD
	print(dp[0][0])

def __starting_point():
	main()
__starting_point()