Buggy python submission (verdict: Time Limit Exceeded):
import sys
input = sys.stdin.readline
from fractions import Fraction

def main(): 
    N = int(input())
    x = [int(i) for i in input().split()]

    MOD = 10**9+7

    answer = 0
    frac = 0
    for i in range(N-1):
        frac += Fraction(1,i+1)
        dist = (x[i+1] - x[i]) % MOD
        answer = (answer + dist * frac) % MOD
    for i in range(1,N):
        answer = (answer * i) % MOD

    print(int(answer))

if __name__ == "__main__":
    main()

Fixed submission:
import sys
input = sys.stdin.readline

# 1~nまでの逆元(mod m) O(n)
def mod_inv_list(n:int, m:int):
    # inv_t[i]: iの法mの下での逆元
    inv_t = [0] + [1]
    for i in range(2,n):
        inv_t += [inv_t[m % i] * (m - m//i) % m]
    return inv_t

def main():
    N = int(input())
    x = [int(i) for i in input().split()]
    MOD = 10**9 + 7

    inv_acum = mod_inv_list(N,MOD)
    for i in range(1,N):
        inv_acum[i] = (inv_acum[i] + inv_acum[i-1]) % MOD

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

    for i in range(1,N):
        ans = (ans * i) % MOD

    print(ans)

if __name__ == "__main__":
    main()

Buggy python submission (verdict: Time Limit Exceeded):
import sys
input = sys.stdin.readline
from fractions import Fraction

def main(): 
    N = int(input())
    x = [int(i) for i in input().split()]

    MOD = 10**9+7

    frac = [""] * (N-1)
    for i in range(N-1):
        if i == 0:
            frac[i] = Fraction(1,i+1)
        else:
            frac[i] = (frac[i-1] + Fraction(1,i+1))

    answer = 0
    for i in range(N-1):
        dist = (x[i+1] - x[i]) % MOD
        answer = (answer + dist * frac[i]) % MOD
    for i in range(1,N):
        answer = (answer * i) % MOD

    print(int(answer))

if __name__ == "__main__":
    main()

Fixed submission:
import sys
input = sys.stdin.readline

# 1~nまでの逆元(mod m) O(n)
def mod_inv_list(n:int, m:int):
    # inv_t[i]: iの法mの下での逆元
    inv_t = [0] + [1]
    for i in range(2,n):
        inv_t += [inv_t[m % i] * (m - m//i) % m]
    return inv_t

def main():
    N = int(input())
    x = [int(i) for i in input().split()]
    MOD = 10**9 + 7

    inv_acum = mod_inv_list(N,MOD)
    for i in range(1,N):
        inv_acum[i] = (inv_acum[i] + inv_acum[i-1]) % MOD

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

    for i in range(1,N):
        ans = (ans * i) % MOD

    print(ans)

if __name__ == "__main__":
    main()

Buggy python submission (verdict: Time Limit Exceeded):
import sys
input = sys.stdin.readline
from fractions import Fraction

def main(): 
    N = int(input())
    x = [int(i) for i in input().split()]

    MOD = 10**9+7

    frac = [""] * (N-1)
    for i in range(N-1):
        if i == 0:
            frac[i] = Fraction(1,i+1)
        else:
            frac[i] = (frac[i-1] + Fraction(1,i+1))

    answer = 0
    for i in range(N-1):
        dist = (x[i+1] - x[i]) % MOD
        answer = (answer + dist * frac[i]) % MOD
    for i in range(1,N):
        answer = (answer * i) % MOD

    print(int(answer))

if __name__ == "__main__":
    main()

Fixed submission:
import sys
input = sys.stdin.readline

# 1~nまでの逆元(mod m) O(n)
def mod_inv_list(n:int, m:int):
    # inv_t[i]: iの法mの下での逆元
    inv_t = [0] + [1]
    for i in range(2,n):
        inv_t += [inv_t[m % i] * (m - m//i) % m]
    return inv_t

def main():
    N = int(input())
    x = [int(i) for i in input().split()]
    MOD = 10**9 + 7

    inv_acum = mod_inv_list(N,MOD)
    for i in range(1,N):
        inv_acum[i] = (inv_acum[i] + inv_acum[i-1]) % MOD

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

    for i in range(1,N):
        ans = (ans * i) % MOD

    print(ans)

if __name__ == "__main__":
    main()