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

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

for i, s in enumerate(S, start=1):
    if s == "?":
        s = "ABC"
    for j in s:
        dp[i][0] += dp[i-1][0]
        dp[i][1] += dp[i-1][1]
        dp[i][2] += dp[i-1][2]
        dp[i][3] += dp[i-1][3]
        if j == "A":
            dp[i][1] += dp[i-1][0]
            dp[i][1] %= mod
        elif j == "B":
            dp[i][2] += dp[i-1][1]
            dp[i][2] %= mod
        elif j == "C":
            dp[i][3] += dp[i-1][2]
            dp[i][3] %= mod

print(dp[N][3] % mod)

Fixed submission:
S = input()
mod = 10 ** 9 + 7

a, ab, abc, n = 0, 0, 0, 1
for s in S:
    if s == "A":
        a += n
        a %= mod
    elif s == "B":
        ab += a
        ab %= mod
    elif s == "C":
        abc += ab
        abc %= mod
    else:
        a, ab, abc, n = 3 * a + n, 3 * ab + a, 3 * abc + ab, 3 * n
        a %= mod
        ab %= mod
        abc %= mod
        n %= mod

print(abc)

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

dp = [1, 0, 0, 0]
for s in S:
    newDP = [0, 0, 0, 0]
    if s == "?":
        s = "ABC"
    for i in s:
        newDP[0] += dp[0]
        newDP[1] += dp[1]
        newDP[2] += dp[2]
        newDP[3] += dp[3]
        if i == "A":
            newDP[1] += dp[0]
            newDP[1] %= mod
        elif i == "B":
            newDP[2] += dp[1]
            newDP[2] %= mod
        elif i == "C":
            newDP[3] += dp[2]
            newDP[3] %= mod
    dp = newDP

print(dp[3] % mod)

Fixed submission:
S = input()
mod = 10 ** 9 + 7

a, ab, abc, n = 0, 0, 0, 1
for s in S:
    if s == "A":
        a += n
        a %= mod
    elif s == "B":
        ab += a
        ab %= mod
    elif s == "C":
        abc += ab
        abc %= mod
    else:
        a, ab, abc, n = 3 * a + n, 3 * ab + a, 3 * abc + ab, 3 * n
        a %= mod
        ab %= mod
        abc %= mod
        n %= mod

print(abc)

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

dp = [1, 0, 0, 0]
for s in S:
    newDP = [0, 0, 0, 0]
    if s == "?":
        s = "ABC"
    for i in s:
        newDP[0] += dp[0]
        newDP[1] += dp[1]
        newDP[2] += dp[2]
        newDP[3] += dp[3]
        if i == "A":
            newDP[1] += dp[0]
            newDP[1] %= mod
        elif i == "B":
            newDP[2] += dp[1]
            newDP[2] %= mod
        elif i == "C":
            newDP[3] += dp[2]
            newDP[3] %= mod
    dp = newDP

print(dp[3] % mod)

Fixed submission:
S = input()
mod = 10 ** 9 + 7

a, ab, abc, n = 0, 0, 0, 1
for s in S:
    if s == "A":
        a += n
        a %= mod
    elif s == "B":
        ab += a
        ab %= mod
    elif s == "C":
        abc += ab
        abc %= mod
    else:
        a, ab, abc, n = 3 * a + n, 3 * ab + a, 3 * abc + ab, 3 * n
        a %= mod
        ab %= mod
        abc %= mod
        n %= mod

print(abc)

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

dp = {"": 0, "A": 0, "AB": 0, "ABC": 0}
dp[""] = 1

for s in S:
    newDP = {"": 0, "A": 0, "AB": 0, "ABC": 0}
    if s == "?":
        s = "ABC"

    for i in s:
        # 入れない
        newDP[""] += dp[""]
        newDP["A"] += dp["A"]
        newDP["AB"] += dp["AB"]
        newDP["ABC"] += dp["ABC"]
        if i == "A":
            newDP["A"] += dp[""]
            newDP["A"] %= mod
        elif i == "B":
            newDP["AB"] += dp["A"]
            newDP["AB"] %= mod
        else:
            newDP["ABC"] += dp["AB"]
            newDP["ABC"] %= mod
    dp = newDP


print(dp["ABC"])

Fixed submission:
S = input()
mod = 10 ** 9 + 7

a, ab, abc, n = 0, 0, 0, 1
for s in S:
    if s == "A":
        a += n
        a %= mod
    elif s == "B":
        ab += a
        ab %= mod
    elif s == "C":
        abc += ab
        abc %= mod
    else:
        a, ab, abc, n = 3 * a + n, 3 * ab + a, 3 * abc + ab, 3 * n
        a %= mod
        ab %= mod
        abc %= mod
        n %= mod

print(abc)

Buggy python submission (verdict: Time Limit Exceeded):
from collections import defaultdict
S = input()
N = len(S)
mod = 10 ** 9 + 7

dp = defaultdict(int)
dp[""] = 1

for s in S:
    newDP = defaultdict(int)
    if s == "?":
        s = "ABC"

    for i in s:
        # 入れない
        newDP[""] += dp[""]
        newDP["A"] += dp["A"]
        newDP["AB"] += dp["AB"]
        newDP["ABC"] += dp["ABC"]
        if i == "A":
            newDP["A"] += dp[""]
            newDP["A"] %= mod
        elif i == "B":
            newDP["AB"] += dp["A"]
            newDP["AB"] %= mod
        else:
            newDP["ABC"] += dp["AB"]
            newDP["ABC"] %= mod
    dp = newDP


print(dp["ABC"])

Fixed submission:
S = input()
mod = 10 ** 9 + 7

a, ab, abc, n = 0, 0, 0, 1
for s in S:
    if s == "A":
        a += n
        a %= mod
    elif s == "B":
        ab += a
        ab %= mod
    elif s == "C":
        abc += ab
        abc %= mod
    else:
        a, ab, abc, n = 3 * a + n, 3 * ab + a, 3 * abc + ab, 3 * n
        a %= mod
        ab %= mod
        abc %= mod
        n %= mod

print(abc)