Problem p03676 (python)

Fixed submission:
#<` _!~`    (?<?OrvvvzzzI`       .(. C/~.~..~..~.(~.~.~_~..~.~~.~~~<-4X~..<;!.``.-XyyyyyyyyyyyyyXwwXy
#~~```````   ...+trrrrrrI`   `    `_(!`  ` _ .._-:..~-.~_....  `  ``__C..;<_.~`..uuZZZZZZZZZZZZX00VT0
#  ` ...-((<zltttttlttttl      `  ~(_ .(>_<~~(c~+~~~_:~(~~~(~~.~..-.  -.<<!.`..JvzzzzzzzzXZC17?C1- `
#---((++zllllllllllllllll. `     `<<<~(z<J<~(v>(C~.~(l~(<~~(o~~~~~(_~~(+...(+zOttrrttv<!<1>` .._----(
#=======================?_  `   ` <(~~jO(Z_(c((y(~~~(I~(_<~(wx~~(~~l~~~( ``.<(v<<111<-(-....(++zOwwww
#???>>?>>>>?>>>>>>>>>>>>>:`   `  `(>~(wZz$~/  (`.__~(r+~!_<~OXZ>(_~z_~(~C-.`.(z??????=1zzOrwwwVwzvrrZ
#wOwzzz++<;;;;;;;++++++<:: `      .<~(zXkr(`  -` ><~(ww-~  ~(?zx(<~z>~(l<_<_`.-+??1zwwZuVzzvOrtrwvwXk
#WkuzrZXyVfWX&swXZXWZyWWkI`  ` ` ` _~(uuZG&..(o-..(:~zz-_...~<(Oo<(wI~~z(.`._``(wzuXUwrrwXXWWVrrrXWXW
#HkkkwwOOwXuuXWHWyXXXZZZyV`   `   `<((uuI(`2-MM/``_<<(r;.jTYWHRwk>(zI-~wG,````. OwzvwwXXkdWHrwXkWHkkW
#HkWWWXwwWXwVwXwXXXXXUXU3_`    ` `((2Xk% .HdMX\   .._(l``dndMN .(wuI<~XWI<-...`(XW0UwwWpWqqbkHHWHpHg
#WWbpfWWXUWXwwwwwwXWkXXZ0~:` `  `  .xWwWO``?kwf     `  ?!`zXMWY`(duXG$(uuu:~~~_<&jWZOXWWHWWXWWWWWHmgg
#gHHHkHUXwuzXXuXXXXXUWHk$~(  `    ``dWXWz{`````` ``      ` ?"= .CuXDd$duuuI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rWuuXu>~~~~jk0dHNHMMMH91HH1VX
#HWrXWWWHHHWXHMMMHX<~~(uXuudh.` ._ `     `.v``` ` `< (``` ``!_ ` `   `.wwwdXuWw~~~~~wHMMMMNmsdN#WSigg
#WK0OVOwWMMHHMMHkWD~~~juSuXHMK+  (_``  ` ``` ` ` ` ~ <`  `  `` ` ```~ `OzZzXuXk:~~~~(V9WNMMNM##Z+v<XM
#MI+vzzzZTWHHMMMHH<~~_wXuZWHkkWm,.< `.  `  `  ` ` `_.~ ` `` ` ` ` ` ` `jzXuzWZWI~~~~~wMMMMM5ZnsJGJWNQ
#XOOwwOzOdkwTWUWWr~~~(uXuXMMMMHHMm(_`_ `  `  ` ``   (` `  ` ` `  `_.```(wvXuzWXk_~~~~dMMHHMM&N9Q8WHMW
#UVwOIjk0+zwU?4kX<~~~(uWZHHMMHHHMMNc-_` `` ` `   ``._`` `  ` ` ```__ `  .OZXXXWR:~~~~(uWMHHM#mV3&QQMQ
import sys
input = sys.stdin.readline

n = int(input())
A = tuple(map(int,input().split()))
p = 10**9+7
used = [-1]*n
for i in range(n+1):
    if used[A[i]-1]>=0:
        a = used[A[i]-1]
        b = i
        break
    used[A[i]-1] = i


#互いに素な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

######################################################


#pは素数とする

#fact[i] : i! modp (0<= i <p) O(p)
fact = [1]
k = 1
for i in range(1,n+2):
    k = k*i%p
    fact.append(k)


#n! = a p^eとしたときのeを求める (O(logp n)
def mod_e(n,p):
    res = 0
    while n >= p:
        res += n//p
        n = n//p
    return res

#n! = a p^eとしたときのa mod pを求める  (O(logp n)
def mod_fact(n,p):
    if n==0:
        return 1
    
    
    res = mod_fact(n//p,p)
    
    if n//p %2 != 0:
        return res*(p-fact[n%p])%p
    return res*fact[n%p]%p
    
######################################################

#nCk mod p
def mod_comb(n,k,p):
    if n < 0 or k < 0 or n<k:
        return 0
    e1 = mod_e(n,p)
    e2 = mod_e(k,p)
    e3 = mod_e(n-k,p)
    #pで割り切れる場合
    if e1 > e2 + e3:
        return 0

    #pで割り切れない場合
    a1 = mod_fact(n,p)
    a2 = mod_fact(k,p)
    a3 = mod_fact(n-k,p)
    return a1 * mod_inv(a2*a3%p,p) %p
#print(a,b)
for i in range(1,n+2):
    res = mod_comb(n+1,i,p)-mod_comb(a+n-b,i-1,p)
    if res < 0:
        res += p
    print(res)

Buggy submission (verdict: Runtime Error):
n = int(input())
a = list(map(int,input().split()))

c = [1,n-1]
#c[i]:n-1 C i
for i in range(1,n+1):
    if n < i+2:
        c.append(0)
    else:
        r = (c[-1]*(n-1-i)//(i+1))
        c.append(r)
k = sum(a)- n*(n+1)//2

pp = []
for i in range(n+1):
    if a[i] == k:
        pp.append(i)
w = pp[1]-pp[0]-1

d = [1,n-w-1]
#d[i]:n-w-1 C i
for i in range(1,n+1):
    if n-w-1  < i+1:
        d.append(0)
    else:
        r = (d[-1]*(n-w-1-i)//(i+1))
        
        d.append(r)
for i in range(len(c)):
    c[i] = c[i]%(10**9+7)
    d[i] = d[i]%(10**9+7)
    

print(n)
for i in range(2,n+2):
    #nagasa k