Problem p02804 (python)

Fixed submission:
def modpow(x,p,mod):
    ret=1
    for i in range(64):
        if (1<<i)&p:
            ret=ret*x%mod
        x=x*x%mod
    return ret

def inverse_table(n,mod):
    table=[]
    for i in range(0,n+1):
        table.append(modpow(i,mod-2,mod))
    return table

def factorial_table(n,mod):
    table=[1]
    for i in range(1,n+1):
        table.append(table[-1]*i%mod)
    return table

def inverse_factorial_table(n,it,mod):
    table=[1]
    for i in range(1,n+1):
        table.append(table[-1]*it[i]%mod)
    return table

it=inverse_table(10**5,1000000007)
ft=factorial_table(10**5,1000000007)
ift=inverse_factorial_table(10**5,it,1000000007)
def c(n,k,it,ft,mod):
    return ft[n]*ift[k]*ift[(n-k)]%mod
n,k=map(int,input().split())
a=sorted(map(int,input().split()))
hoge=[]
while True:
    hoge.append(c(n-1,k-1,it,ft,1000000007))
    if n-1==k-1:
        break
    n-=1
ans=0
for i in range(len(hoge)):
    ans-=hoge[i]*a[i]%1000000007
    ans+=hoge[i]*a[-i-1]%1000000007
    ans%=1000000007
print(ans)

Buggy submission (verdict: Runtime Error):
def modpow(x,p,mod):
    ret=1
    for i in range(64):
        if (1<<i)&p:
            ret=ret*x%mod
        x=x*x%mod
    return ret

def inverse_table(n,mod):
    table=[]
    for i in range(0,n+1):
        table.append(modpow(i,mod-2,mod))
    return table

def factorial_table(n,mod):
    table=[1]
    for i in range(1,n+1):
        table.append(table[-1]*i%mod)
    return table


it=inverse_table(10**5,1000000007)
ft=factorial_table(10**5,1000000007)

def c(n,k,it,ft,mod):
    return ft[n]*it[ft[k]]*it[ft[(n-k)]]%mod

n,k=map(int,input().split())
a=sorted(map(int,input().split()))
hoge=[]
while True:
    hoge.append(c(n-1,k-1,it,ft,1000000007))
    if n-1==k-1:
        break
    n-=1
ans=0
for i in range(len(hoge)):
    ans-=hoge[i]*a[i]%1000000007
    ans+=hoge[i]*a[-i-1]%1000000007
    ans%=1000000007
print(ans)

Buggy submission (verdict: Runtime Error):
def modpow(x,p,mod):
    ret=1
    for i in range(70):
        if (1<<i)&p:
            ret=ret*x%mod
        x=x*x%mod
    return ret

def inverse_table(n,mod):
    table=[]
    for i in range(0,n+1):
        table.append(modpow(i,mod-2,mod))
    return table

def factorial_table(n,mod):
    table=[1]
    for i in range(1,n+1):
        table.append(table[-1]*i%mod)
    return table


it=inverse_table(10**5,1000000007)
ft=factorial_table(10**5,1000000007)

def c(n,k,it,ft,mod):
    return ft[n]*it[ft[k]]*it[ft[(n-k)]]%mod

n,k=map(int,input().split())
a=sorted(map(int,input().split()))
hoge=[]
while True:
    hoge.append(c(n-1,k-1,it,ft,1000000007))
    if n-1==k-1:
        break
    n-=1
ans=0
for i in range(len(hoge)):
    ans-=hoge[i]*a[i]%1000000007
    ans+=hoge[i]*a[-i-1]%1000000007
    ans%=1000000007
print(ans)

Buggy submission (verdict: Runtime Error):
import sys
sys.setrecursionlimit(1000000000)

def modpow(x,p,mod):
    ret=1
    for i in range(100):
        if (1<<i)&p:
            ret=ret*x%mod
        x=x*x%mod
    return ret

def inverse_table(n,mod):
    table=[]
    for i in range(0,n+1):
        table.append(modpow(i,mod-2,mod))
    return table

def factorial_table(n,mod):
    table=[1]
    for i in range(1,n+1):
        table.append(table[-1]*i%mod)
    return table


it=inverse_table(10**5,1000000007)
ft=factorial_table(10**5,1000000007)

memo={}
def c(n,k,it,ft,mod):
    return ft[n]*it[ft[k]]*it[ft[(n-k)]]%mod

n,k=map(int,input().split())
a=sorted(map(int,input().split()))
hoge=[]
while True:
    hoge.append(c(n-1,k-1,it,ft,1000000007))
    if n-1==k-1:
        break
    n-=1
ans=0
for i in range(len(hoge)):
    ans-=hoge[i]*a[i]%1000000007
    ans+=hoge[i]*a[-i-1]%1000000007
    ans%=1000000007
print(ans)

Buggy submission (verdict: Runtime Error):
from functools import lru_cache
@lru_cache(maxsize=100000000)
def c(n,k): 
    return 1 if(k<=0 or n<=k) else ( c(n-1, k-1) + c(n-1, k) ) 

n,k=map(int,input().split())
a=sorted(map(int,input().split()))
num=c(n,k)
hoge=[]
while True:
    hoge.append(c(n-1,k-1))
    if n-1==k-1:
        break
    n-=1

ans=0
for i in range(len(hoge)):
    ans-=hoge[i]*a[i]
    ans+=hoge[i]*a[-i-1]
    ans%=1000000007
print(ans)