Problem p02807 (python)

Fixed submission:
#coding:utf-8
import sys
sys.setrecursionlimit(10**6)
write = sys.stdout.write
dbg = lambda *something : print(*something) if DEBUG else 0
DEBUG = False
def main(given = sys.stdin.readline):
    input = lambda : given().rstrip()
    LMIIS = lambda : list(map(int,input().split()))
    II = lambda : int(input())
    XLMIIS = lambda x : [LMIIS() for _ in range(x)]
    MOD = 10**9+7
    from math import factorial
    N = II()
    X = LMIIS()
    # f = factorial(N-1)
    # def calc(k,slimes):
    #     # print(slimes)
    #     if k == N-1:
    #         return 0
    #     r = 0
    #     # for i in range(N-k-1):
    #     #     slimes2 = slimes[:]
    #     #     r = (slimes2[i+1]-slimes2[i]) / float(N-k-1)
    #     #     slimes2.pop(i)
    #     #     r += calc(k+1,slimes2) % MOD
    #     i = N-k-2
    #     slimes2 = slimes[:]
    #     print(i,slimes2)
    #     r = (slimes2[i+1]-slimes2[i]) * f / (N-k-1) % MOD
    #     slimes2.pop(i)
    #     r += calc(k+1,slimes2) 
    #     return r % MOD
    f = factorial(N-1) % MOD
    def calc(k,slimes):
        r = 0

        for k in range(N-1):
            r += (slimes[-1]-slimes[-2-k]) * f * pow((N-k-1),MOD-2,MOD)
            r %= MOD
        return r
    print(calc(0,X))




if __name__ == '__main__':
    main()

Buggy submission (verdict: Runtime Error):
#coding:utf-8
import sys
sys.setrecursionlimit(10**6)
write = sys.stdout.write
dbg = lambda *something : print(*something) if DEBUG else 0
DEBUG = False
def main(given = sys.stdin.readline):
    input = lambda : given().rstrip()
    LMIIS = lambda : list(map(int,input().split()))
    II = lambda : int(input())
    XLMIIS = lambda x : [LMIIS() for _ in range(x)]
    MOD = 10**9+7
    from math import factorial
    N = II()
    X = LMIIS()
    # f = factorial(N-1)
    # def calc(k,slimes):
    #     # print(slimes)
    #     if k == N-1:
    #         return 0
    #     r = 0
    #     # for i in range(N-k-1):
    #     #     slimes2 = slimes[:]
    #     #     r = (slimes2[i+1]-slimes2[i]) / float(N-k-1)
    #     #     slimes2.pop(i)
    #     #     r += calc(k+1,slimes2) % MOD
    #     i = N-k-2
    #     slimes2 = slimes[:]
    #     print(i,slimes2)
    #     r = (slimes2[i+1]-slimes2[i]) * f / (N-k-1) % MOD
    #     slimes2.pop(i)
    #     r += calc(k+1,slimes2) 
    #     return r % MOD
    f = factorial(N-1)
    def calc(k,slimes):
        if k == N-1:
            return 0
        r = 0


        for k in range(N-1):
            # print(slimes)
            i = N-k-2
            r += (slimes[-1]-slimes[-2-k]) * f / (N-k-1) % MOD
            # slimes.pop(i)
            # r += calc(k+1,slimes) 
        return r % MOD
    
    print(int(calc(0,X)))




if __name__ == '__main__':
    main()

Buggy submission (verdict: Runtime Error):
#coding:utf-8
import sys
sys.setrecursionlimit(10**6)
write = sys.stdout.write
dbg = lambda *something : print(*something) if DEBUG else 0
DEBUG = False
def main(given = sys.stdin.readline):
    input = lambda : given().rstrip()
    LMIIS = lambda : list(map(int,input().split()))
    II = lambda : int(input())
    XLMIIS = lambda x : [LMIIS() for _ in range(x)]
    MOD = 10**9+7
    from math import factorial
    N = II()
    X = LMIIS()
    f = factorial(N-1)
    def calc(k,slimes):
        # print(slimes)
        if k == N-1:
            return 0
        r = 0
        # for i in range(N-k-1):
        #     slimes2 = slimes[:]
        #     r = (slimes2[i+1]-slimes2[i]) / float(N-k-1)
        #     slimes2.pop(i)
        #     r += calc(k+1,slimes2) % MOD
        i = N-k-2
        slimes2 = slimes[:]
        r = (slimes2[i+1]-slimes2[i]) * f / (N-k-1) % MOD
        slimes2.pop(i)
        r += calc(k+1,slimes2) 
        return r % MOD
    
    print(int(calc(0,X)))




if __name__ == '__main__':
    main()

Buggy submission (verdict: Runtime Error):
#coding:utf-8
import sys
sys.setrecursionlimit(10**6)
write = sys.stdout.write
dbg = lambda *something : print(*something) if DEBUG else 0
DEBUG = False
def main(given = sys.stdin.readline):
    input = lambda : given().rstrip()
    LMIIS = lambda : list(map(int,input().split()))
    II = lambda : int(input())
    XLMIIS = lambda x : [LMIIS() for _ in range(x)]
    MOD = 10**9+7
    from math import factorial
    def calc(k,slimes):
        # print(slimes)
        if k == N-1:
            return 0
        r = 0
        # for i in range(N-k-1):
        #     slimes2 = slimes[:]
        #     r = (slimes2[i+1]-slimes2[i]) / float(N-k-1)
        #     slimes2.pop(i)
        #     r += calc(k+1,slimes2) % MOD
        i = N-k-2
        slimes2 = slimes[:]
        r = (slimes2[i+1]-slimes2[i]) / float(N-k-1)
        slimes2.pop(i)
        r += calc(k+1,slimes2) % MOD
        return r
    N = II()
    X = LMIIS()
    print(int(calc(0,X)* factorial(N-1)% MOD))




if __name__ == '__main__':
    main()