Problem p02807 (python)

Fixed submission:
MOD = 10**9+7
N = int(input())
x = list(map(int,input().split()))
x.sort()

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

ans = 0
for i in range(N - 1):
    ans = (ans * (i+1) + (x[-1] - x[i]) * kaijo[i]) % MOD

print(ans)

Buggy submission (verdict: Runtime Error):
MOD = 10**9+7
N = int(input())
x = list(map(int,input().split()))

kaijo = 1
for i in range(1,N):
    kaijo *= i
    #kaijo %= MOD
"""for i in range(N-1):
    d[i] = x[i+1] - x[i]
    d[i] %= MOD
"""
prob = []
a = 0
for i in range(1,N):
    a += 1/i
    prob.append(a)
ans = 0
for i in range(1,N):

    """for j in range(1,i+1):
        prob += 1/j
    """
    ans += (x[i]-x[i-1]) * prob[i-1]
ans *= kaijo
ans %= MOD
ans = int(ans)
print(ans)

Buggy submission (verdict: Runtime Error):
import itertools

N = int(input())
x = list(map(int,input().split()))
y = []
CONST = 10**9 + 7
def kaijo(n):
    if n==1:
        return n
    return n * kaijo(n-1)
N_1 = kaijo(N-1)
ans = 0
for i in range(1,N):
    y.append(i)
Y = list(itertools.permutations(y,len(y)))
del_num = []
ok_num = []
#操作のやつ
#iは何回目の操作かを指す
for i in range(1,N):
    prob = 1/(N-i)
    #順列Yの操作フェーズ
    for j in range(len(Y)):
        #存在しうる順列を探す
        for k in range(N-i):
            #N-i番目の値がN-i以下ならばok、存在しうる
            #print(k)
            #print(Y[j])
            if Y[j][k] <= (N-i):
                flag = 1
                if k == (N-i-1):
                    ok_num.append(j)
            else:
                flag = 0
                break
        if flag != 1:
            if j not in ok_num:
                del_num.append(j)
    #存在しない順列を削除していく
    for num in range(len(del_num)):
        del Y[del_num[num]]
ans_list = []
#ansを求めていく
for i in range(len(Y)):
    Y_list = []
    prob = 1
    ans = 0
    x_list = []
    for m in range(len(x)):
        x_list.append(x[m])
    for m in range(len(y)):
        Y_list.append(Y[i][m])
    #ここから
    for j in range(len(y)):
        prob *= 1/(N-(1+j))
        tmp = Y_list[j] - 1

        ans += x_list[tmp+1] - x_list[tmp]
        for k in range(len(y)):
            if tmp < (Y_list[k]):
                Y_list[k] = Y_list[k] - 1
        del x_list[tmp]
    ans = ans * prob
    ans_list.append(ans)

ans = 0
for i in range(len(ans_list)):
    ans += ans_list[i]
ans = ans * N_1
ans = int(ans)
ans = ans % CONST
print(ans)

Buggy submission (verdict: Runtime Error):
import itertools

N = int(input())
x = list(map(int,input().split()))
y = []
CONST = 10**9 + 7
def kaijo(n):
    if n==1:
        return n
    return n * kaijo(n-1)
N_1 = kaijo(N-1)
ans = 0
for i in range(1,N):
    y.append(i)
Y = list(itertools.permutations(y,len(y)))
del_num = []
ok_num = []
#操作のやつ
#iは何回目の操作かを指す
for i in range(1,N):
    prob = 1/(N-i)
    #順列Yの操作フェーズ
    for j in range(len(Y)):
        #存在しうる順列を探す
        for k in range(N-i):
            #N-i番目の値がN-i以下ならばok、存在しうる
            #print(k)
            #print(Y[j])
            if Y[j][k] <= (N-i):
                flag = 1
                if k == (N-i-1):
                    ok_num.append(j)
            else:
                flag = 0
                break
        if flag != 1:
            if j not in ok_num:
                del_num.append(j)
    #存在しない順列を削除していく
    for num in range(len(del_num)):
        del Y[del_num[num]]
ans_list = []
print("Y")
print(Y)
#ansを求めていく
for i in range(len(Y)):
    Y_list = []
    prob = 1
    ans = 0
    x_list = []
    for m in range(len(x)):
        x_list.append(x[m])
    for m in range(len(y)):
        Y_list.append(Y[i][m])
    #ここから
    for j in range(len(y)):
        prob *= 1/(N-(1+j))
        tmp = Y_list[j] - 1

        ans += x_list[tmp+1] - x_list[tmp]
        print("ans")
        print(ans)
        for k in range(len(y)):
            if tmp < (Y_list[k]):
                Y_list[k] = Y_list[k] - 1
        del x_list[tmp]
    ans = ans * prob
    ans_list.append(ans)

ans = 0
for i in range(len(ans_list)):
    ans += ans_list[i]
ans = ans * N_1
ans = int(ans)
ans = ans % CONST
print("fina._ans")
print(ans)