Buggy python submission (verdict: Time Limit Exceeded):

dp = [0, 0, 0, 1]
for s in input()[::-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))


Fixed submission:

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))

Buggy python submission (verdict: Time Limit Exceeded):

S = input()

dp = [0, 0, 0, 1]
for i in range(len(S) - 1, -1, -1):
    tmp = dp[:]

    dp[3] = tmp[3] * 3 if S[i] == '?' else tmp[3]

    for j in range(2, -1, -1):
        dp[j] = tmp[j] * 3 + tmp[j + 1] if S[i] == '?' else \
                tmp[j] + tmp[j + 1] if S[i] == 'ABC'[j] else \
                tmp[j]

print(dp[0] % (10**9 + 7))


Fixed submission:

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))

Buggy python submission (verdict: Time Limit Exceeded):
# -*- coding: utf-8 -*-
S = input()

# 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]

#     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

# print(dp[0][0] % (10**9 + 7))

dp = [0, 0, 0, 1]
for i in range(len(S) - 1, -1, -1):
    tmp = dp[:]

    dp[3] = tmp[3] * 3 if S[i] == '?' else tmp[3]

    for j in range(2, -1, -1):
        dp[j] = tmp[j] * 3 + tmp[j + 1] if S[i] == '?' else tmp[j]
        dp[j] += tmp[j + 1] if S[i] == 'ABC' [j] else 0

print(dp[0] % (10**9 + 7))


Fixed submission:

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))

Buggy python submission (verdict: Time Limit Exceeded):
# -*- coding: utf-8 -*-
S = input()

# 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]

#     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

# print(dp[0][0] % (10**9 + 7))

dp = [0, 0, 0, 1]
for i in range(len(S) - 1, -1, -1):
    tmp = dp[:]

    dp[3] = tmp[3] * 3 if S[i] == '?' else tmp[3]

    for j in range(2, -1, -1):
        dp[j] = tmp[j] * 3 if S[i] == '?' else tmp[j]
        dp[j] += tmp[j + 1] if S[i] == 'ABC' [j] or S[i] == '?' else 0

print(dp[0] % (10**9 + 7))


Fixed submission:

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))

Buggy python submission (verdict: Time Limit Exceeded):
# -*- coding: utf-8 -*-
S = input()
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]

    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

print(dp[0][0] % (10**9 + 7))


Fixed submission:

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))