Problem p03051 (python)

Fixed submission:
import sys
from collections import defaultdict
from itertools import accumulate
from operator import xor


def solve_0(acc):
    MOD = 10 ** 9 + 7
    cnt_0 = 0
    counts = defaultdict(list)
    last_0 = {}
    for a in acc:
        if a == 0:
            cnt_0 += 1
        else:
            cnt = counts[a]
            if len(cnt) == 0:
                cnt.append(1)
                last_0[a] = cnt_0
            else:
                btw_0 = cnt_0 - last_0[a]
                if btw_0 > 0:
                    cnt.append(btw_0)
                    cnt.append(1)
                    last_0[a] = cnt_0
                else:
                    cnt[-1] += 1

    ans = pow(2, cnt_0 - 1, MOD)
    for i, cnt in counts.items():
        dp0 = 1
        dp1 = cnt[0]
        for c0, c1 in zip(cnt[1::2], cnt[2::2]):
            dp0 = (dp0 + dp1 * c0) % MOD
            dp1 = (dp1 + dp0 * c1) % MOD
        ans = (ans + dp1) % MOD

    return ans


def solve_1(acc):
    MOD = 10 ** 9 + 7
    common = acc[-1]
    dp0 = 1
    dp1 = 0
    for a in acc:
        if a == 0:
            dp0 = (dp0 + dp1) % MOD
        elif a == common:
            dp1 = (dp1 + dp0) % MOD
    return dp0


n = int(input())
aaa = list(map(int, input().split()))
if 'PyPy' in sys.version:
    acc = [0] * n
    t = 0
    for i in range(n):
        t = acc[i] = t ^ aaa[i]
else:
    acc = list(accumulate(aaa, func=xor))
# print(acc)
if acc[-1] == 0:
    print(solve_0(acc))
else:
    print(solve_1(acc))

Buggy submission (verdict: Runtime Error):
from collections import defaultdict
from itertools import accumulate
from operator import xor


def solve_0(acc):
    MOD = 10 ** 9 + 7
    cnt_0 = 0
    counts = defaultdict(list)
    for a in acc:
        if a == 0:
            for i, cnt in counts.items():
                if len(cnt) % 2 == 0:
                    cnt[-1] += 1
                else:
                    cnt.append(1)
            cnt_0 += 1
        else:
            cnt = counts[a]
            if len(cnt) % 2 == 0:
                cnt.append(1)
            else:
                cnt[-1] += 1
    ans = pow(2, cnt_0 - 1, MOD)
    for i, cnt in counts.items():
        c1 = cnt[0]
        ans += c1
        loc = {c1: 0}
        if len(cnt) > 2:
            for c0, c1 in zip(cnt[1::2], cnt[2:-1:2]):
                tmp = c1
                for i1, i0 in loc.items():
                    tmp += i1 * (i0 + c0) * c1
                    loc[i1] += c0
                loc[tmp] = 0
                ans = (ans + tmp) % MOD
                # print(ans, c0, c1, loc, tmp)
        ans %= MOD
    return ans


def solve_1(acc):
    MOD = 10 ** 9 + 7
    common = acc[-1]
    step = 1
    cnt = 0
    ans = 0
    for a in acc:
        if a == common:
            cnt = (cnt + step) % MOD
        elif a == 0:
            step = (step + cnt) % MOD
            ans = cnt
    return ans + 1


n = int(input())
aaa = list(map(int, input().split()))
acc = list(accumulate(aaa, func=xor))
# print(acc)
if acc[-1] == 0:
    print(solve_0(acc))
else:
    print(solve_1(acc))

Buggy submission (verdict: Runtime Error):
from collections import defaultdict
from itertools import accumulate
from operator import xor


def solve_0(acc):
    MOD = 10 ** 9 + 7
    cnt_0 = 0
    tmp_dp = defaultdict(lambda: 0)
    fix_dp = defaultdict(lambda: 0)
    last_0 = defaultdict(lambda: 0)
    steps = defaultdict(lambda: 1)
    for a in acc:
        if a == 0:
            for i, c in tmp_dp.items():
                if i in last_0:
                    fix_dp[i] = (fix_dp[i] + (c - 1) * (cnt_0 - last_0[i]) + 1) % MOD
                else:
                    fix_dp[i] = (fix_dp[i] + c) % MOD
                steps[i] = (steps[i] + fix_dp[i]) % MOD
                last_0[i] = cnt_0
            tmp_dp.clear()
            cnt_0 += 1
        else:
            tmp_dp[a] = (tmp_dp[a] + steps[a]) % MOD
        # print(f'a:{a}  tmp:{dict(tmp_dp)}  fix:{dict(fix_dp)}  step:{dict(steps)}')
    # print(f'tmp:{dict(tmp_dp)}  fix:{dict(fix_dp)}  step:{dict(steps)}')
    ans = sum(fix_dp.values()) % MOD
    ans = (ans + pow(2, cnt_0 - 1, MOD)) % MOD
    return ans


def solve_1(acc):
    MOD = 10 ** 9 + 7
    common = acc[-1]
    step = 1
    cnt = 0
    ans = 0
    for a in acc:
        if a == common:
            cnt = (cnt + step) % MOD
        elif a == 0:
            step = (step + cnt) % MOD
            ans = cnt
    return ans + 1


n = int(input())
aaa = list(map(int, input().split()))
acc = list(accumulate(aaa, func=xor))
# print(acc)
if acc[-1] == 0:
    print(solve_0(acc))
else:
    print(solve_1(acc))