Buggy python submission (verdict: Runtime Error):
#include <iostream>
#include <string>
#define ll long long
using namespace std;

const int MOD = 1000000007;

int main() {
  string S;
  cin >> S;

  int len = S.size();
  ll a = 0, ab = 0, abc = 0, all_s = 1;

  for (int i = 0; i < len; i++) {
    if (S[i] == 'A')
      a = (a + all_s) % MOD;
    else if (S[i] == 'B')
      ab = (ab + a) % MOD;
    else if (S[i] == 'C')
      abc = (abc + ab) % MOD;
    else if (S[i] == '?') {
      abc   = (abc * 3 + ab) % MOD;
      ab    = (ab * 3 + a) % MOD;
      a     = (a * 3 + all_s) % MOD;
      all_s = (all_s * 3) % MOD;
    }
  }
  cout << abc << endl;
}

Fixed submission:
MOD = 1000000007

S = input()

a, ab, abc, all_s = 0, 0, 0, 1

for s in S:
    if s == "A":
        a = (a + all_s) % MOD
    elif s == "B":
        ab = (ab + a) % MOD
    elif s == "C":
        abc = (abc + ab) % MOD
    elif s == "?":
        abc = (abc * 3 + ab) % MOD
        ab = (ab * 3 + a) % MOD
        a = (a * 3 + all_s) % MOD
        all_s = (all_s * 3) % MOD

print(abc)

Buggy python submission (verdict: Time Limit Exceeded):
MOD = 1000000007

S = input()

a, ab, abc, all_s = 0, 0, 0, 1

for s in S:
    if s == "A":
        a += all_s
    elif s == "B":
        ab += a
    elif s == "C":
        abc += ab
    elif s == "?":
        abc = abc * 3 + ab
        ab = ab * 3 + a
        a = a * 3 + all_s
        all_s = all_s * 3

print(abc % MOD)

Fixed submission:
MOD = 1000000007

S = input()

a, ab, abc, all_s = 0, 0, 0, 1

for s in S:
    if s == "A":
        a = (a + all_s) % MOD
    elif s == "B":
        ab = (ab + a) % MOD
    elif s == "C":
        abc = (abc + ab) % MOD
    elif s == "?":
        abc = (abc * 3 + ab) % MOD
        ab = (ab * 3 + a) % MOD
        a = (a * 3 + all_s) % MOD
        all_s = (all_s * 3) % MOD

print(abc)

Buggy python submission (verdict: Time Limit Exceeded):
MOD = 1000000007

S = input()

a = 0
ab = 0
abc = 0
all_s = 1

for s in S:
    if s == "A":
        a += all_s
    elif s == "B":
        ab += a
    elif s == "C":
        abc += ab
    elif s == "?":
        abc = abc * 3 + ab % MOD
        ab = ab * 3 + a % MOD
        a = a * 3 + all_s % MOD
        all_s = all_s * 3 % MOD

print(abc % MOD)


Fixed submission:
MOD = 1000000007

S = input()

a, ab, abc, all_s = 0, 0, 0, 1

for s in S:
    if s == "A":
        a = (a + all_s) % MOD
    elif s == "B":
        ab = (ab + a) % MOD
    elif s == "C":
        abc = (abc + ab) % MOD
    elif s == "?":
        abc = (abc * 3 + ab) % MOD
        ab = (ab * 3 + a) % MOD
        a = (a * 3 + all_s) % MOD
        all_s = (all_s * 3) % MOD

print(abc)

Buggy python submission (verdict: Time Limit Exceeded):
MOD = 1000000007

S = input()

a = 0
ab = 0
abc = 0
all_s = 1

for i, s in enumerate(S):
    if s == "A":
        a += all_s
    elif s == "B":
        ab += a
    elif s == "C":
        abc += ab
    elif s == "?":
        abc = abc * 3 + ab
        ab = ab * 3 + a
        a = a * 3 + all_s
        all_s *= 3

print(abc % MOD)


Fixed submission:
MOD = 1000000007

S = input()

a, ab, abc, all_s = 0, 0, 0, 1

for s in S:
    if s == "A":
        a = (a + all_s) % MOD
    elif s == "B":
        ab = (ab + a) % MOD
    elif s == "C":
        abc = (abc + ab) % MOD
    elif s == "?":
        abc = (abc * 3 + ab) % MOD
        ab = (ab * 3 + a) % MOD
        a = (a * 3 + all_s) % MOD
        all_s = (all_s * 3) % MOD

print(abc)