Buggy python submission (verdict: Time Limit Exceeded):
ABC = 'ABC'
S = list(input())

dp = [0, 0, 0, 0]
dp[3] = 1

for i in range(len(S))[::-1]:
    c = S[i]
    if c == '?':
        dp[0] = 3 * dp[0] + dp[1]
        dp[1] = 3 * dp[1] + dp[2]
        dp[2] = 3 * dp[2] + dp[3]
        dp[3] = 3 * dp[3]
    elif c == 'A':
        dp[0] = 1 * dp[0] + dp[1]
        dp[1] = 1 * dp[1]
        dp[2] = 1 * dp[2]
        dp[3] = 1 * dp[3]
    elif c == 'B':
        dp[0] = 1 * dp[0]
        dp[1] = 1 * dp[1] + dp[2]
        dp[2] = 1 * dp[2]
        dp[3] = 1 * dp[3]
    elif c == 'C':
        dp[0] = 1 * dp[0]
        dp[1] = 1 * dp[1]
        dp[2] = 1 * dp[2] + dp[3]
        dp[3] = 1 * dp[3]

print(dp[0] % 1000000007)


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


if __name__ == "__main__":
    S = input()
    ans = we_love_abc(S)
    print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
ABC = 'ABC'
S = input()

dp = [0, 0, 0, 0]
dp[3] = 1

for i in range(len(S))[::-1]:
    c = S[i]
    if c == '?':
        dp[0] = 3 * dp[0] + dp[1]
        dp[1] = 3 * dp[1] + dp[2]
        dp[2] = 3 * dp[2] + dp[3]
        dp[3] = 3 * dp[3]
    elif c == 'A':
        dp[0] = 1 * dp[0] + dp[1]
        dp[1] = 1 * dp[1]
        dp[2] = 1 * dp[2]
        dp[3] = 1 * dp[3]
    elif c == 'B':
        dp[0] = 1 * dp[0]
        dp[1] = 1 * dp[1] + dp[2]
        dp[2] = 1 * dp[2]
        dp[3] = 1 * dp[3]
    elif c == 'C':
        dp[0] = 1 * dp[0]
        dp[1] = 1 * dp[1]
        dp[2] = 1 * dp[2] + dp[3]
        dp[3] = 1 * dp[3]

print(dp[0] % 1000000007)


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


if __name__ == "__main__":
    S = input()
    ans = we_love_abc(S)
    print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
ABC = 'ABC'
S = input()

dp = [0, 0, 0, 0]
dp[3] = 1

for i in range(len(S)-1, -1, -1):
    c = S[i]
    if c == '?':
        m, m1 = 3, 3
    else:
        m, m1 = 1, 1

    for j in range(3):
        m2 = 1 if c == '?' or c == ABC[j] else 0
        dp[j] = m1 * dp[j] + m2 * dp[j+1]
    dp[3] = m * dp[3]

print(dp[0] % 1000000007)


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


if __name__ == "__main__":
    S = input()
    ans = we_love_abc(S)
    print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
S = input()


ABC = 'ABC'

dp = [0, 0, 0, 0]
dp[3] = 1

for c in reversed(S):
    if c == '?':
        m, m1 = 3, 3
    else:
        m, m1 = 1, 1

    for j in range(3):
        m2 = 1 if c == '?' or c == ABC[j] else 0
        dp[j] = m1 * dp[j] + m2 * dp[j+1]
    dp[3] = m * dp[3]

print(dp[0] % 1000000007)


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


if __name__ == "__main__":
    S = input()
    ans = we_love_abc(S)
    print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
S = ''


def we_love_abc()->int:
    global S
    ABC = 'ABC'

    dp = [0, 0, 0, 0]
    dp[3] = 1

    for c in reversed(S):
        if c == '?':
            m, m1 = 3, 3
        else:
            m, m1 = 1, 1

        for j in range(3):
            m2 = 1 if c == '?' or c == ABC[j] else 0
            dp[j] = m1 * dp[j] + m2 * dp[j+1]
        dp[3] = m * dp[3]

    return dp[0] % 1000000007


if __name__ == "__main__":
    S = input()
    ans = we_love_abc()  # S)
    print(ans)


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


if __name__ == "__main__":
    S = input()
    ans = we_love_abc(S)
    print(ans)

Buggy python submission (verdict: Runtime Error):
S = ''

def we_love_abc()->int:
    global S
    ABC = 'ABC'

    dp = [0, 0, 0, 0]
    dp[3] = 1

    for c in reversed(S):
        if c == '?':
            m, m1 = 3, 3
        else:
            m, m1 = 1, 1

        for j in range(3):
            m2 = 1 if c == '?' or c == ABC[j] else 0
            dp[j] = m1 * dp[j] + m2 * dp[j+1]
        dp[3] = m * dp[3]

    return dp[0] % 1000000007


if __name__ == "__main__":
    S = input()
    ans = we_love_abc(S)
    print(ans)


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


if __name__ == "__main__":
    S = input()
    ans = we_love_abc(S)
    print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
def we_love_abc(S: str)->int:
    ABC = 'ABC'

    dp = [0, 0, 0, 0]
    dp[3] = 1

    for c in reversed(S):
        if c == '?':
            m, m1 = 3, 3
        else:
            m, m1 = 1, 1

        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]
        # dp[3] = (3 if c == '?' else 1) * dp[3]
        dp[3] = m * dp[3]

    return dp[0] % 1000000007


if __name__ == "__main__":
    S = input()
    ans = we_love_abc(S)
    print(ans)


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


if __name__ == "__main__":
    S = input()
    ans = we_love_abc(S)
    print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
def we_love_abc(S: str)->int:
    ABC = 'ABC'

    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]
        dp[3] = (3 if c == '?' else 1) * dp[3]

    return dp[0] % 1000000007


if __name__ == "__main__":
    S = input()
    ans = we_love_abc(S)
    print(ans)


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


if __name__ == "__main__":
    S = input()
    ans = we_love_abc(S)
    print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
def we_love_abc(S: str)->int:
    ABC = 'ABC'

    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]
        dp[3] = (3 if c == '?' else 1) * dp[3]

    return dp[0] % 1000000007


if __name__ == "__main__":
    S = input()
    ans = we_love_abc(S)
    print(ans)


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


if __name__ == "__main__":
    S = input()
    ans = we_love_abc(S)
    print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
def we_love_abc(S: str)->int:
    n = len(S)
    dp = [[0, 0, 0, 0] for _ in range(n + 1)]
    dp[n][3] = 1

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

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


if __name__ == "__main__":
    S = input()
    ans = we_love_abc(S)
    print(ans)


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


if __name__ == "__main__":
    S = input()
    ans = we_love_abc(S)
    print(ans)