Buggy python submission (verdict: Runtime Error):
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 % MODE
        if s == c or s == '?':
            dp[j + 1] += dp[j]
    if s == '?':
        dp[0] = dp[0] % MOD

ans = dp[-1] % MOD

print(ans)

Fixed submission:
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)

Buggy python submission (verdict: Time Limit Exceeded):
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] *= factor
        if s == c or s == '?':
            dp[j + 1] += dp[j]
    if s == '?':
        dp[0] *= 3

ans = dp[-1] % MOD

print(ans)

Fixed submission:
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)

Buggy python submission (verdict: Time Limit Exceeded):
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] *= factor
        if s in c + '?':
            dp[j + 1] += dp[j]
    if s == '?':
        dp[0] *= 3

ans = dp[-1] % MOD

print(ans)

Fixed submission:
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)

Buggy python submission (verdict: Time Limit Exceeded):
S = input()
N = len(S)
MOD = 10 ** 9 + 7

q = 3 if S[0] == '?' else 1
a = b = c = 0
def fq():
    global a, b, c, q
    c = c * 3 + b
    b = b * 3 + a
    a = a * 3 + q
    q *= 3
def fa():
    global a
    a += q
def fb():
    global b
    b += a
def fc():
    global c
    c += b
funcs = {'A':fa, 'B': fb, 'C':fc, '?':fq}
if S[0] in 'A?':
    a = 1
for s in S[1:]:
    funcs[s]()
ans = c % MOD
print(ans)

Fixed submission:
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)

Buggy python submission (verdict: Time Limit Exceeded):
S = input()
N = len(S)
MOD = 10 ** 9 + 7

q = 3 if S[0] == '?' else 1
a = b = c = 0
def fq():
    global a, b, c, q
    c = c * 3 + b
    b = b * 3 + a
    a = a * 3 + q
    q *= 3
def fa():
    global a
    a += q
def fb():
    global b
    b += a
def fc():
    global c
    c += b
funcs = {'A':fa, 'B': fb, 'C':fc, '?':fq}
if S[0] in 'A?':
    a = 1
for s in S[1:]:
    funcs[s]()
ans = c % MOD
print(ans)

Fixed submission:
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)

Buggy python submission (verdict: Time Limit Exceeded):
S = input()
N = len(S)
MOD = 10 ** 9 + 7

q = 3 if S[0] == '?' else 1
a = b = c = 0
def fq():
    global a, b, c, q
    c = c * 3 + b
    b = b * 3 + a
    a = a * 3 + q
    q *= 3
def fa():
    global a
    a += q
def fb():
    global b
    b += a
def fc():
    global c
    c += b
funcs = {'A':fa, 'B': fb, 'C':fc, '?':fq}
if S[0] in 'A?':
    a = 1
for s in S[1:]:
    funcs[s]()
ans = c % MOD
print(ans)

Fixed submission:
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)

Buggy python submission (verdict: Time Limit Exceeded):
S = input()
N = len(S)
MOD = 10 ** 9 + 7

q = 3 if S[0] == '?' else 1
a = b = c = 0
def fq(a, b, c, q):
    return a * 3 + q, b * 3 + a, c * 3 + b, q * 3
def fa(a, b, c, q):
    return a + q, b, c, q
def fb(a, b, c, q):
    return a, b + a, c, q
def fc(a, b, c, q):
    return a, b, c + b, q
funcs = {'A':fa, 'B': fb, 'C':fc, '?':fq}
if S[0] in 'A?':
    a = 1
from functools import reduce
ans = reduce(lambda x, y: funcs[y](*x), S[1:], (a, b, c, q))
ans = ans[2] % MOD
print(ans)

Fixed submission:
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)

Buggy python submission (verdict: Time Limit Exceeded):
S = input()
N = len(S)
MOD = 10 ** 9 + 7

q = 3 if S[0] == '?' else 1
a = b = c = 0
def fq(a, b, c, q):
    return a * 3 + q, b * 3 + a, c * 3 + b, q * 3
def fa(a, b, c, q):
    return a + q, b, c, q
def fb(a, b, c, q):
    return a, b + a, c, q
def fc(a, b, c, q):
    return a, b, c + b, q
funcs = {'A':fa, 'B': fb, 'C':fc, '?':fq}
if S[0] in 'A?':
    a = 1
for s in S[1:]:
    a, b, c, q = funcs[s](a, b, c, q)

ans = c % MOD
print(ans)

Fixed submission:
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)

Buggy python submission (verdict: Time Limit Exceeded):
S = input()
N = len(S)
MOD = 10 ** 9 + 7

q = 3 if S[0] == '?' else 1
a = b = c = 0
if S[0] in 'A?':
    a = 1
for s in S[1:]:
    if s == '?':
        c = c * 3 + b
        b = b * 3 + a
        a = a * 3 + q
        q *= 3
    elif s == 'A':
        a += q
    elif s == 'B':
        b += a
    elif s == 'C':
        c += b
    a, b, c = a % MOD, b % MOD, c % MOD

ans = c % MOD
print(ans)

Fixed submission:
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)

Buggy python submission (verdict: Runtime Error):
S = input()
N = len(S)
MOD = 10 ** 9 + 7

q = 3 if S[0] == '?' else 1
a = b = c = 0
if S[0] in 'A?':
    a = 1
for s in S[1:]:
    if s == '?':
        c = c * 3 + b
        b = b * 3 + a
        a = a * 3 + q
        q *= 3
    elif s == 'A':
        a += q
    elif s == 'B':
        b += a
    elif s == 'C':
        c += b
    a, b, c = a % MOD, b % MOD, C % MOD

ans = c % MOD
print(ans)

Fixed submission:
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)

Buggy python submission (verdict: Time Limit Exceeded):
S = input()
N = len(S)
MOD = 10 ** 9 + 7

q = 3 if S[0] == '?' else 1
a = b = c = 0
if S[0] in 'A?':
    a = 1
for s in S[1:]:
    if s == '?':
        c = c * 3 + b
        b = b * 3 + a
        a = a * 3 + q
        q *= 3
    elif s == 'A':
        a += q
    elif s == 'B':
        b += a
    elif s == 'C':
        c += b

ans = c % MOD
print(ans)

Fixed submission:
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)

Buggy python submission (verdict: Time Limit Exceeded):
S = input()
N = len(S)
MOD = 10 ** 9 + 7

q = 3 if S[0] == '?' else 1
a = b = c = 0
if S[0] in 'A?':
    a = 1
for i, s in enumerate(S[1:], 1):
    if s == '?':
        c = c * 3 + b
        b = b * 3 + a
        a = a * 3 + q
        q *= 3
    elif s == 'A':
        a += q
    elif s == 'B':
        b += a
    elif s == 'C':
        c += b

ans = c % MOD
print(ans)

Fixed submission:
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)

Buggy python submission (verdict: Time Limit Exceeded):
S = input()
N = len(S)
MOD = 10 ** 9 + 7

Qs = 3 if S[0] == '?' else 1
A = [0] * N
AB = [0] * N
ABC = [0] * N
if S[0] in 'A?':
    A[0] = 1
a, ab, abc = A[0], AB[0], ABC[0]
for i in range(1, N):
    s = S[i]
    if s == '?':
        A[i] = a * 3 + Qs
        AB[i] = ab * 3 + a
        ABC[i] = abc * 3 + ab
        Qs *= 3
    elif s == 'A':
        A[i] = a + Qs
        AB[i] = ab
        ABC[i] = abc
    elif s == 'B':
        A[i] = a
        AB[i] = ab + a
        ABC[i] = abc
    elif s == 'C':
        A[i] = a
        AB[i] = ab
        ABC[i] = abc + ab
    a, ab, abc = A[i], AB[i], ABC[i]

ans = ABC[-1] % MOD
print(ans)

Fixed submission:
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)

Buggy python submission (verdict: Time Limit Exceeded):
S = input()
N = len(S)
MOD = 10 ** 9 + 7

Qs = 3 if S[0] == '?' else 1
A = [0] * N
AB = [0] * N
ABC = [0] * N
if S[0] in 'A?':
    A[0] = 1
for i in range(1, N):
    s = S[i]
    if s == '?':
        A[i] = A[i - 1] * 3 + Qs
        AB[i] = AB[i - 1] * 3 + A[i - 1]
        ABC[i] = ABC[i - 1] * 3 + AB[i - 1]
        Qs *= 3
    elif s == 'A':
        A[i] = A[i - 1] + Qs
        AB[i] = AB[i - 1]
        ABC[i] = ABC[i - 1]
    elif s == 'B':
        A[i] = A[i - 1]
        AB[i] = AB[i - 1] + A[i - 1]
        ABC[i] = ABC[i - 1]
    elif s == 'C':
        A[i] = A[i - 1]
        AB[i] = AB[i - 1]
        ABC[i] = ABC[i - 1] + AB[i - 1]

ans = ABC[-1] % MOD
print(ans)

Fixed submission:
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)

Buggy python submission (verdict: Time Limit Exceeded):
S = input()
N = len(S)
MOD = 10 ** 9 + 7

Qs = 3 if S[0] == '?' else 1
A = [0] * N
AB = [0] * N
ABC = [0] * N
if S[0] in 'A?':
    A[0] = 1
for i, s in enumerate(S[1:], 1):
    if s == '?':
        A[i] = A[i - 1] * 3 + Qs
        AB[i] = AB[i - 1] * 3 + A[i - 1]
        ABC[i] = ABC[i - 1] * 3 + AB[i - 1]
        Qs *= 3
    elif s == 'A':
        A[i] = A[i - 1] + Qs
        AB[i] = AB[i - 1]
        ABC[i] = ABC[i - 1]
    elif s == 'B':
        A[i] = A[i - 1]
        AB[i] = AB[i - 1] + A[i - 1]
        ABC[i] = ABC[i - 1]
    elif s == 'C':
        A[i] = A[i - 1]
        AB[i] = AB[i - 1]
        ABC[i] = ABC[i - 1] + AB[i - 1]

ans = ABC[-1] % MOD
print(ans)

Fixed submission:
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)

Buggy python submission (verdict: Time Limit Exceeded):
S = input()
N = len(S)
MOD = 10 ** 9 + 7

Qs = 3 if S[0] == '?' else 1
A = [0] * N
AB = [0] * N
ABC = [0] * N
if S[0] in 'A?':
    A[0] = 1
for i, s in enumerate(S[1:], 1):
    factor = 3 if s == '?' else 1
    A[i] = A[i - 1] * factor % MOD
    AB[i] = AB[i - 1] * factor % MOD
    ABC[i] = ABC[i - 1] * factor % MOD
    if s in 'A?':
        A[i] += Qs
    if s in 'B?':
        AB[i] += A[i - 1]
    if s in 'C?':
        ABC[i] += AB[i - 1]
    if s == '?':
        Qs *= 3

ans = ABC[-1] % MOD
print(ans)

Fixed submission:
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)