Problem p02793 (python)

Fixed submission:
#coding:utf-8
import sys,os
sys.setrecursionlimit(10**6)
write = sys.stdout.write
dbg = (lambda *something: print(*something)) if 'TERM_PROGRAM' in os.environ else lambda *x: 0
def main(given=sys.stdin.readline):
    input = lambda: given().rstrip()
    LMIIS = lambda: list(map(int,input().split()))
    II = lambda: int(input())
    XLMIIS = lambda x: [LMIIS() for _ in range(x)]
    YN = lambda c : print('Yes') if c else print('No')
    MOD = 10**9+7

    # def gen_primes2(upper_bound):
    #     upper_bound += 1
    #     P = [True] * upper_bound
    #     P[0] = P[1] = False
    #     for i in range(4,upper_bound,2):
    #         P[i] = False
    #     for i in range(3,int(upper_bound**0.5),2):
    #         if P[i]:
    #             for j in range(i**2,upper_bound,i):
    #                 P[j] = False
    #     res = []
    #     for i in range(upper_bound):
    #         if P[i]:
    #             res.append(i)
    #     return res

    N = II()
    A = LMIIS()
    # upper_bound = max(A)
    # primes = gen_primes2(upper_bound)
    # LCMP = [0]*len(primes)
    # for i in range(N):
    #     # if i % 100 == 0:
    #     #     print(i)
    #     a = A[i]
    #     for j,p  in enumerate(primes):
    #         t = 0
    #         while a % p == 0:
    #             a //= p
    #             t += 1
    #         LCMP[j] = max(t, LCMP[j])
    #         if a == 1:
    #             break
    # lcm = 1
    # for i in range(len(primes)):
    #     lcm = lcm * pow(primes[i],LCMP[i],MOD) % MOD
    lcm = A[0]
    from fractions import gcd

    for i in range(1,N):
        lcm = lcm * A[i] // gcd(lcm,A[i])

    invans = 0
    for a in A:
        invans += pow(a, MOD-2, MOD)
        invans %= MOD
    print(lcm*invans%MOD)

if __name__ == '__main__':
    main()

Buggy submission (verdict: Runtime Error):
#coding:utf-8
import sys,os
sys.setrecursionlimit(10**6)
write = sys.stdout.write
dbg = (lambda *something: print(*something)) if 'TERM_PROGRAM' in os.environ else lambda *x: 0
def main(given=sys.stdin.readline):
    input = lambda: given().rstrip()
    LMIIS = lambda: list(map(int,input().split()))
    II = lambda: int(input())
    XLMIIS = lambda x: [LMIIS() for _ in range(x)]
    YN = lambda c : print('Yes') if c else print('No')
    MOD = 10**9+7

    
    def gen_primes(upper_bound):
        from collections import deque
        target = deque(range(2,upper_bound+1))
        primes = []
        while target[0] <= upper_bound**0.5:
            top = target.popleft()
            primes.append(top)
            tmp = deque()
            for num in target:
                if num % top != 0:
                    tmp.append(num)
            target = tmp
        
        return primes + list(target)


    N = II()
    A = LMIIS()

    primes = gen_primes(max(A))
    LCMP = [0]*len(primes)
    for i in range(N):
        a = A[i]
        for j in range(len(primes)):
            t = 0
            while a % primes[j] == 0:
                a //= primes[j]
                t += 1
            LCMP[j] = max(t, LCMP[j])
            if a == 1:
                break
    lcm = 1
    for i in range(len(primes)):
        lcm = lcm * pow(primes[i],LCMP[i],MOD) % MOD

    # print(lcm)

    # from fractions import gcd
    # lcm = 1
    # for a in A:
    #     g = gcd(lcm,a)
    #     lcm = lcm * a * pow(g, MOD-2, MOD) % MOD
    # print(lcm)



    ans = 0
    # for i in range(N):
    #     t = 1
    #     for j in range(len(primes)):
    #         t = t*pow(primes[j],LCMP[j] - dp[i][j],MOD)%MOD
    #     ans = (ans + t) % MOD
    for a in A:
        ans += lcm * pow(a, MOD-2, MOD)
        ans %= MOD
    print(ans)




    

if __name__ == '__main__':
    main()

Buggy submission (verdict: Runtime Error):
#coding:utf-8
import sys,os
sys.setrecursionlimit(10**6)
write = sys.stdout.write
dbg = (lambda *something: print(*something)) if 'TERM_PROGRAM' in os.environ else lambda *x: 0
def main(given=sys.stdin.readline):
    input = lambda: given().rstrip()
    LMIIS = lambda: list(map(int,input().split()))
    II = lambda: int(input())
    XLMIIS = lambda x: [LMIIS() for _ in range(x)]
    YN = lambda c : print('Yes') if c else print('No')
    MOD = 10**9+7

    
    def gen_primes(upper_bound):
        from collections import deque
        target = deque(range(2,upper_bound+1))
        primes = deque()
        while target[0] <= upper_bound**0.5:
            top = target.popleft()
            primes.append(top)
            tmp = deque()
            for num in target:
                if num % top != 0:
                    tmp.append(num)
            target = tmp
        return list(primes + target)


    N = II()
    A = LMIIS()
    primes = gen_primes(max(A))
    LCMP = [0]*len(primes)
    for i in range(N):
        a = A[i]
        for j in range(len(primes)):
            t = 0
            while a % primes[j] == 0:
                a //= primes[j]
                t += 1
            LCMP[j] = max(t, LCMP[j])
            if a == 1:
                break
    lcm = 1
    for i in range(len(primes)):
        lcm = lcm * pow(primes[i],LCMP[i],MOD) % MOD

    # print(lcm)

    # from fractions import gcd
    # lcm = 1
    # for a in A:
    #     g = gcd(lcm,a)
    #     lcm = lcm * a * pow(g, MOD-2, MOD) % MOD
    # print(lcm)



    ans = 0
    # for i in range(N):
    #     t = 1
    #     for j in range(len(primes)):
    #         t = t*pow(primes[j],LCMP[j] - dp[i][j],MOD)%MOD
    #     ans = (ans + t) % MOD
    for a in A:
        ans += lcm * pow(a, MOD-2, MOD)
        ans %= MOD
    print(ans)




    

if __name__ == '__main__':
    main()