Problem p03291 (python)

Fixed submission:
s = input()

ans = 0

rc = [0]
rq = [0]
la = [0]
lq = [0]
q3 = [1]
t = 10**9+7

for i in range(len(s)-1):
    if s[len(s)-i-1]=='C':
        rc.append(rc[-1]+1)
        rq.append(rq[-1])
    elif s[len(s)-i-1]=='?':
        rc.append(rc[-1])
        rq.append(rq[-1]+1)
    else:
        rc.append(rc[-1])
        rq.append(rq[-1])
    if s[i]=='A':
        la.append(la[-1]+1)
        lq.append(lq[-1])
    elif s[i]=='?':
        la.append(la[-1])
        lq.append(lq[-1]+1)
    else:
        la.append(la[-1])
        lq.append(lq[-1])
    q3.append(q3[-1] * 3 % t)
rc.reverse()
rq.reverse()

q3.append(1/3)

for i in range(len(s)):
    if s[i]=='B' or s[i]=='?':
        l = q3[lq[i]-1]
        r = q3[rq[i]-1]
        ans += l*r*(3*la[i]+lq[i])*(3*rc[i]+rq[i]) % t

print(round(ans%t))

Buggy submission (verdict: Runtime Error):
s = input()

ans = 0

q = s.count('?')

rc = [0]
rq = [0]
la = [0]
lq = [0]

for i in range(len(s)-1):
    if s[len(s)-i-1]=='C':
        rc.append(rc[-1]+1)
    else:
        rc.append(rc[-1])
    if s[len(s)-i-1]=='?':
        rq.append(rq[-1] + 1)
    else:
        rq.append(rq[-1])
    if s[i]=='A':
        la.append(la[-1]+1)
    else:
        la.append(la[-1])
    if s[i]=='?':
        lq.append(lq[-1]+1)
    else:
        lq.append(lq[-1])
rc.reverse()
rq.reverse()

t = 10**9+7

for i in range(len(s)):
    if s[i]=='B' or s[i]=='?':
        lq_ = lq[i]-1
        rq_ = rq[i]-1
        l = 1
        r = 1
        while lq_ > 20:
            l *= 3**20%t
            lq -=20
        while rq_ > 20:
            r *= 3**20%t
            rq_ -= 20
        l *= 3**lq_%t
        r *= 3**rq_%t
        l %= t
        r %= t
        ans += l*r*(3*la[i]+lq[i])*(3*rc[i]+rq[i]) % t

print(int(ans%t))

Buggy submission (verdict: Runtime Error):
s = input()

ans = 0

q = s.count('?')

rc = [0]
rq = [0]
la = [0]
lq = [0]

for i in range(len(s)-1):
    if s[len(s)-i-1]=='C':
        rc.append(rc[-1]+1)
    else:
        rc.append(rc[-1])
    if s[len(s)-i-1]=='?':
        rq.append(rq[-1] + 1)
    else:
        rq.append(rq[-1])
    if s[i]=='A':
        la.append(la[-1]+1)
    else:
        la.append(la[-1])
    if s[i]=='?':
        lq.append(lq[-1]+1)
    else:
        lq.append(lq[-1])
rc.reverse()
rq.reverse()

t = 10**9+7

for i in range(len(s)):
    if s[i]=='B' or s[i]=='?':
        lq_ = lq[i]-1
        rq_ = rq[i]-1
        l = 1
        r = 1
        while lq_ > 20:
            l *= 3**20%t
            lq -=20
        while rq_ > 20:
            r *= 3**20%t
            rq_ -= 20
        l *= 3**lq_%t
        r *= 3**rq_%t
        ans += l*r*(3*la[i]+lq[i])*(3*rc[i]+rq[i]) % t

print(int(ans%t))

Buggy submission (verdict: Runtime Error):
s = input()

ans = 0

q = s.count('?')

rc = [0]
rq = [0]
la = [0]
lq = [0]

for i in range(len(s)-1):
    if s[len(s)-i-1]=='C':
        rc.append(rc[-1]+1)
    else:
        rc.append(rc[-1])
    if s[len(s)-i-1]=='?':
        rq.append(rq[-1] + 1)
    else:
        rq.append(rq[-1])
    if s[i]=='A':
        la.append(la[-1]+1)
    else:
        la.append(la[-1])
    if s[i]=='?':
        lq.append(lq[-1]+1)
    else:
        lq.append(lq[-1])
rc.reverse()
rq.reverse()


for i in range(len(s)):
    if s[i]=='B' or s[i]=='?':
        ans += la[i]*(3**lq[i])*rc[i]*(3**rq[i]) % (10**9+7)
        ans += lq[i]*(3**(lq[i]-1))*rc[i]*(3**rq[i]) % (10**9+7)
        ans += la[i]*(3**lq[i])*rq[i]*(3**(rq[i]-1)) % (10**9+7)
        ans += lq[i]*(3**(lq[i]-1))*rq[i]*(3**(rq[i]-1)) % (10**9+7)

print(int(ans%(10**9+7)))

Buggy submission (verdict: Runtime Error):
s = input()

ans = 0

q = s.count('?')

rc = [0]
rq = [0]
la = [0]
lq = [0]

for i in range(len(s)-1):
    if s[len(s)-i-1]=='C':
        rc.append(rc[-1]+1)
    else:
        rc.append(rc[-1])
    if s[len(s)-i-1]=='?':
        rq.append(rq[-1] + 1)
    else:
        rq.append(rq[-1])
    if s[i]=='A':
        la.append(la[-1]+1)
    else:
        la.append(la[-1])
    if s[i]=='?':
        lq.append(lq[-1]+1)
    else:
        lq.append(lq[-1])
rc.reverse()
rq.reverse()


for i in range(len(s)):
    if s[i]=='B' or s[i]=='?':
        ans += la[i]*(3**lq[i])*rc[i]*(3**rq[i]) + lq[i]*(3**(lq[i]-1))*rc[i]*(3**rq[i]) + la[i]*(3**lq[i])*rq[i]*(3**(rq[i]-1)) + lq[i]*(3**(lq[i]-1))*rq[i]*(3**(rq[i]-1))
        ans %= 10**9+7

print(int(ans))