Problem p02793 (python)

Fixed submission:
from fractions import gcd
from functools import reduce

#互いに素なa,bについて、a*x+b*y=1の一つの解
def extgcd(a,b):
    r = [1,0,a]
    w = [0,1,b]
    while w[2]!=1:
        q = r[2]//w[2]
        r2 = w
        w2 = [r[0]-q*w[0],r[1]-q*w[1],r[2]-q*w[2]]
        r = r2
        w = w2
    #[x,y]
    return [w[0],w[1]]

# aの逆元(mod m)を求める。(aとmは互いに素であることが前提)
def mod_inv(a,m):
    x = extgcd(a,m)[0]
    return (m+x%m)%m

def lcm_mod(a, mod=-1):
    x = a[0]
    if mod == -1:
        for i in range(1, len(a)):
            x = (x * a[i]) // gcd(x, a[i])
    else:
        for i in range(1, len(a)):
            x *= (a[i] * mod_inv(gcd(x, a[i]), P)) % P
    return x

_,a = [list(map(int, s.split())) for s in open(0)]

P = 10**9 + 7
N = max(a)

lcm_ = lcm_mod(a, P)

ans = 0
for x in a:
    ans += int((lcm_*mod_inv(x,P))%P)
print(int(ans%P))

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

#互いに素なa,bについて、a*x+b*y=1の一つの解
def extgcd(a,b):
    r = [1,0,a]
    w = [0,1,b]
    while w[2]!=1:
        q = r[2]//w[2]
        r2 = w
        w2 = [r[0]-q*w[0],r[1]-q*w[1],r[2]-q*w[2]]
        r = r2
        w = w2
    #[x,y]
    return [w[0],w[1]]

# aの逆元(mod m)を求める。(aとmは互いに素であることが前提)
def mod_inv(a,m):
    x = extgcd(a,m)[0]
    return (m+x%m)%m

def lcm_mod(a, mod=-1):
    x = a[0]
    if mod == -1:
        for i in range(1, len(a)):
            x = (x * a[i]) // gcd(x, a[i])
    else:
        for i in range(1, len(a)):
            x *= (a[i] * mod_inv(gcd(x, a[i]), P)) % P
    return x

_,a = [list(map(int, s.split())) for s in open(0)]

P = 10**9 + 7
N = max(a)

lcm_ = lcm_mod(a, P)

ans = 0
for x in a:
    ans += int((lcm_*inv_t[x])%P)
print(int(ans%P))

Buggy submission (verdict: Runtime Error):
import fractions
from functools import reduce

P = 10**9 + 7
N = 1000000
inv_t = [0]+[1]
for i in range(2,N):
    inv_t += [inv_t[P % i] * (P - int(P / i)) % P]

_,p = [list(map(int, s.split())) for s in open(0)]
gcd_ = reduce(fractions.gcd, p)
lcm_ = 1
for q in p:
    lcm_ *= inv_t[q]
lcm_ *= inv_t[gcd_]
x = 1e9 + 7
ans = 0
for q in p:
    ans += int((lcm_/q)%x)
print(int(ans%x))

Buggy submission (verdict: Runtime Error):
import fractions
from functools import reduce

P = 10**9 + 7
N = 1000000
inv_t = [0]+[1]
for i in range(2,N):
    inv_t += [inv_t[P % i] * (P - int(P / i)) % P]

_,p = [list(map(int, s.split())) for s in open(0)]
gcd_ = reduce(fractions.gcd, p)
lcm_ = 1
for q in p:
    lcm_ *= inv_t[q]
x = 1e9 + 7
ans = 0
for q in p:
    ans += int((lcm_/q)%x)
print(int(ans%x))

Buggy submission (verdict: Runtime Error):
import fractions
from functools import reduce
def lcm_base(x, y):
    return (x * y) // fractions.gcd(x, y)

_,p = [list(map(int, s.split())) for s in open(0)]
lcm = reduce(lcm_base, p)
x = 1e9 + 7
ans = 0
for q in p:
    ans += int((lcm/q)%x)
print(int(ans%x))

Buggy submission (verdict: Runtime Error):
import numpy as np
_,p = [list(map(int, s.split())) for s in open(0)]
lcm = np.lcm.reduce(p)
x = 1e9 + 7
ans = 0
for q in p:
    ans += int((lcm/q)%x)
print(int(ans%x))

Buggy submission (verdict: Runtime Error):
_,p = [list(map(int, s.split())) for s in open(0)]
lcm = np.lcm.reduce(p)
x = 1e9 + 7
ans = 0
for q in p:
    ans += int((lcm/q)%x)
print(int(ans%x))