Problem p02793 (python)

Fixed submission:
import fractions

def main():
    mod = 10**9+7
    N = int(input())
    A = list(map(int,input().split()))
    lcm = 1
    for Ai in A:
        lcm = lcm*Ai//fractions.gcd(lcm,Ai)
    lcm %= mod
    ans = 0
    for Ai in A:
        ans += lcm*pow(Ai,mod-2,mod)%mod
        ans %= mod
    print(ans)
    
if __name__ == '__main__':
    main()

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

N = int(input())
A = list(map(int,input().split()))

#https://qiita.com/maebaru/items/317af98e85e9f4db04f9
def prime_numbers(m):
    # 素数を保持するset
    primes = set()

    # 2以上m以下の素数を数え上げる
    n = 2
    while n <= m:
        # 自身より小さい全ての素数で割り切れなければ、素数とみなせる
        if all(n % i != 0 for i in primes):
            primes.add(n)
        n += 1

    return list(primes)
    
#https://qiita.com/snow67675476/items/e87ddb9285e27ea555f8
def factorization(n,primes,primenum_count_for_lcm):
    temp = n
    for i in range(len(primes)):
        if temp%primes[i]==0:
            cnt=0
            while temp%primes[i]==0:
                cnt+=1
                temp //= primes[i]
            if primenum_count_for_lcm[i] < cnt:
                primenum_count_for_lcm[i] = cnt
        if temp < primes[i]:
            break

    if temp!=1:
        idx = primenum_count_for_lcm.index(temp)
        if primenum_count_for_lcm[idx] == 0:
            primenum_count_for_lcm[idx] = 1
            
    #if arr==[]:
    #    arr.append([n, 1])
    
    return primenum_count_for_lcm

primes = prime_numbers(int(10**3))
primes.sort()
primenum_count_for_lcm = [0]*len(primes)
for i in range(N):
    primenum_count_for_lcm = factorization(A[i],primes,primenum_count_for_lcm)

sugoilcm = 1
for i in range(len(primes)):
    sugoilcm *= primes[i]**primenum_count_for_lcm[i]

ans = 0
for i in range(N):
    ans += sugoilcm//A[i]
print(int(ans%(10**9+7)))

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

N = int(input())
A = list(map(int,input().split()))

#https://qiita.com/maebaru/items/317af98e85e9f4db04f9
def prime_numbers(m):
    # 素数を保持するset
    primes = set()

    # 2以上m以下の素数を数え上げる
    n = 2
    while n <= m:
        # 自身より小さい全ての素数で割り切れなければ、素数とみなせる
        if all(n % i != 0 for i in primes):
            primes.add(n)
        n += 1

    return list(primes)
    
#https://qiita.com/snow67675476/items/e87ddb9285e27ea555f8
def factorization(n,primes,primenum_count_for_lcm):
    temp = n
    for i in range(len(primes)):
        if temp%primes[i]==0:
            cnt=0
            while temp%primes[i]==0:
                cnt+=1
                temp //= primes[i]
            if primenum_count_for_lcm[i] < cnt:
                primenum_count_for_lcm[i] = cnt
        if temp < i:
            break

    if temp!=1:
        idx = primenum_count_for_lcm.index(temp)
        if primenum_count_for_lcm[idx] == 0:
            primenum_count_for_lcm[idx] = 1
            
    #if arr==[]:
    #    arr.append([n, 1])
    
    return primenum_count_for_lcm

primes = prime_numbers(int(10**3))
primes.sort()
primenum_count_for_lcm = [0]*len(primes)
for i in range(N):
    primenum_count_for_lcm = factorization(A[i],primes,primenum_count_for_lcm)

sugoilcm = 1
for i in range(len(primes)):
    sugoilcm *= primes[i]**primenum_count_for_lcm[i]

ans = 0
for i in range(N):
    ans += sugoilcm//A[i]
print(int(ans%(10**9+7)))

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

N = int(input())
A = list(map(int,input().split()))

#https://qiita.com/maebaru/items/317af98e85e9f4db04f9
def prime_numbers(m):
    # 素数を保持するset
    primes = set()

    # 2以上m以下の素数を数え上げる
    n = 2
    while n <= m:
        # 自身より小さい全ての素数で割り切れなければ、素数とみなせる
        if all(n % i != 0 for i in primes):
            primes.add(n)
        n += 1

    return list(primes)
    
#https://qiita.com/snow67675476/items/e87ddb9285e27ea555f8
def factorization(n,primes,primenum_count_for_lcm):
    temp = n
    for i in range(len(primes)):
        if temp%primes[i]==0:
            cnt=0
            while temp%primes[i]==0:
                cnt+=1
                temp //= primes[i]
            if primenum_count_for_lcm[i] < cnt:
                primenum_count_for_lcm[i] = cnt
        if temp < i:
            break

    if temp!=1:
        idx = primenum_count_for_lcm.index(temp())
        if primenum_count_for_lcm[idx] == 0:
            primenum_count_for_lcm[idx] = 1
            
    #if arr==[]:
    #    arr.append([n, 1])
    
    return primenum_count_for_lcm

primes = prime_numbers(int(10**3))
primes.sort()
primenum_count_for_lcm = [0]*len(primes)
for i in range(N):
    primenum_count_for_lcm = factorization(A[i],primes,primenum_count_for_lcm)

sugoilcm = 1
for i in range(len(primes)):
    sugoilcm *= primes[i]**primenum_count_for_lcm[i]

ans = 0
for i in range(N):
    ans += sugoilcm//A[i]
print(int(ans%(10**9+7)))