Buggy python submission (verdict: Time Limit Exceeded):
def solve(string):
    s = string.replace("?", "d").lower()
    n = len(s)
    dp = [[0] * (n + 1) for _ in range(4)]
    dp[0][0] = 1
    for i, _s in enumerate(s):
        dp[0][i + 1] = dp[0][i]
        dp[1][i + 1] = dp[1][i]
        dp[2][i + 1] = dp[2][i]
        dp[3][i + 1] = dp[3][i]
        if _s == "a":
            dp[1][i + 1] += dp[0][i]
        if _s == "b":
            dp[2][i + 1] += dp[1][i]
        if _s == "c":
            dp[3][i + 1] += dp[2][i]
        if _s == "d":
            dp[0][i + 1] *= 3
            dp[1][i + 1] *= 3
            dp[1][i + 1] += dp[0][i]
            dp[2][i + 1] *= 3
            dp[2][i + 1] += dp[1][i]
            dp[3][i + 1] *= 3
            dp[3][i + 1] += dp[2][i]
    return str(int(dp[3][-1] % (10**9 + 7)))


if __name__ == '__main__':
    print(solve(input()))


Fixed submission:
def solve(string):
    s = string.replace("?", "d").lower()
    n = len(s)
    dp = [[0] * (n + 1) for _ in range(4)]
    dp[0][0] = 1
    mod = 10**9 + 7
    for i, _s in enumerate(s):
        dp[0][i + 1] = dp[0][i] % mod
        dp[1][i + 1] = dp[1][i] % mod
        dp[2][i + 1] = dp[2][i] % mod
        dp[3][i + 1] = dp[3][i] % mod
        if _s == "a":
            dp[1][i + 1] += dp[0][i]
        if _s == "b":
            dp[2][i + 1] += dp[1][i]
        if _s == "c":
            dp[3][i + 1] += dp[2][i]
        if _s == "d":
            dp[0][i + 1] *= 3
            dp[1][i + 1] *= 3
            dp[1][i + 1] += dp[0][i]
            dp[2][i + 1] *= 3
            dp[2][i + 1] += dp[1][i]
            dp[3][i + 1] *= 3
            dp[3][i + 1] += dp[2][i]
    return str(int(dp[3][-1] % (10**9 + 7)))


if __name__ == '__main__':
    print(solve(input()))

Buggy python submission (verdict: Time Limit Exceeded):
from itertools import accumulate


def solve(string):
    s = string.replace("?", "q").lower()
    n = len(s)
    i_a = []
    i_b = []
    i_c = []
    i_q = []
    for i, _s in enumerate(s):
        exec("i_{}.append({})".format(_s, i))
    s = s[::-1]

    c_b = [1 if _s == "b" else 0 for _s in s]
    c_c = [1 if _s == "c" else 0 for _s in s]
    c_q = [1 if _s == "q" else 0 for _s in s]
    c_b = list(accumulate(c_b))[::-1]
    c_c = list(accumulate(c_c))[::-1]
    c_q = list(accumulate(c_q))[::-1]
    ans = 0
    # ABC
    ans += sum(
        sum(c_c[_b + 1] * 3**len(i_q)
            for _b in i_b
            if _a < _b < n - 1)
        for _a in i_a
        if _a < n - 2)
    # AB?
    ans += sum(
        sum(c_q[_b + 1] * 3**(len(i_q) - 1)
            for _b in i_b
            if _a < _b < n - 1)
        for _a in i_a
        if _a < n - 2)
    # A?C
    ans += sum(
        sum(c_c[_b + 1] * 3**(len(i_q) - 1)
            for _b in i_q
            if _a < _b < n - 1)
        for _a in i_a
        if _a < n - 2)
    # ?BC
    ans += sum(
        sum(c_c[_b + 1] * 3**(len(i_q) - 1)
            for _b in i_b
            if _a < _b < n - 1)
        for _a in i_q
        if _a < n - 2)
    # A??
    ans += sum(
        sum(c_q[_b + 1] * 3**(len(i_q) - 2)
            for _b in i_q
            if _a < _b < n - 1)
        for _a in i_a
        if _a < n - 2)
    # ?B?
    ans += sum(
        sum(c_q[_b + 1] * 3**(len(i_q) - 2)
            for _b in i_b
            if _a < _b < n - 1)
        for _a in i_q
        if _a < n - 2)
    # ??C
    ans += sum(
        sum(c_c[_b + 1] * 3**(len(i_q) - 2)
            for _b in i_q
            if _a < _b < n - 1)
        for _a in i_q
        if _a < n - 2)
    # ???
    ans += sum(
        sum(c_q[_b + 1] * 3**(len(i_q) - 3)
            for _b in i_q
            if _a < _b < n - 1)
        for _a in i_q
        if _a < n - 2)
    return str(int(ans % (10**9 + 7)))


if __name__ == '__main__':
    print(solve(input()))


Fixed submission:
def solve(string):
    s = string.replace("?", "d").lower()
    n = len(s)
    dp = [[0] * (n + 1) for _ in range(4)]
    dp[0][0] = 1
    mod = 10**9 + 7
    for i, _s in enumerate(s):
        dp[0][i + 1] = dp[0][i] % mod
        dp[1][i + 1] = dp[1][i] % mod
        dp[2][i + 1] = dp[2][i] % mod
        dp[3][i + 1] = dp[3][i] % mod
        if _s == "a":
            dp[1][i + 1] += dp[0][i]
        if _s == "b":
            dp[2][i + 1] += dp[1][i]
        if _s == "c":
            dp[3][i + 1] += dp[2][i]
        if _s == "d":
            dp[0][i + 1] *= 3
            dp[1][i + 1] *= 3
            dp[1][i + 1] += dp[0][i]
            dp[2][i + 1] *= 3
            dp[2][i + 1] += dp[1][i]
            dp[3][i + 1] *= 3
            dp[3][i + 1] += dp[2][i]
    return str(int(dp[3][-1] % (10**9 + 7)))


if __name__ == '__main__':
    print(solve(input()))