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: Runtime Error

Solution code:
# B
import math
n = int(input())
lst = list(map(int, input().split()))
mod = 10**9 + 7
sp = [lst[i+1] - lst[i] for i in range(n-1)]

ex = [1]
t = 1
for j in range(2, n):
    t += 1/j
    ex.append(t)
totalex = 0
for s, e in zip(sp, ex):
    totalex += s*e
    totalex %= mod
ans = (totalex*math.factorial(n-1)) % mod
print(int(ans))

Submission status: Runtime Error

Solution code:
# B
import math
n = int(input())
lst = list(map(int, input().split()))
mod = 10**9 + 7
sp = [lst[i+1] - lst[i] for i in range(n-1)]

ex = [1]
t = 1
for j in range(2, n):
    t += 1/j
    ex.append(t)
totalex = 0
for s, e in zip(sp, ex):
    totalex += s*e
ans = (totalex*math.factorial(n-1)) % mod
print(int(ans))

Submission status: Runtime Error

Solution code:
import math

N = int(input())
xn = list(map(int, input().split()))
mod = 10**9+7

c = [1]
for i in range(1, N-1):
    c.append(c[i-1]+1/(i+1))


ans = 0
for i in range(N-1):
    ans += c[i]*(xn[i+1]-xn[i])

print(int(ans*math.factorial(N-1) % mod))

Submission status: Runtime Error

Solution code:
import math
import sys 
input = sys.stdin.readline
N = int(input())
x = list(map(int,input().split()))
mod = 10**9+7

d = [0]*(N-1)
"""
for i in range(N-1):
    d[i] = x[i+1]-x[i]
    d[i] %= mod
"""

kaijou = 1
for i in range(1,N):
    i %= mod
    kaijou *= i
    kaijou %= mod

ans=0
x=0
"""
def pow_r(x, n):
    """
    O(log n)
    """
    x %= mod
    if n == 0:  # exit case
        return 1
    if n % 2 == 0:  # standard case ① n is even
        return pow_r(x ** 2, n // 2)%mod
    else:  # standard case ② n is odd
        return (x * (pow_r(x ** 2, (n - 1) // 2)))%mod

for i in range(N-1):
    #(i+1)%(mod-2)を計算してyへ
    y = pow_r(i+1,mod-2)
    #x+= kaijou//(i+1))%(10**9+7)
    x += (kaijou*y)%mod
    ans+= (d[i]*x)%mod
"""
print(ans%mod)

Submission status: Runtime Error

Solution code:
import sys 
input = sys.stdin.readline
N = int(input())
x = list(map(int,input().split()))
mod = 10**9+7

d = [0]*(N-1)
for i in range(N-1):
    d[i] = x[i+1]-x[i]
    d[i] %= mod


kaijou = 1
for i in range(1,N):
    kaijou *= i%mod
kaijou %= mod

ans=0
x=0
"""
def pow_r(x, n):
    """
    O(log n)
    """
    x %= mod
    if n == 0:  # exit case
        return 1
    if n % 2 == 0:  # standard case ① n is even
        return pow_r(x ** 2, n // 2)%mod
    else:  # standard case ② n is odd
        return (x * (pow_r(x ** 2, (n - 1) // 2)))%mod

for i in range(N-1):
    #(i+1)%(mod-2)を計算してyへ
    y = pow_r(i+1,mod-2)
    #x+= kaijou//(i+1))%(10**9+7)
    x += (kaijou*y)%mod
    ans+= (d[i]*x)%mod
"""
print(ans%mod)

Submission status: Runtime Error

Solution code:
import math
import sys 
input = sys.stdin.readline
N = int(input())
x = list(map(int,input().split()))

d = [0]*(N-1)
for i in range(N-1):
    d[i] = x[i+1]-x[i]
    d[i] %= 10**9+7

mod = 10**9+7
kaijou = 1
for i in range(1,N):
    kaijou *= i%mod
kaijou %= mod

ans=0
x=0

def pow_r(x, n):
    """
    O(log n)
    """
    x %= mod
    if n == 0:  # exit case
        return 1
    if n % 2 == 0:  # standard case ① n is even
        return pow_r(x ** 2, n // 2)%mod
    else:  # standard case ② n is odd
        return (x * (pow_r(x ** 2, (n - 1) // 2)))%mod

for i in range(N-1):
    #(i+1)**(10**9+5)を計算してyへ
    y = pow_r(i+1,k)
    #x+= kaijou//(i+1))%(10**9+7)
    x += (kaijou*y)%mod
    ans+= (d[i]*x)%mod

print(ans%mod)

Submission status: Runtime Error

Solution code:
N = int(input())
X = list(map(int,input().split()))
import numpy as np
import math
ans = 0
mod = 10**9+7
x=X[-1]
for i in range(N-1):
  for j in range(i+1,N-1):
    a = j-i-1
    ans += ((X[j]-X[i])%mod)*(math.factorial(a)%mod)*(math.factorial(N-1)%mod*pow(math.factorial(a+2),mod-2,mod))%mod
    
for k,y in enumerate(X[:N-1:]):
  a = N-k-2
  ans+=((x-y)%mod)*(math.factorial(a)%mod)*(math.factorial(N-1)%mod*pow(math.factorial(a+1),mod-2,mod))%mod
print(ans%mod)

Submission status: Runtime Error

Solution code:
def solve():
    N = int(input())
    x = list(map(int,input().split()))
    
    mod = int(1e9+7)
    
    distance = []
    for i in range(N-1):
        distance.append((x[i+1]-x[i]))
        
    inv_sum = [0]*N
    inv_sum[0] = 1
    for i in range(1,N):
        inv_sum[i] = (inv_sum[i-1]+1/(i+1))
    
    factorial = 1
    for i in range(1,N):
        factorial *= (i%mod)

    ans = 0
    for i in range(N-1):
        tmp = (factorial*inv_sum[i])%mod
        ans += (distance[i]*tmp)
    
    print(int(ans%mod))

if __name__ == "__main__":
    solve()

Submission status: Runtime Error

Solution code:
def solve():
    N = int(input())
    x = list(map(int,input().split()))
    
    mod = int(1e9+7)
    
    distance = []
    for i in range(N-1):
        distance.append((x[i+1]-x[i]))
        
    inv_sum = [0]*N
    inv_sum[0] = 1
    for i in range(1,N):
        inv_sum[i] = (inv_sum[i-1]+1/(i+1))%mod
    
    ans = 0
    for i in range(N-1):
        ans += (distance[i]*inv_sum[i])%mod
    
    for i in range(1,N):
        ans = ans*(i%mod)
    
    print(int(ans%mod))

if __name__ == "__main__":
    solve()

Submission status: Runtime Error

Solution code:
def solve():
    N = int(input())
    x = list(map(int,input().split()))
    
    mod = int(1e9+7)
    
    distance = []
    for i in range(N-1):
        distance.append((x[i+1]-x[i]))
        
    inv_sum = [0]*N
    inv_sum[0] = 1
    for i in range(1,N):
        inv_sum[i] = (inv_sum[i-1]+1/(i+1))%mod
    
    ans = 0
    for i in range(N-1):
        ans += distance[i]*inv_sum[i]
    
    for i in range(1,N):
        ans = ans*(i%mod)
    
    print(int(ans%mod))

if __name__ == "__main__":
    solve()

Submission status: Runtime Error

Solution code:
N = int(input())
x = list(map(int,input().split()))

mod = int(1e9+7)

distance = []
for i in range(N-1):
    distance.append((x[i+1]-x[i]))

inv_sum = [0]*N
inv_sum[0] = 1
for i in range(1,N):
    inv_sum[i] = (inv_sum[i-1]+1/(i+1))%mod

ans = 0
for i in range(N-1):
    ans += distance[i]*inv_sum[i]

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

print(int(ans%mod))

Submission status: Runtime Error

Solution code:
N = int(input())
x = list(map(int,input().split()))

mod = int(1e9+7)

distance = []
for i in range(N-1):
    distance.append(x[i+1]-x[i])

inv_sum = [0]*N
inv_sum[0] = 1
for i in range(1,N):
    inv_sum[i] = inv_sum[i-1]+1/(i+1)

ans = 0
for i in range(N-1):
    ans += distance[i]*inv_sum[i]

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

print(int(ans%mod))

Submission status: Runtime Error

Solution code:
N = int(input())
x = list(map(int,input().split()))

inv_sum = [0]*(N+2)
inv_sum[0] = 1
for i in range(1,N):
    inv_sum[i] = inv_sum[i-1]+1/(i+1)

mod = int(1e9+7)
ans = 0

for i in range(N-1):
    ans += (x[i+1]-x[i])*inv_sum[i]
    
for i in range(1,N):
    ans = ans*(i%mod)

print(int(ans%mod))

Submission status: Runtime Error

Solution code:
N = int(input())
x = list(map(int,input().split()))

inv_sum = [0]*N
inv_sum[0] = 1
for i in range(1,N):
    inv_sum[i] = inv_sum[i-1]+1/(i+1)

mod = int(1e9+7)
ans = 0

for i in range(N-1):
    ans += (x[i+1]-x[i])*inv_sum[i]
    
for i in range(1,N):
    ans = ans*(i%mod)

print(int(ans%mod))

Submission status: Runtime Error

Solution code:
N = int(input())
x = list(map(int,input().split()))

inv_sum = [0]*N
inv_sum[0] = 1
for i in range(1,N):
    inv_sum[i] = inv_sum[i-1]+1/(i+1)

mod = int(1e9+7)
ans = 0

for i in range(N-1):
    ans += (x[i+1]-x[i])*inv_sum[i]
    
for i in range(1,N):
    ans = (ans*i)

print(int(ans%mod))

Submission status: Runtime Error

Solution code:
N = int(input())
x = list(map(int,input().split()))

inv_sum = [0]*N
inv_sum[0] = 1
for i in range(1,N):
    inv_sum[i] = inv_sum[i-1]+1/(i+1)

mod = int(1e9+7)
ans = 0

for i in range(N-1):
    ans += (x[i+1]-x[i])*inv_sum[i]
    
for i in range(1,N):
    ans = (ans*i)

print(int(ans%mod))

Submission status: Runtime Error

Solution code:
import sys
from collections import defaultdict, deque
import bisect
from heapq import *
from math import factorial, ceil, floor
from collections import defaultdict
import itertools
sys.setrecursionlimit(200000)
input = sys.stdin.readline

# N, M, = map(int, input().split())
# N = int(input())
# L = [int(v) for v in input().split()]
# L = [[int(v) for v in input().split()] for _ in range(N)]
# L = [int(input()) for _ in range(N)]
# L = [list(input().strip())  for _ in range(N)]
# S = input().strip()

N, = map(int, input().split())
L = [int(v) for v in input().split()]

def f(n):
    v = 0
    for i in range(1, n + 1):
        v += 1 / i
    return v

p = 0
for i in range(N - 1):
    p += (L[i + 1] - L[i]) * f(i + 1)
    
pa = 10 ** 9 + 7
for i in range(1, N):
    p = p * i

print(int(p % pa))

Submission status: Runtime Error

Solution code:

import math

mod=10**9+7
n=int(input())
x=list(map(int,input().split()))

kai=math.factorial(n-1)
a=[]
for i in range(n-1):
    a.append(x[i+1]-x[i])
ans=0
count=0
a1=sum(a)
for i in range(n-1):
    count+=kai/(i+1)
    ans+=a[i]*count
print(int(ans%mod))

Submission status: Runtime Error

Solution code:
import statistics
import math

MOD = 10**9+7


def main():
    n = int(input())
    x = list(map(int, input().split()))

    c = 0
    t = 0
    for i in range(n-1):
        c += 1/(i+1) % MOD
        t += c * (x[i+1]-x[i])
        t %= MOD

    print(int(t * math.factorial(n-1) % MOD))


main()

Submission status: Runtime Error

Solution code:
import statistics
import math


def main():
    n = int(input())
    x = list(map(int, input().split()))

    c = 0
    t = 0
    for i in range(n-1):
        c += 1/(i+1)
        t += c * (x[i+1]-x[i])
        t %= 10**9+7

    print(int(t * math.factorial(n-1) % (10**9+7)))


main()

Submission status: Runtime Error

Solution code:
import statistics
import math


def main():
    n = int(input())
    x = list(map(int, input().split()))

    c = 0
    t = 0
    for i in range(n-1):
        c += 1/(i+1)
        t += c * (x[i+1]-x[i])
        t %= 10**9+7

    print(t * math.factorial(n-1) % (10**9+7))


main()

Submission status: Runtime Error

Solution code:
#include <bits/stdc++.h>
using namespace std;

int kaijyo(int k) {
    if (k ==1) return 1;
    else {
        return k*kaijyo(k-1);
    }
}
int main(void){
    // Your code here!
    int n;
    cin >> n;
    vector<long long> x(n+1);
    for ( int i = 1 ; i <= n ; i++) {
        cin >> x.at(i);
    }
    long long  sum = 0;
    for (int i = 1; i < n;i++) {
        for (double j =1; j <= i;j++) {
            sum+=(x.at(i+1)-x.at(i))*kaijyo(n-1)/j;
        }
    }
    int result;
    result = sum % (1000000000+7);
    cout << result << endl;
}

Submission status: Runtime Error

Solution code:
import collections as c
ip = lambda : map(int, input().split())
# ##############
N = int(input())
l = list(ip())
d = []
for i in range(N-1):
    d.append(l[i+1]-l[i])

ans = 0;mod = 1e9+7;c=0;l=1
for i in range(N-1):
    c = (i+1)*c + l
    ans = ((i+1)*ans + c*d[i])%mod
    l = l*(i+1)%mod
print(int(ans%mod))

Submission status: Runtime Error

Solution code:
import collections as c
ip = lambda : map(int, input().split())
# ##############
N = int(input())
l = list(ip())
d = []
for i in range(N-1):
    d.append(l[i+1]-l[i])

ans = 0;mod = 1e9+7;c=0;l=1
for i in range(N-1):
    c = (i+1)*c + l
    ans = (i+1)*ans + c*d[i]
    ans%=mod
    l *= i+1
print(int(ans%mod))

Submission status: Runtime Error

Solution code:
N = int(input())
X = list(map(int, input().split()))
mod = 10**9 + 7
  
SUM = 0
for i in range(len(X) - 1):
  b = X[i+1] - X[i]
  c = 0
  for j in range(i+1):
    c = c + 1/(j+1)
  SUM += b*c

import math
Nk = math.factorial(N-1)

a = SUM * Nk % mod
print(int(a))

Submission status: Runtime Error

Solution code:
a = int(input())
b_list = list(map(int, input().split()))


c = a-1
d = 1//c
q = 1
for i in range(c):
    q = q * i+1
print(q)

f_list = []

for e in range(c):
    f_list.append(b_list[e+1])

g = sum(f_list)
g = g / c
h = g*q
print(h)

Submission status: Runtime Error

Solution code:
N = int(input())
X = list(map(int, input().split()))
mod = 10**9 + 7
  
SUM = 0
for i in range(len(X) - 1):
  b = X[i+1] - X[i]
  c = 0
  for j in range(i+1):
    c = c + 1/(j+1)
  SUM += b*c

Nk = 1
for i in range(1,N):
  Nk = Nk * i
a = (SUM * Nk) % mod
print(int(a))

Submission status: Runtime Error

Solution code:
import collections as c
ip = lambda : map(int, input().split())
# ##############
N = int(input())
l = list(ip())
d = []
for i in range(N-1):
    d.append(l[i+1]-l[i])

ans = 0;mod = 1e9+7;c=0;l=1
for i in range(N-1):
    # if i == 0:
    #     c = 1
    # else:
    #     c = (c*(i+1)/(i*l) + 1)*l
    # print(ans)
    # ans = (ans*(i+1)%mod + d[i]*c)%mod
    # l = (l*i+1)%mod
    c+=1/(i+1)
    ans += c*d[i]
    ans%=mod
    l *= i+1
print(int(ans*l%mod))

Submission status: Runtime Error

Solution code:
import numpy as np

N = int(input())
X = list(map(int, input().split()))
mod = 10**9 + 7
  
bl = []
for i in range(len(X) - 1):
  b = X[i+1] - X[i]
  bl.append(b)

C = []
for i in range(1, len(X)):
  c = 0
  for j in range(i):
    c = c + 1/(j+1)
  C.append(c)
B = [b*c for b,c in zip(bl,C)]
B = np.sum(B)

Nk = 1
for i in range(1,N):
  Nk = Nk * i
a = (B * Nk) % mod
print(int(a))

Submission status: Runtime Error

Solution code:
import math

n = int(input())
x = [0] + list(map(int, input().split()))
mod = 10**9 + 7

f = [1] * (n+1)
for i in range(1,n+1):
    f[i] = f[i-1] * i % mod

s = [0] + [pow(i, mod-2, mod) for i in range(1,n+1)]
for i in range(n):
    s[i+1] = (s[i+1] + s[i]) % mod

ans = 0.0
for i in range(1,n):
    ans += ((x[i+1] - x[i]) * s[i]) % mod
ans = int(ans * math.factorial(n-1)) % mod
print(ans)

Submission status: Runtime Error

Solution code:
n = int(input())
x = list(map(int, input().split()))
d = [x[i+1] - x[i] for i in range(n - 1)]

def fact(m):
  return m * fact(m-1) % 1000000007 if m != 1 else 1
 
fact_n_1 = fact(n-1)

e = [None] * (n-1)
for i in range(n-1):
  if i == 0:
    e[i] = fact_n_1
  else:
    e[i] = (e[i - 1] + fact_n_1 // (i + 1)) % 1000000007

print(sum(e_i * d_i % 1000000007
          for i, (d_i, e_i) 
          in enumerate(zip(d, e))) % 1000000007 )

Submission status: Runtime Error

Solution code:
import math

N = int(input())

x = [0] * N
x = list(map(int, input().split()))

def calc_c(n):
    rslt = 0
    for i in range(1,n+1):
        rslt += 1/i
    return rslt


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



print(int(ans * math.factorial(N - 1)) % 1000000007)

Submission status: Runtime Error

Solution code:
N = int(input())
x = list(map(int, input().split()))
MOD = 10 ** 9 + 7
count = 0

import math
for i in range(N - 2):
    count += (x[N - 1] - x[i]) * math.factorial(N - 1) / math.factorial(N - 2 - i)
count += x[N - 1] - x[N - 2]

print(int(count % MOD))

Submission status: Runtime Error

Solution code:
N = int(input())
X = list(map(int,input().split()))
import math
ans = 0
mod = 10**9+7
a = 0
ans = 0
for i in range(N-1):
  a += (1/(i+1))*math.factorial(N-1)
  ans += (X[i+1]-X[i]%mod)*(a%mod)

print(int(ans)%mod)

Submission status: Runtime Error

Solution code:
def factorial_cor(n):
    fact = 1
    for integer in range(1, n + 1):
        fact *= integer
    return fact
n = int(input())
X = list(map(int, input().split()))
sub = []
sum1 = 0
ans = 0
for i in range(1, n):
  sub.append(X[i]-X[i-1])
for i in range(1,n):
  sum1 = 0
  for j in range(1, i+1):
    sum1 += 1/j
  ans += sub[i-1]*sum1
print(ans*factorial_cor(n-1)/1000000007)

Submission status: Runtime Error

Solution code:
from math import factorial
import itertools

N=int(input())
x=list(map(int, input().split()))

C=0
t=[math.factorial(N-i) for i in range(2,N)]

t=list(itertools.accumulate(t))
t.append(math.factorial(N-1))
t.reverse()
T=list(itertools.accumulate(t))

C=[(x[j]-x[j-1])*T[j-1] for j in range(1,N) ]

print(sum(C)%((10 ** 9)+7))

Submission status: Runtime Error

Solution code:
N = int(input())
X = list(map(int,input().split()))
import math
ans = 0
mod = 10**9+7
a = 0
ans = 0
for i in range(N-1):
  a += 1/(i+1)
  ans += (X[i+1]-X[i])*a

ans *= math.factorial(N-1)
print(int(ans)%mod)

Submission status: Runtime Error

Solution code:
from math import factorial
from itertools import accumulate

N=int(input())
x=list(map(int, input().split()))

C=0
t=[math.factorial(N-i) for i in range(2,N)]

t=list(itertools.accumulate(t))
t.append(math.factorial(N-1))
t.reverse()
T=list(itertools.accumulate(t))

C=[(x[j]-x[j-1])*T[j-1] for j in range(1,N) ]

print(sum(C)%((10 ** 9)+7))

Submission status: Runtime Error

Solution code:
N=int(input())
x = list(map(int,input().split()))
dp = [[0]*(N+2) for _ in range(2)]
dp[1][1] = -1
dp[1][2] = 1
a = 1
MOD = 10**9+7
for i in range(2,N):
    a = (a*(i-1))%MOD
    i2 = (i+1)%2+1
    i3 = i%2+1
    for j in range(1,i+2):
        if i+1==j:
            dp[i3][j] = (dp[i2][j-1]*(i-0) + a)%MOD
        elif j==1:
            dp[i3][j] = (dp[i2][j]*(i-1) - a)%MOD
        else:
            dp[i3][j] = (dp[i2][j]*(i-j) + dp[i2][j-1] * (j-1))%MOD
res = 0
n = (N-1)%2
for i in range(N):
    res += (dp[n][i+1]*x[i])%MOD
print(res%MOD)

Submission status: Runtime Error

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)

sum=0
ss=0
for i in range(1,n):
  ss+=1/i
  sum+=ss*dist[i-1]
ans=int((sum*f)%MOD)


print(ans)

Submission status: Runtime Error

Solution code:
import math

N = int(input())
x = list(map(int,input().split()))

diff = []

for i in range(N-1):
  d = x[i+1] - x[i]
  diff.append(d)
  
ans = 0
fact = math.factorial(N-1)
for i in range(N-1):
  n = 0
  for j in range(i+1):
    n = n + (fact / (j+1))
  ans = ans + diff[i] * n

mod = ans % (10**9 + 7)
print(int(mod))

Submission status: Runtime Error

Solution code:
import math

N = int(input())
x = list(map(int,input().split()))

diff = []

for i in range(N-1):
  d = x[i+1] - x[i]
  diff.append(d)
  
ans = 0

fact = math.factorial(N-1)
for i in range(N-1):
  n = 0
  for j in range(i+1):
    n = n + (fact / (j+1))
  #print("n=" + str(n))
  #print(diff[i])
  ans = ans + diff[i] * n

mod = ans % (10**9 + 7)
print(int(mod))

Submission status: Runtime Error

Solution code:
N = int(input())
x = list(map(int,input().split()))
xx = [x[-i]-x[-i-1] for i in range(1, N)]
xxx = [[] for i in range(N-1)]
xxx[0].append(xx[0])
for i in range(1, N-1):
    for l in range(i):
        xxx[i].append(xxx[i-1][l]+xx[i])
    xxx[i].append(xx[i])
for i in range(N-1):
    xxx[i].reverse()
MOD = 10**9+7
p = 1
for i in range(1, N):
    p *= i
S = [[1]]
ans = 0
for i in range(3, N+1):
    t = []
    for l in range(i-3):
        t.append(S[i-3][l])
    t.append(S[i-3][-1]/(i-1))
    t.append(1/(i-1))
    for l in range(i-1):
        ans += xxx[i-2][l]*t[l]
    S.append(t)
ans += xx[0]
print(int((ans*p)%MOD))

Submission status: Runtime Error

Solution code:
import math
N = int(input())
x = list(map(int,input().split()))
ans = 0
tmp = 0

frac = math.fractorial(N-1)%1000000007
for i in range(N-1):
    ans += (x[N-1] - x[i])/(i+1)*frac
    if ans > 1000000007:
        ans = ans % 1000000007

print(ans)

Submission status: Runtime Error

Solution code:
import math
N = int(input())
x = list(map(int,input().split()))
ans = 0
tmp = 0

for i in range(N-1):
    ans += (x[N-1] - x[i])/(i+1)*math.factorial(N-1)
    if ans > 1000000007:
        ans = ans % 1000000007

print(ans)

Submission status: Runtime Error

Solution code:
n,*c=map(int,open(0).read().split())
mod=10**9+7
dif=[j-i for i,j in zip(c,c[1:])]
fac=[1]*n
#階乗。毎回modで割っても大丈夫
for i in range(1,n):
    fac[i]=fac[i-1]*i%mod

inv=[pow(i,mod-2,mod) for i in range(n)]
#itertoolより。
acc_inv=list(accumulate(inv))
ans=0
for k in range(1,n):
    ans+=dif[k-1]*acc_inv[k]
    ans%=mod
print(ans*fac[-1]%mod)

Submission status: Runtime Error

Solution code:
from math import factorial

N = int(input())
x_list = list(map(str, input().split()))
for i in range(N):
    x_list[i] = int(x_list[i])

result = 0
for i in range(N-1):
    keisu = factorial(N-2-i)*factorial(i)
    for j in range(N-1-i):
        result += keisu*(x_list[N-1] - x_list[j])*(int(factorial(N-2-j) / factorial(i) / factorial(N-2-j - i)))
        result %= (1000000000+7)
print(result)

Submission status: Runtime Error

Solution code:
import math
N = int(input())
x = list(map(int,input().split()))
ans = 0
tmp = 0

for i in range(N-1):
    ans += (x[N-1] - x[i])/(i+1)*math.factorial(N-1)
    if ans > 1000000007:
        ans = ans % 1000000007

print(int(ans))

Submission status: Runtime Error

Solution code:
import math

n = int(input())
x = [int(i) for i in input().split()]
mod = 10**9+7
ans = 0
for i in range(n-1):
    p = 0
    for j in range(1,i+2):
        p += 1/j
    ans += ((x[i+1] - x[i]) * p)
ans = int(ans * math.factorial(n-1) % mod)
print(ans)

Submission status: Runtime Error

Solution code:
# import numpy as np
# import math
import sys
input = sys.stdin.readline


def main():
    N = int(input())
    x = list(map(int,input().split()))

    mod = 10 ** 9 + 7

    res = 0
    frac = 1
    sum = 0

    for i in range(N-1):
        diff = x[i+1] - x[i]
        sum += 1 / (i+1)
        frac *= i + 1
        res += diff * sum
        res %= mod

    res *= frac
    res %= mod
    res = int(res)

    print(res)



main()

Submission status: Runtime Error

Solution code:
import math
import sys
sys.setrecursionlimit(10**6)

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)
sum=0.0
ss=0
for i in range(1,n-1+1):
  ss+=f/i
  sum+=ss*dist[i-1]%MOD
ans=int(sum%MOD)
print(ans)

Submission status: Runtime Error

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)
sum=0.0
ss=0
for i in range(1,n-1+1):
  ss+=f/i
  sum+=ss*dist[i-1]%MOD
ans=int(sum%MOD)
print(ans)

Submission status: Runtime Error

Solution code:
n = int(input())
x = list(map(int, input().split()))
d = [x[i+1] - x[i] for i in range(n - 1)]
 
def fact(m):
  return m * fact(m-1) if m != 1 else 1 

fact_n_1 = fact(n-1)

e = {}
for i in range(n-1):
  if i == 0:
    e[i] = fact_n_1
  else:
    e[i] = e[i - 1] + fact_n_1 // (i + 1)

print(sum(e[i] * d_i % 1000000007
          for i, d_i 
          in enumerate(d)) % 1000000007 )

Submission status: Runtime Error

Solution code:
import math
import itertools

N=int(input())
x=list(map(int, input().split()))

C=0
t=[math.factorial(N-i) for i in range(1,N+1)]
T=list(itertools.accumulate(t))

for j in range(1,N+1):
    C=C+(x[j]-x[j-1])*T[j]

print(C%(10**9+7))

Submission status: Runtime Error

Solution code:
#B
mod = 10**9 + 7
N = int(input())
X = list(map(int,input().split()))

perm = 1
for i in range(N-1):
    perm*=(i+1)

D = []
for i in range(N-1):
    D.append(X[i+1]-X[i])
    
p_list = []
now = 0
for i in range(N-1):
    now+=(1/(i+1))
    p_list.append(now)
ans = 0
for i in range(N-1):
    d,p = D[i],p_list[i]
    ans+=d*p
    
print(int(ans*perm)%mod)

Submission status: Runtime Error

Solution code:
#B
mod = 10**9 + 7
N = int(input())
X = list(map(int,input().split()))

perm = 1
for i in range(N-1):
    perm*=(i+1)

D = []
for i in range(N-1):
    D.append(X[i+1]-X[i])
    
p = 0
ans = 0
for i,d in enumerate(D):
    p+=(1/(i+1))
    ans+=d*p
    
print(int(ans*perm)%mod)

Submission status: Runtime Error

Solution code:
#B
mod = 10**9 + 7
N = int(input())
X = list(map(int,input().split()))

D = []
for i in range(N-1):
    D.append(X[i+1]-X[i])
    
p = 0
kai = 1
ans = 0
for i in range(N-1):
    kai*=(i+1)
    p+=(1/(i+1))
    ans+=D[i]*p
    
print(int(ans*kai)%mod)

Submission status: Runtime Error

Solution code:
import math
import itertools

N=int(input())
x=list(map(int, input().split()))

t=[]

for i in range(1,N+1):
    v=math.factorial(N-i)
    t.append(v)
T=itertools.accumulate(t)

for j in range(1,N+1):
    C=C(x[j]-x[j-1])*T[i]

print(C%(10**9+7))

Submission status: Runtime Error

Solution code:
import numpy as np
from math import factorial


def main():
    mod = 10**9 + 7

    N = int(input())
    slimes = np.array(list(map(int, input().split())))
    costs = slimes[1:] - slimes[:-1]

    base = np.array([i*2 + 1 for i in range(1, N-1)])

    w = factorial(N-1) // 6
    base *= w

    base = np.insert(base, 0, 0)
    base += factorial(N-1)
    base = base % mod

    costs = base * costs
    costs = costs % mod

    total = sum(costs)
    print(total % mod)

    # stacked = costs
    #     #
    # added = [0]*(N-1)
    # normal = dict()
    #
    # for i in range(1, N):
    #     normal[i] = set(range(1, i))
    # print("normal", normal)
    #
    # for comb in combinations:
    #     print(comb)
    #     for c in range(1, N-1):
    #         idx = comb.index(c)
    #         # 自分+1が後ろに居れば0
    #         if c+1 in comb[idx:]:
    #             print(c, "correct")
    #             continue
    #         s = set(comb[:idx]) - normal[c]
    #         print(c, s)
    #         for i in s:
    #             added[i-1] += 1
    #
    # print(added)


if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
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)

Submission status: Runtime Error

Solution code:
import math

N = int(input())
X = list(map(int, input().split()))
MOD = 10 ** 9 + 7

def combinations_count(n, r):
    return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))


def main(N, X):
    ans = 0
    for i in range(N-1):
        ans += X[i+1] * math.factorial(N-1) /2

        for j in range(i+2, N):
            if N-1-(j-i+1)>=0 and j-i+1>=0:
                ans += X[j] * combinations_count(N-1, j-i+1) * math.factorial(j-i-1)
    print((ans- sum(X[-1:])*math.factorial(N-1)) % MOD)
main(N, X)

Submission status: Runtime Error

Solution code:
import numpy as np
import copy

N = int(input())
x = list(map(int, input().split()))

if N == 2:
    print(x[1]-x[0])
elif N == 3:
    print(2*(x[2]-x[1]) + x[2]-x[0] + x[1]-x[0])
else:
    l = [[x[i+j] - x[i] for i in range(N-j)] + [0]*(j-1) for j in range(1, N)]

    move = [0, 0, 0, 0, np.array([[2, 2, 5], [1, 3, 0], [2, 0, 0]])]
    move += [np.array([[1]*(N-1)] + [[0] * (N-1) for _ in range(2, N)]) for i in range(5, N+1)]
    
    for i in range(5, N+1):
        for j in range(i-1):
            nmove = np.insert(np.insert(move[i-1], i-2, 0, axis=0).T, j, 0, axis=0)
            for k in range(j):
                a = copy.copy(nmove[k])
                a = np.insert(a, j-k, 0)
                nmove[k] = np.delete(a, i-1, 0)
            move[i] += nmove.T
    
    print(np.sum(l*move[N]))

Submission status: Runtime Error

Solution code:
import math
N = int(input())
X = list(map(int, input().split()))
MOD = 10**9 + 7
ans = 0

count_max = math.factorial(N-1)
L = [[count_max]]

for i in reversed(range(1, N+1)):
  l = []
  for j in range(1,i+1):
    ans += (X[i-1]-X[j-1]) * count_max * (1/j - 1/i)
    
ans %= MOD
print(int(ans))

Submission status: Runtime Error

Solution code:
#############  B  #############
import numpy as np
N = int(input())
x = list(map(int, input().split()))
li = np.array(range(2**(N-2),low+dif*(N-1),2**(N-3)))
d = np.diff(x,n=1)
SUM = np.dot(li,d)
print(SUM)

Submission status: Runtime Error

Solution code:
import numpy as np
n = int(input())
x = input().split(" ")
x = np.array(list(map(int, x)))
print(x)
num = 1
ans = 0
minus = 0
for i in range(n-1):
    num *= n-(i+1)
    minus += x[i]
    ans += ((i+1) * x[-1] - minus) / (num)
print((ans*num) % (10**9 + 7))

Submission status: Runtime Error

Solution code:
mod=10**9+7
N=int(input())
X=list(map(int,input().split()))
dist=[X[i+1]-X[i] for i in range(N-1)]

def kaijo(n):
    if n == 1:
        return n
    return (n*kaijo(n-1))%mod
f=kaijo(N-1)  
ans=0
for i in range(N-1):
  mi=0
  ans=(ans+dist[i]*((f+(i*2**((N-2)*(N-1)/2-1))%mod-mi)%mod))%mod
  mi+=i+1

print(int(ans))

Submission status: Runtime Error

Solution code:

import math
MOD=10**9+7


n=int(input())
li=list(map(int,input().split()))

rr=[]
for i in range(n-1):
    tmp=(li[i+1]-li[i])
    rr.append(tmp)

k=[]    
f=math.factorial(n-1)
ss=0
for i in range(1,n-1+1):
    ss+=1.0/i*f
    k.append(ss)
sum=0
for i,j in zip(rr,k):
   sum+=(i%MOD)*(j%MOD)

ans=int(sum%MOD)
print(ans)