Problem p02793 (python)

Fixed submission:
import math
from typing import List, Counter, Tuple
from collections import Counter
from itertools import permutations, zip_longest, groupby


def read_int() -> int:
    return int(input().strip())


def read_ints() -> List[int]:
    return list(map(int, input().strip().split(' ')))

modulo = 10**9+7


def merge(A, start, end, divisors):
    if start == end:
        return Counter(divisors[A[start]])
    mid = start+(end-start)//2
    left = merge(A, start, mid, divisors)
    right = merge(A, mid+1, end, divisors)
    for r in right:
        left[r] = max(left[r], right[r])
    return left

def solve() -> int:
    N = read_int()
    A = read_ints()
    M = 10**6+1
    divisors: List[List[int]] = [[] for _ in range(M)]
    for i in range(2, M):
        if len(divisors[i]) == 0:
            for j in range(i, M, i):
                temp = j
                while temp%i == 0:
                    divisors[j].append(i)
                    temp //= i
    lcd_counter = merge(A, 0, N-1, divisors)
    lcd = 1
    for k in lcd_counter:
        lcd = (lcd*pow(k, lcd_counter[k], modulo))%modulo
    answer = 0
    for a in A:
        answer = (answer+(lcd*pow(a, modulo-2, modulo))%modulo)%modulo
    return answer


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

Buggy submission (verdict: Runtime Error):
from collections import Counter


def read_int():
    return int(input().strip())


def read_ints():
    return list(map(int, input().strip().split(' ')))


def solve():
    n = read_int()
    A = read_ints()
    MAX = max(A)+1 # 10**6+1
    count = [Counter() for i in range(MAX)]
    modulo = 10**9+7
    # disassemble all numbers up to 10**6
    for number in range(2, MAX):
        if len(count[number]) > 0: # not prime
            continue
        for step in range(number, MAX, number):
            temp = number
            while step%temp == 0:
                count[step][number] += 1
                temp *= number
    max_counter = Counter()
    for a in A:
        for k, v in count[a].items():
            max_counter[k] = max(max_counter[k], v)
    answer = 0
    for a in A:
        B = 1
        for k, v in max_counter.items():
            B = (B*k**(v-count[a][k]))%modulo
        answer = (answer+B)%modulo
    return answer


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

Buggy submission (verdict: Runtime Error):
from collections import Counter


def read_int():
    return int(input().strip())


def read_ints():
    return list(map(int, input().strip().split(' ')))


def solve():
    n = read_int()
    A = read_ints()
    MAX = max(A)+1 # 10**6+1
    count = [Counter() for i in range(MAX)]
    modulo = 10**9+7
    # disassemble all numbers up to 10**6
    for number in range(2, MAX):
        if len(count[number]) > 0: # not prime
            continue
        for step in range(number, MAX, number):
            temp = number
            while step%temp == 0:
                count[step][number] += 1
                temp *= number
    max_counter = Counter()
    for a in A:
        for k, v in count[a].items():
            max_counter[k] = max(max_counter[k], v)
    answer = 0
    for a in A:
        B = 1
        for k, v in max_counter.items():
            B = (B*k**(v-count[a][k]))%modulo
        answer = (answer+B)%modulo
    return answer


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