Problem p02807 (python)

Fixed submission:
N = int(input())
x = list(map(int, input().split()))
MOD = 10**9+7
D = x[-1]-x[0]
ans = 0
fact = [1]

for i in range(1, N):
    fact.append(fact[-1]*i%MOD)

inv_table = [-1]*N

for i in range(N):
    inv_table[i] = pow(i, MOD-2, MOD)
    
for i in range(N-1):
    d = D-(x[i]-x[0])
    ans += d*fact[N-1]*inv_table[i+1]
    ans %= MOD

print(ans)

Buggy submission (verdict: Runtime Error):
import sys
input = sys.stdin.readline

def inv(x):
    return pow(x, MOD-2, MOD)
    
def C(n, r):
    #return fact[n]*inv(fact[r])*inv(fact[n-r])
    return fact[n]*inv_table[r]*inv_table[n-r]

N = int(input())

if N>3000:
    1/0
    
x = list(map(int, input().split()))
ans = 0
MOD = 10**9+7
fact = [1]

for i in range(1, N+1):
    fact.append(fact[-1]*i%MOD)

inv_table = [-1]*(N+1)

for i in range(N+1):
    inv_table[i] = inv(fact[i])
    
for i in range(N-1):
    for j in range(i+1, N):
        #print(i, j)
        
        #r = j-i+1
        add = 1
        
        if j==N-1:
            r = N-2-i+1
            add *= (x[j]-x[i])
            add %= MOD
            add *= C(N-1, r)
            add %= MOD
            add *= fact[r-1]
            add %= MOD
            add *= fact[N-1-r]
            add %= MOD
            #ans += (x[j]-x[i])*C(N-1, r)*fact[r-1]*fact[N-1-r]
            ans += add
            #print(C(N-1, r-1)*fact[N-1-r+1]*fact[r-2])
        else:
            r = j-i+1
            add *= (x[j]-x[i])
            add %= MOD
            add *= C(N-1, r)
            add %= MOD
            add *= fact[j-i-1]
            add %= MOD
            add *= fact[N-1-r]
            add %= MOD
            ans += add
            #ans += (x[j]-x[i])*C(N-1, r)*fact[j-i-1]*fact[N-1-r]
        #print(x[j]-x[i])
            #print(C(N-1, r)*fact[N-1-r]*fact[r-2])
        #ans += (x[j]-x[i])*C(N-1, r)*fact[N-1-r]
        #print('ans:', ans)
        ans %= MOD

#ans *= fact[N-1]
ans %= MOD

print(ans)

Buggy submission (verdict: Runtime Error):
import sys
input = sys.stdin.readline

def inv(x):
    return pow(x, MOD-2, MOD)
    
def C(n, r):
    return fact[n]*inv(fact[r])*inv(fact[n-r])

N = int(input())

if N>3000:
    1/0
    
x = list(map(int, input().split()))
ans = 0
MOD = 10**9+7
fact = [1]

for i in range(1, N):
    fact.append(fact[-1]*i%MOD)
    
for i in range(N-1):
    for j in range(i+1, N):
        #print(i, j)
        
        #r = j-i+1
        add = 1
        
        if j==N-1:
            r = N-2-i+1
            add *= (x[j]-x[i])
            add %= MOD
            add *= C(N-1, r)
            add %= MOD
            add *= fact[r-1]
            add %= MOD
            add *= fact[N-1-r]
            add %= MOD
            #ans += (x[j]-x[i])*C(N-1, r)*fact[r-1]*fact[N-1-r]
            ans += add
            #print(C(N-1, r-1)*fact[N-1-r+1]*fact[r-2])
        else:
            r = j-i+1
            add *= (x[j]-x[i])
            add %= MOD
            add *= C(N-1, r)
            add %= MOD
            add *= fact[j-i-1]
            add %= MOD
            add *= fact[N-1-r]
            add %= MOD
            ans += add
            #ans += (x[j]-x[i])*C(N-1, r)*fact[j-i-1]*fact[N-1-r]
        #print(x[j]-x[i])
            #print(C(N-1, r)*fact[N-1-r]*fact[r-2])
        #ans += (x[j]-x[i])*C(N-1, r)*fact[N-1-r]
        #print('ans:', ans)
        ans %= MOD

#ans *= fact[N-1]
ans %= MOD

print(ans)

Buggy submission (verdict: Runtime Error):
import sys
input = sys.stdin.readline

def inv(x):
    return pow(x, MOD-2, MOD)
    
def C(n, r):
    return fact[n]*inv(fact[r])*inv(fact[n-r])

N = int(input())

if N>3000:
    1/0
    
x = list(map(int, input().split()))
ans = 0
MOD = 10**9+7
fact = [1]

for i in range(1, N):
    fact.append(fact[-1]*i%MOD)
    
for i in range(N-1):
    for j in range(i+1, N):
        #print(i, j)
        
        r = j-i+1
        
        if j==N-1:
            r = N-2-i+1
            ans += (x[j]-x[i])*C(N-1, r)*fact[r-1]*fact[N-1-r]
            #print(C(N-1, r-1)*fact[N-1-r+1]*fact[r-2])
        else:
            r = j-i+1
            ans += (x[j]-x[i])*C(N-1, r)*fact[j-i-1]*fact[N-1-r]
        #print(x[j]-x[i])
            #print(C(N-1, r)*fact[N-1-r]*fact[r-2])
        #ans += (x[j]-x[i])*C(N-1, r)*fact[N-1-r]
        #print('ans:', ans)
        ans %= MOD

#ans *= fact[N-1]
ans %= MOD

print(ans)