Problem p02793 (python)

Fixed submission:
def main():
    from math import sqrt
    from fractions import gcd
    from collections import defaultdict
    from copy import deepcopy

    N = int(input())
    A = list(map(int, input().split()))
    mod = 10 ** 9 + 7

    p = [1] * (1001)
    P = []  # 1000までの素数表
    p[0] = p[1] = 0
    for x in range(2, int(sqrt(1000000)) + 1):
        if p[x]:
            P.append(x)
            for y in range(x * x, 1001, x):
                p[y] = 0

    B = [defaultdict(int) for i in range(N)]  # N個の数を素因数分解したもの
    lcm = defaultdict(int)  # N個の数のlcmを素因数分解したもの

    for i in range(N):
        for j in P:
            if A[i] % j == 0:
                while A[i] % j == 0:
                    A[i] //= j
                    B[i][j] += 1
            lcm[j] = max(lcm[j], B[i][j])
        if A[i] != 1:
            B[i][A[i]] = 1
            lcm[A[i]] = 1

    l = 1  # N個の数のlcm
    for i in lcm.keys():
        l = (l * pow(i, lcm[i], mod)) % mod

    ans = 0
    # l/A[i]の和を求めていく
    for i in range(N):
        r = l
        for j in list(B[i].keys()):
            r = (r * pow(j, mod - B[i][j] - 1, mod)) % mod
        ans = (ans + r) % mod

    print(ans)

if __name__ == '__main__':
    main()

Buggy submission (verdict: Runtime Error):
from math import gcd,sqrt
from collections import defaultdict
from copy import deepcopy

N = int(input())
A = list(map(int,input().split()))
mod = 10**9+7

p = [1]*(1001)
P = []  #1000までの素数表
p[0] = p[1] = 0
for x in range(2, int(sqrt(1000000))+1):
    if p[x]:
        P.append(x)
        for y in range(x*x, 1001, x):
            p[y] = 0

B = [defaultdict(int) for i in range(N)]  #N個の数を素因数分解したもの
lcm = defaultdict(int)  #N個の数のlcmを素因数分解したもの

for i in range(N):
    for j in P:
        if A[i] % j == 0:
            while A[i] % j == 0:
                A[i] //= j
                B[i][j] += 1
        lcm[j] = max(lcm[j],B[i][j])
    if A[i] != 1:
        B[i][A[i]] = 1
        lcm[A[i]] = 1

l = 1  #N個の数のlcm
for i in lcm.keys():
    l = (l*pow(i,lcm[i],mod)) % mod

ans = 0
#l/A[i]の和を求めていく
for i in range(N):
    r = l
    for j in list(B[i].keys()):
        r = (r*pow(j,mod-B[i][j]-1,mod)) % mod
    ans = (ans + r) % mod

print(ans)

Buggy submission (verdict: Runtime Error):
from math import gcd,sqrt
from collections import defaultdict
from copy import deepcopy

N = int(input())
A = list(map(int,input().split()))
mod = 10**9+7

def f(n):
    d = defaultdict(int)
    if n % 2 == 0:
        r = 0
        while n % 2 == 0:
            n //= 2
            r += 1
        d[2] = r
    for i in range(3,int(sqrt(n))+1):
        if n % i == 0:
            s = 0
            while n % i == 0:
                n //= i
                s += 1
            d[i] = s
    if n != 1:
        d[n] = 1
    return d

B = []
for i in range(N):
    B.append(f(A[i]))

lp = deepcopy(B[0])

for i in range(N-1):
    for j in list(set(B[i+1].keys()) | set(lp.keys())):
        lp[j] = max(B[i+1][j],lp[j])

ans = 0

for i in range(N):
    r = 1
    for j in list(lp.keys()):
        r = (r*pow(j,lp[j]-B[i][j],mod)) % mod
    ans = (ans + r) % mod

print(ans)

Buggy submission (verdict: Runtime Error):
from math import gcd,sqrt
from collections import defaultdict
from copy import deepcopy

N = int(input())
A = list(map(int,input().split()))
mod = 10**9+7

def f(n):
    d = defaultdict(int)
    for i in range(2,int(sqrt(n))+1):
        if n % i == 0:
            r = 0
            while n % i == 0:
                n //= i
                r += 1
            d[i] = r
    if n != 1:
        d[n] = 1
    return d

B = []
for i in range(N):
    B.append(f(A[i]))

lp = deepcopy(B[0])

for i in range(N-1):
    for j in list(set(B[i+1].keys()) | set(lp.keys())):
        lp[j] = max(B[i+1][j],lp[j])

ans = 0

for i in range(N):
    r = 1
    for j in list(lp.keys()):
        r = (r*pow(j,lp[j]-B[i][j],mod)) % mod
    ans = (ans + r) % mod

print(ans)

Buggy submission (verdict: Runtime Error):
from math import gcd

N = int(input())
A = list(map(int,input().split()))
mod = 10**9+7

l = A[0]
for i in range(N-1):
    l = l*A[i+1]//gcd(l,A[i+1])

ans = 0
for i in range(N):
    ans = (ans + l//A[i]) % mod

print(ans)