Problem:


Score : 600 points

Problem Statement

There are N slimes standing on a number line.
The i-th slime from the left is at position x_i.

It is guaruanteed that 1 \leq x_1 < x_2 < \ldots < x_N \leq 10^{9}.

Niwango will perform N-1 operations. The i-th operation consists of the following procedures:

Choose an integer k between 1 and N-i (inclusive) with equal probability.

Move the k-th slime from the left, to the position of the neighboring slime to the right.

Fuse the two slimes at the same position into one slime.

Find the total distance traveled by the slimes multiplied by (N-1)! (we can show that this value is an integer), modulo (10^{9}+7). If a slime is born by a fuse and that slime moves, we count it as just one slime.

Constraints

2 \leq N \leq 10^{5}

1 \leq x_1 < x_2 < \ldots < x_N \leq 10^{9}

x_i is an integer.

Subtasks

400 points will be awarded for passing the test cases satisfying N \leq 2000.

Input

Input is given from Standard Input in the following format:

N
x_1 x_2 \ldots x_N

Output

Print the answer.

Sample Input 1

3
1 2 3

Sample Output 1

5

With probability \frac{1}{2}, the leftmost slime is chosen in the first operation, in which case the total distance traveled is 2.

With probability \frac{1}{2}, the middle slime is chosen in the first operation, in which case the total distance traveled is 3.

The answer is the expected total distance traveled, 2.5, multiplied by 2!, which is 5.

Sample Input 2

12
161735902 211047202 430302156 450968417 628894325 707723857 731963982 822804784 880895728 923078537 971407775 982631932

Sample Output 2

750927044

Find the expected value multiplied by (N-1)!, modulo (10^9+7).



Sample input:
3
1 2 3


Sample output:
5

Submission status: Time Limit Exceeded

Solution code:
import math
MOD=10**9+7
n=int(input())
li=list(map(int,input().split()))
dist=[]
for i in range(n-1):
  dist.append(li[i+1]-li[i])
  

f=math.factorial(n-1)

ss=0
rr=[]
for i in range(1,n):
  ss+=(f)//i
  rr.append(ss%MOD)

sum=0
for i,j in zip(dist,rr):
  sum+=i*j%MOD
ans=int((sum)%MOD)


print(ans)

Submission status: Time Limit Exceeded

Solution code:
import sys
input = sys.stdin.readline

mod = 10**9+7


def f(N, x):
    n = 1
    for i in range(1, N):
        if i != x:
            n *= i
        n %= mod
    return n


def main():
    N = int(input())
    X = list(map(int, input().split()))
    D = [0 for i in range(N)]

    for i in range(N-1):
        D[i] = X[i+1] - X[i]

    ans = 0
    r = 0
    for i in range(1, N):
        r += f(N, i)
        ans += D[i-1] * r
        ans %= mod
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
P = list(map(int,input().split()))
num = 0
i = 0
import math
import copy

first = 1
second = 1
mod_list = [0] * (N-1)
mod_list[0] = first
mod_list[1] = second
for i,_ in enumerate(mod_list):
  if i < 2:
    pass
  else:
    mod_list[i] = math.factorial(i)

mod_list.reverse()

count = 0

while count < math.factorial(N-1):
  K = copy.deepcopy(P)
  k = count
  zero = [0] * (N-1)
  for i,j in enumerate(mod_list):
    q = k // mod_list[i]
    mod = k % mod_list[i]
    k = mod
    zero[i] = q
  #print("zero: {}".format(zero))
  for i in zero:
      num += K[i+1] - K[i]
      K.pop(i)

    
    
  count += 1

print(num % (10^9+7))

Submission status: Time Limit Exceeded

Solution code:
import sys
read = sys.stdin.buffer.read

sys.setrecursionlimit(100002)
def myfact(n):
    if n < 2:
        return 1
    else:
        return n * myfact(n - 1)
n,*X=map(int,read().split())
last=X[-1]
X=[last-i for i in X[:-1]]
mod = 10**9+7
f=myfact(n-1)
s = sum(f//i*item for i,item in enumerate(X,1))
print(s%mod)

Submission status: Time Limit Exceeded

Solution code:
import sys
input = sys.stdin.readline


def main():
    N = int(input())
    X = list(map(int, input().split()))
    D = [0 for i in range(N)]

    for i in range(N-1):
        D[i] = X[i+1] - X[i]

    n = 1
    for i in range(1, N):
        n *= i

    ans = 0
    r = 0
    mod = 10**9+7
    for i in range(1, N):
        r += n // i
        r %= mod
        ans += D[i-1] * r
        ans %= mod
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
import sys
input = sys.stdin.readline


def f(N, x):
    n = 1
    for i in range(1, N):
        if i != x:
            n *= i
    return n


def main():
    N = int(input())
    X = list(map(int, input().split()))
    D = [0 for i in range(N)]

    for i in range(N-1):
        D[i] = X[i+1] - X[i]

    ans = 0
    r = 0
    mod = 10**9+7
    for i in range(1, N):
        r += f(N, i)
        r %= mod
        ans += D[i-1] * r
        ans %= mod
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
import sys
input = sys.stdin.readline


def f(N, x):
    n = 1
    for i in range(1, N):
        if i != x:
            n *= i
    return n


def main():
    N = int(input())
    X = list(map(int, input().split()))
    D = [0 for i in range(N)]

    for i in range(N-1):
        D[i] = X[i+1] - X[i]

    ans = 0
    r = 0
    for i in range(1, N):
        r += f(N, i)
        ans += D[i-1] * (r % (10**9+7))
    print(ans % (10**9+7))


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
from math import factorial

N = int(input())
X = list(map(int, input().split()))
X_dis = [ X[-1]-X[i] for i in range(N-1) ]
mod = 10**9 + 7
fact = factorial(N-1)
ans = 0


for i in range(N-1):
    ans = ans + X_dis[i] * (fact // (i+1))

print(ans % mod)

Submission status: Time Limit Exceeded

Solution code:
import sys
input = sys.stdin.readline


def f(N, x):
    n = 1
    for i in range(1, N):
        if i != x:
            n *= i
    return n


def main():
    N = int(input())
    X = list(map(int, input().split()))
    D = [0 for i in range(N)]

    for i in range(N-1):
        D[i] = X[i+1] - X[i]

    ans = 0
    r = 0
    for i in range(1, N):
        r += f(N, i)
        ans += D[i-1] * r
        ans %= 10**9+7
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
x = list(map(int, input().split()))
ans = 0
p = 1
for i in range(1, n):
  p *= i
for i in range(n-1):
  ans += (x[n-1]-x[i])*p//(i+1)
ans %= 1000000007
print(ans)