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:
N = int(input())
X = [int(x) for x in input().split()]

C = 1000000007

dp = []
for i in range(N + 1):
    dp.append([0] * (i + 3))
dp[0][1] = 1
for i in range(1, N + 1):
    for j in range(1, i + 2):
        dp[i][j] = dp[i-1][j-1] + dp[i-1][j] * i

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

ans = 0
for i in range(N):
    for j in range(i + 3):
        for k in range(N - i):
            dp[i][j] *= k
            dp[i][j] %= C
            dp[i][j] *= j
            dp[i][j] %= C
            dp[i][j] *= dis[i]
            ans += dp[i][j]
            ans %= C

print(ans)

Submission status: Runtime Error

Solution code:
#!/usr/bin/env python3
import sys
import math
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
n = int(readline())
x = list(map(int,input().split()))
cum = [0]*n # 後ろからの累積わ sum(x[i:])
sum_x = sum(x)
cum[0] = sum_x
for i in range(1,n):
    cum[i] = cum[i-1] - x[i-1]
#print(n,x,cum)
e = 0
# x[0] ~ x[n-2]までのそれ以降の全点までの距離の平均を足し合わせる
for i in range(n-1):
    e += (cum[i] - (n-i)*x[i]) / (n-i-1)  
mod = 10**9+7
#print(e,math.factorial(n-1))
print(int(e * math.factorial(n-1))%mod)

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 = []
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)

Submission status: Runtime Error

Solution code:
n = int(input())
X = list(map(int, input().split()))

ans = 0
multi = 1
for i in range(1,n):
    multi *= i

n_two = 1
for j in range(1,n-1):
    n_two *= j


for i in range(n-1):
    base = (X[i+1] - X[i])*multi + (X[i+1]-X[i])*(2*i*n_two/2)
    ans += int(base)
    ans = ans % (10**9+7)

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

Submission status: Runtime Error

Solution code:
import math

dev = 10**9+7

def kai(n):
    if n == 1:
        return 1
    else:
        return (n * kai(n-1))


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

s = 0
now = 0
#f = kai(N-1) % dev
f = math.factorial(N-1)
for i in range(N-1):
  now += (f/(i+1)) % dev
  s += (((X[i+1]-X[i])%dev)*now) % dev

print(int(s%dev))

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*j%MOD

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

Submission status: Runtime Error

Solution code:
import math
n=int(input())
x=list(map(int,input().split()))
ans=0
for i in range(n-1):
    for j in range(1,n-i):
        f=math.factorial(j-1)
        ff=math.factorial(i+1)
        d=x[i+j]-x[i]
        ans=(ans+d*f*ff)%(1e+9+7)
print(int(ans))

Submission status: Runtime Error

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

N = 1e+9 + 7

tmp = 1
l = []
ln = []
for i in range(n-1):
    tmp *= (i+1)%N
    l.append(tmp)
for k in range(n-1):
    ln.append(int(tmp/(k+1))%N)

a = 0
b = 1

for i in range(n-1):
    b *= (i+1)%N
    a += ((ln[i]%N) * (x[-1]-x[i]))%N

b *= n
print(int(a%N))

Submission status: Runtime Error

Solution code:
N=int(input())
X=list(map(int,input().split()))
a=1
c=10**9+7
for i in range(N-1):
    a*=i+1
l1=list()
l2=list()
ans=0
for i in range(N):
    A=a
    for j in range(N-i-2):
        l1.append(a/((j+2)*(j+1)))
        A-=a/((j+2)*(j+1))
    for k in range(N)[i+1:]:
        l2.append(X[k]-X[i])
    l1.append(A)
l1.pop(-1)
for n1,n2 in zip(l1,l2):
    ans+=n1*n2
print(int(ans%c))

Submission status: Runtime Error

Solution code:
import math

def comb(a,b):
	return(int(math.factorial(a)/math.factorial(a-b)/math.factorial(b)))

n=int(input())
l=list(map(int,input().split()))
ll=[]
for i in range(n-1):
	ll.append(l[i+1]-l[i])

m=0
for i in range(n-1):
    k=0
    for j in range(i+1):
        k+=(comb(n-2-j,n-2-i))*(sum(ll[j:]))
    m+=math.factorial(n-1)*(k/((n-1-i)*comb(n-1,n-1-i)))
print(int(m%(10**9+7)))

Submission status: Runtime Error

Solution code:
a=input()
b=input()
N=int(a)
x=b.split()
d=[]
EX=0
L=1
M=1
O=0
for i in range(N-1):
	d.append(int(x[i+1])-int(x[i]))
	L=L*(i+1)
L=L/N
for j in range(N-1):
	EX+=d[j]
for k in range(N-1):
	M=M*(k+1)
	if(k>1):
		O+=L/M
		EX+=O*d[k]
print(EX%1000000007)

Submission status: Runtime Error

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

def fact(m):
  return m * fact(m-1) if m != 1 else 1

d = [x[i+1] - x[i] for i in range(n - 1)]
fact_n_1 = fact(n-1)
print(sum(sum(fact_n_1 // (j + 1) 
              for j 
              in range(i + 1)) * d_i % 1000000007
          for i, d_i 
          in enumerate(d)) % 1000000007 )

Submission status: Runtime Error

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


kai = 1
ans = 0
for k in range(N-1):
    kai*=(k+1)
    ans += int(perm*D[k]*(2-(1/kai)))
    ans%=mod
    print(kai)
    

ans%=mod
print(ans)

Submission status: Runtime Error

Solution code:
import math

n = int(input())
x = list(map(int, input().split()))
s = sorted(x)

d = 0
d1 = 0
for i in range(n-1):
    d2 = 0
    d1 += s[i+1] - s[i]
    for j in range(i+1, n):
        d2 += s[j] - s[i]
    d += (d1 + d2) / (n-i)
    
f = math.factorial(n-1)
m = d * f % (10**9 + 7)
print(int(m))

Submission status: Runtime Error

Solution code:
import math

def comb(a,b):
  return(math.factorial(a)/math.factorial(a-b)/math.factorial(b))

n=int(input())
l=list(map(int,input().split()))
ll=[]
for i in range(n-1):
    ll.append(l[i+1]-l[i])

m=0
for i in range(n-1):
    k=0
    for j in range(i+1):
        k+=(comb(n-2-j,n-2-i))*(sum(ll[j:]))
    m+=math.factorial(n-1)*(k/((n-1-i)*comb(n-1,n-1-i)))
print(int(m%(10**9+7)))

Submission status: Runtime Error

Solution code:
import numpy as np


def cumprod(arr, MOD):
    L = len(arr)
    Lsq = int(L**.5 + 1)
    arr = np.resize(arr, Lsq**2).reshape(Lsq, Lsq)
    for n in range(1, Lsq):
        arr[:, n] *= arr[:, n - 1]
        arr[:, n] %= MOD
    for n in range(1, Lsq):
        arr[n] *= arr[n - 1, -1]
        arr[n] %= MOD
    return arr.ravel()[:L]


def make_fact(U, MOD):
    x = np.arange(U, dtype=np.int64)
    x[0] = 1
    fact = cumprod(x, MOD)
    x = np.arange(U, 0, -1, dtype=np.int64)
    x[0] = pow(int(fact[-1]), MOD - 2, MOD)
    fact_inv = cumprod(x, MOD)[::-1]
    return fact, fact_inv


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

mod = 10**9 + 7

v_dic = {}

for i in range(N - 1):
    d = x[-1] - x[i]
    v_dic[(x[i], x[-1])] = d


def f(arr):
    t = tuple(arr)
    if v_dic.get(t):
        return v_dic.get(t)
    s = 0
    for i in np.arange(arr.shape[0] - 1):
        _arr = arr[np.arange(arr.shape[0]) != i]
        v = arr[i + 1] - arr[i]
        s += v % mod
        s += f(_arr) % mod
    v_dic[t] = s % mod
    return s


s = f(x)
print(s % mod)

Submission status: Runtime Error

Solution code:
import numpy as np
import math
import sys
readline = sys.stdin.buffer.readline
MOD = 10**9+7

N = int(readline())
x = np.array(readline().split(), dtype=np.int64)
# print(N, x)

dif = x[1:] - x[:N-1]
# print(dif)

cnt = np.zeros(N-1, dtype=np.int64)
sm = math.factorial(N-1)
for i in range(N-1):
    if i == 0:
        cnt[i] = sm
        tmp = sm
    else:
        tmp = tmp * i // (i+1)
        cnt[i] = cnt[i-1] + tmp
# print(cnt)

sums = np.mod((dif * cnt), MOD)
# print(sms)
ans = 0
for i in range(N-1):
    ans += sums[i]
    ans %= MOD
print(ans)

Submission status: Runtime Error

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

mod = 10**9+7
ans = 0
count = 0
kai = 1
for i in range(n-1):
    kai *= (i+1)
    
for i in range(n-1):
    count += 1/(i+1)
    
    
    ans += sa[i]*count
    ans %= mod
    

print(int(kai*ans%mod))

Submission status: Runtime Error

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

N = 1e+9 + 7

def seq(m):
    tmp = 1
    l = []
    ans = []
    for i in range(m-1):
        tmp *= (i+1)%N
        l.append(tmp)
    for k in range(m-1):
        ans.append(int(tmp/(k+1))%N)
    return ans


ln = seq(n)
a = 0
b = 1

for i in range(n-1):
    b *= (i+1)%N
    a += ((ln[i]%N) * (x[-1]-x[i]))%N

b *= n
print(int(a%N))

Submission status: Runtime Error

Solution code:
import math

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

ans = 0

for i in range(n - 1):
    for j in range(i):
        k = i - j
        ans += (ls[i] - ls[j]) / (k * (k + 1)) 
        
for l in range(n - 1):
    k = n - 1 - l
    ans += (ls[n - 1] - ls[l]) / k

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

Submission status: Runtime Error

Solution code:
import random
n = int(input())
x = list(map(int,input().split()))

d = 0
count = n
res = 0
while count != 1:
    for i in range(0,n):
        cho = n - i + 1
        Z = []
        for j in range(1,cho+1):
            Z.append(j)
        k = random.choice(Z)
        for t in range(k+1,n):
            if x[t] != 0:
                x[t] += x[k]
                x[k] = 0
                d += x[t] - x[k]
        res += d/len(Z)
    count -= 1

b = 1
for h in range(1,n):
    b *= h

if b*res < 10**9+7:
    print(int((b*res)))
else:
    print(int((b*res)%10**9+7))

Submission status: Runtime Error

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

dx = [0]*(n-1)
for i in range(n-1):
    dx[i] = x[i+1] - x[i]

def fact(n):
    if n <= 1:
        return n
    else:
        return n*fact(n-1)
    
r = fact(n-1)//2
res = 0
for j in range(n-1):
    if j == 0:
        res += dx[j]*fact(n-1)
    else:
        res += dx[j]*fact(n-1) + r
        r = r + r//(j+1)
        
ans = res%(10**9+7)
print(ans)

Submission status: Runtime Error

Solution code:
import math
N = int(input())
x = list(map(int,input().split()))
m = 10**9 +7
k = 1/N
F = math.factorial(N-1)
All = list(map(lambda x: x/(N-1),[x for x in range(2, N+1)]))
res = sum(All)
# res = All*k
# F = math.factorial(N-1)
print((res*F)%m)

Submission status: Runtime Error

Solution code:
import math
N = int(input())
x = list(map(int,input().split()))
m = 10**9 +7
k = 1/N
F = math.factorial(N-1)
All = list(map(lambda x: x/(N-1),[x for x in range(2, N+1)]))
res = sum(All)
# res = All*k
# F = math.factorial(N-1)
print((res*F)%m,All)

Submission status: Runtime Error

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

# 一番移動量が大きくなるのはN-1番目->N-2番目(N-1番目)と選ばれるの
# 一番移動量が小さくなるのは1番目->2番目（一番目）と選ばれるの
# つまり右端の数字で移動量が決まる．例えば一番目のスライムの行き先は(N-1)通りあり(2~N)これらは等しい確率？
# ではなくない？ だってどう考えたって，右に2がある確率が高いし,N番目のがある確率は限りなく低くない？
# ２番めより先に１番目が呼ばれれば右に２があるのか．つまり1/2か？右に３があるのは2->1->3の順番かつまり1/4か?
# つまり1/2 + 1/4 + 1/8...1/2^(N-1?)か右にNがある確率は(1-(1/2+1/4+...1/2^(N-1?)))かな？
# N=2の時は別に分けないといけないのか？これ．もうよくわからん．めんどくさいのでこれで終わり
from collections import deque

X = deque(X)
kitai = 0
for i in range(N-1):  # N-1回の繰り返し
    if len(X)==2:
        kitai += (X[-1]-X[-2])
        break
    x = X.popleft()
    # xの行き先とそこに行ける確率で移動距離の期待値を計算する.
    left_num = len(X)
    left_dis = [X[k] - x for k in range(left_num)]
    kakuritsu = [1/(2**l) for l in range(1,left_num)]
    last_kakuritsu = 1 - sum(kakuritsu)
    kakuritsu.append(last_kakuritsu)
    x_kitai = [dis*kaku for (dis,kaku) in zip(left_dis,kakuritsu)]
    kitai += sum(x_kitai)
import math
print(int(kitai*math.factorial(N-1)) % (10**9 + 7))

Submission status: Runtime Error

Solution code:
import numpy as np
import math
import sys
readline = sys.stdin.buffer.readline
MOD = 10**9+7

N = int(readline())
x = np.array(readline().split(), dtype=np.int64)
# print(N, x)

dif = x[1:] - x[:N-1]
# print(dif)

cnt = np.zeros(N-1, dtype=np.int64)
sm = math.factorial(N-1)
for i in range(N-1):
    if i == 0:
        cnt[i] = sm
        tmp = sm
    else:
        tmp = tmp * i // (i+1)
        cnt[i] = cnt[i-1] + tmp
# print(cnt)

ans = np.mod((dif * cnt), MOD)
# print(ans)
ans = ans.sum() % MOD
print(ans)

Submission status: Runtime Error

Solution code:
import math
import numpy as np

n = int(input())
x = np.array(list(map(np.longlong, input().split())))

# 最低移動和?
move_min = x[len(x)-1] - x[0]
print("min:", x[len(x)-1] - x[0])
# 最大値計算?
move_max = np.sum(np.abs(x - x[-1]))
print(np.abs(x - x[-1]))
print("max:", np.sum(np.abs(x - x[-1])))
# 平均期待値?
hope = (move_max + move_min) / 2
print(np.longlong(hope * math.factorial(n - 1) % 1000000007))

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/i*f
    k.append(ss)
    




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


print(int(sum%MOD))

Submission status: Runtime Error

Solution code:
import math
import numpy as np

N = int(input())
# スペース区切りの整数の入力
x = np.array(input().rstrip().split(" ")).astype(np.uint64)

out = 0

N_kai = math.factorial(N - 1)
for i in range(N - 1):
    x_i = (x[i + 1 :] - x[i])[::-1]
    exp = 1 / (np.arange(2, N + 1 - i) * np.arange(1, N - i))
    exp[N - 2 - i] = 1 / (N - 1 - i)
    out = out + np.sum(exp * x_i)
    out = out % (10 ** 9 + 7)

print(int(out * N_kai % (10 ** 9 + 7)))

Submission status: Runtime Error

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

dx = [0]*(n-1)
for i in range(n-1):
    dx[i] = x[i+1] - x[i]

def fact(n):
    if n <= 1:
        return n
    else:
        return n*fact(n-1)
    
r = fact(n-1)//2
res = 0
for j in range(n-1):
    if j == 0:
        res += dx[j]*fact(n-1)
    else:
        res += dx[j]*fact(n-1) + r
        r = r + r/(j+1)
        
ans = int(res%(10**9+7))
print(ans)

Submission status: Runtime Error

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

N = 1e+9 + 7

def seq(m):
    tmp = 1
    l = []
    ans = []
    for i in range(m-1):
        tmp *= (i+1)
        l.append(tmp)
    for k in range(m-1):
        ans.append(int(tmp/(k+1)))
    return ans


ln = seq(n)
a = 0
b = 1

for i in range(n-1):
    b *= (i+1)
    a += ((ln[i]%N) * (x[-1]-x[i]))%N

b *= n
print(int(a%N))

Submission status: Runtime Error

Solution code:
N = int(input())
x = list(map(int, input().split()))
import itertools
import numpy as np
mod = 10**9+7
Y = 0
Z = 0
n = 1

x = np.array(x)
w = np.diff(x)
A=1
for j in range(1,N):
        A *= j

for i in w:
    Y += A/n % mod
    Z += Y * i % mod
    n += 1
print(int(Z))

Submission status: Runtime Error

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

import math


for i in range(2,N+1):#iは右端からの数
    #(x[-1]-x[-i])/i+(x[-2]-x[-i])/i......(x[-i+1]-x[-i])/i
    for j in range(i-1):
        ans+=(x[-j-1]-x[-i])/(i-1)
print(int((ans*math.factorial(N-1))%MOD))

Submission status: Runtime Error

Solution code:
import sys
from math import factorial
from fractions import Fraction
import heapq, bisect, fractions
import math
import itertools
sys.setrecursionlimit(10 ** 5 + 10)
INF = 10**15 +5
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
MOD = 10**9 + 7

n = INT()
x = LIST()
dist = [x[i+1]-x[i] for i in range(n-1)]
n -= 1

dp = [[0]*n for i in range(n)]
dp[1][0] = 1
dp[1][1] = 1
for i in range(2, n):
    dp[i][0] = dp[i-1][0] *i
    for j in range(i-1):
        dp[i][j+1] = dp[i-1][j+1] * (i+1)
    dp[i][i] = sum(dp[i-1][1:i]) + i*dp[i][0]

res = [0,] + dp[n-1][1:n]
ans = [res[i]+dp[n-1][0]*n for i in range(n)]
temp = sum([ans[i] * dist[i] for i in range(n)])%MOD

print(temp)

Submission status: Runtime Error

Solution code:
import sys
sys.setrecursionlimit(3000)
n = int(input())
x = list(map(int, input().split()))

dx = [0]*(n-1)
for i in range(n-1):
    dx[i] = x[i+1] - x[i]

def fact(n):
    if n <= 1:
        return n
    else:
        return n*fact(n-1)
    
r = fact(n-1)//2
res = 0
for j in range(n-1):
    if j == 0:
        res += dx[j]*fact(n-1)
    else:
        res += dx[j]*fact(n-1) + r
        r = r + r/(j+1)
        
ans = res%(10**9+7)
print(ans)

Submission status: Runtime Error

Solution code:
import sys

sys.setrecursionlimit(100000000)

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

a = 10**9+7

def solve(lst):
    print(lst)
    l = len(lst)
    if l == 2:
        return lst[1] - lst[0]
    else:
        out = 0
        for i in range(l-1):
            d = lst[i+1] - lst[i]
            out += d + solve(lst[0:i]+lst[i+1:l])
        return out % a

print(solve(x) % a)

Submission status: Runtime Error

Solution code:
dist = 0

def fact(n):
    f = 1
    for i in range(2, n + 1):
        f = f * i
    return f

def distsum(n, *x):
    global dist
    y = list(x)
    for i in range(n - 1):
        dist = dist + (y[i + 1] - y[i])
    for i in range(n - 1):
        y = list(x)
        y.remove(y[i])
        if len(y) > 1:
            distsum(n - 1, *y)

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

distsum(N, *x)

a = dist
b = 1e9 + 7
if a < b:
    print(a)
else:
    print(a % b)

Submission status: Runtime Error

Solution code:
import math
mod=10**9+7

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

mi=X[-1]-X[0]

ma=0
for i in range(n-1):
    ma+=X[-1]-X[i]

print(int((((mi+ma)/2)*math.factorial(n-1))%mod))

Submission status: Runtime Error

Solution code:

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

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

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

Submission status: Runtime Error

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

dx = [0]*(n-1)
for i in range(n-1):
    dx[i] = x[i+1] - x[i]

def fact(n):
    if n <= 1:
        return n
    else:
        return n*fact(n-1)
    
r = fact(n-1)//2
res = 0
for j in range(n-1):
    if j == 0:
        res += dx[j]*fact(n-1)
    else:
        res += dx[j]*fact(n-1) + r
        r = r + r/(j+1)
        
ans = res%(10**9+7)
print(ans)

Submission status: Runtime Error

Solution code:
import math

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

dist = []
tmp = x[0]
for x in x[1:]:
    dist.append(x - tmp)
    tmp = x
    
ans = 0

for i in range(1, N):
    if i == 1:
        tmp = sum(dist) / (N-i)
    else:
        tmp = tmp + sum(dist[i-1:])/(N-1)
    ans += tmp
    ans = ans % (1e9 + 7)

ans = int(ans * math.factorial(N-1)) % (1e9 + 7)
print(int(ans))

Submission status: Runtime Error

Solution code:
def A():
    N = int(input())
    index = {}
    durations = []
    for i in range(N):
        temp = input().split()
        index[temp[0]] = i
        durations.append(int(temp[1]))
    X = input()
    print(sum(durations[index[X]+1:]))

import math
def B():
    N = int(input())
    X = [int(n) for n in input().split()]
    #n_minus_one_factorial = math.factorial(N-1)
    diff = [b-a for a, b in zip(X[:-1], X[1:])]
    def process(differences):
        #print(differences)
        if len(differences) == 1:
            return differences[0]%(10**9+7)
        ans = 0
        denom = len(differences)
        fact = math.factorial(denom-1)
        for i in range(denom):
            temp = differences[:i]+differences[i+1:]
            if i > 0:
                temp[i-1] += differences[i]
            ans += (differences[i]*fact + process(temp))
        #print(ans, differences)
        return ans%(10**9+7)
    print(process(diff))

B()

Submission status: Runtime Error

Solution code:
import math

dev = 10**9+7

def kai(n):
    if n == 1:
        return 1
    else:
        return (n * kai(n-1)) % dev

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

s = 0
now = 0
f = kai(N-1) % dev
#f = math.factorial(N-1)
for i in range(N-1):
  now += (f/(i+1)) % dev
  s += (((X[i+1]-X[i])%dev)*now) % dev

print(int(s%dev))

Submission status: Runtime Error

Solution code:
N=int(input())
X= [int(i) for i in input().split()]

def update(l):
    n = len(l)
    k = []
    k.append((l[0]*(n+1)) % lim)
    for i in range(1,n):
        k.append((l[0] + l[i-1]*(i+1) + l[i]*(n-i)) % lim)
    k.append((l[0] + l[n-1]*(n+1))% lim)
    return k

def generate(n):
    if n == 1:
        return [1]
    else:
        l = [1]
        for _ in range(n-1):
            l = update(l)
        return l

diff = [(X[i+1] -X[i]) % lim for i in range(N-1)]

M=generate(N-1)

ans = 0
for i in range(N-1):
    ans = (ans + (diff[i]*M[i]) % lim) % lim
print(ans)

Submission status: Runtime Error

Solution code:
n=int(input())
x=list(map(int,input().split()))
a=x[n-1]-x[0]
def fuc(n):
    a=1
    for i in range(1,n+1):
        a*=i
    return a

def comb(a,b):
    x=fuc(a)//(fuc(b)*fuc(a-b))
    return x

p=1
ans=0
for i in range(1,n):
    cnt=0
    p/=n-i
    ans+=p*(x[n-1]-x[0])*(comb(n-1,i-1))
    for j in range(i-1):
        k=i-j-1 #左から何個消えてるか
        ans+=p*(x[0]-x[k])*comb(n-1-k,i-k-1)
        cnt+=comb(n-1-k,i-k-1)


print(int(ans*fuc(n-1))%(10**9+7))

Submission status: Runtime Error

Solution code:
import math

N = int(input())
X = list(map(int, input().split()))
x_max = X[N-1]

d_max = 0
for x in reversed(X):
	d_max += (x_max - x)
	
d_min = 0
prev = X[0]
for x in X:
	d_min += (x - prev)
	prev = x

ev = (d_max+d_min)/2

#print(X)
#print(d_min, d_max)
#print(math.factorial(N-1), ev)
print(int(math.factorial(N-1)*ev % (10**9+7)))

Submission status: Runtime Error

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

min_ = sum(sp)
max_ = 0
t = 0
for l in sp[::-1]:
    t += l
    max_ += t
exp = (min_+max_)/2
ans = (exp*math.factorial(n-1)) % mod
print(ans)

Submission status: Runtime Error

Solution code:
N=int(input())
X=list(map(int,input().split()))
a=1
b=1
c=10**9+7
for i in range(N)[1:]:
    a=a*i
for i in range(N-1):
    b*=i+1
l1=list()
l2=list()
ans=0
for i in range(N):
    A=a
    for j in range(N-i-2):
        l1.append(a/((j+2)*(j+1)))
        A=A-a/((j+2)*(j+1))
    l1.append(A)
l1.pop(-1)
for i in range(N):
    for j in range(N)[i+1:]:
        l2.append(X[j]-X[i])
for n1,n2 in zip(l1,l2):
    ans+=n1*n2
print(int(ans%c))

Submission status: Runtime Error

Solution code:
import numpy as np
N = int(input())
x = list(map(int, input().split()))

low = 2**(N-2)
dif = 2**(N-3)
num = N-1
high = low+dif*(num)
#print(low, high, dif)
l = range(low, high ,dif)
#print(l)
li =np.array([i for i in l])
#print(li)

d = np.array([x[i+1]-x[i] for i in range(N-1)])
#print(d)
SUM = li@d
print(SUM)

Submission status: Runtime Error

Solution code:
import copy
import numpy as np
import fractions
import itertools

n = int(input())

l = input().split()

for i in range(n):
    l[i] = int(l[i])

new_l = [l[n-1]-i for i in l]
# print(new_l)

new_l.pop()
new_l.reverse()

length = len(new_l)
allSum = 0

for i in range(length):
    ruiseki = np.cumsum(new_l)
    allSum += sum(ruiseki)
    new_l.pop()

ruiseki = np.cumsum(new_l)
allSum += sum(ruiseki)
# print(ruiseki)
print(allSum)
# ans = allSum/fractions.factorial(n)
factorial = 1
# print(len(list(itertools.permutations(new_l))))
for i in range(1, n):
    factorial *= i
print(factorial)

ans = int(allSum/(n-1) * factorial)
print(ans)
print(ans % 1000000007)

Submission status: Runtime Error

Solution code:
import itertools
import numpy as np

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

x_max = x.pop(-1)
x_left = x
length = len(x_left)

x_left_list = list(itertools.permutations(x_left, length))
x_left_list = [list(v) for v in x_left_list]

# numpy 配列に。
x_left_array = np.array(x_left_list).reshape(-1, length)
x_max_array = np.array([x_max for i in range(length)]).reshape(-1, 1)
x_all = np.hstack((x_max_array, x_left_array))

x_diff = np.array(np.abs(x_all[:, :-1] - x_all[:, 1:])).reshape(-1)

X = np.sum(x_diff)

print(X)

Submission status: Runtime Error

Solution code:
import math
n = int(input())
*x, = map(int, input().split())

inf = 10**9+7

d = [0]*(n-1)
for i in range(n-1):
    d[i] = abs(x[i+1]-x[i])

p = 1/(n-1)
ev = 0
sd = sum(d)
for i in range(len(d)):
    m = sd + sum(d[i+1:])
    ev += p*m%inf


print(int(ev*math.factorial(n-1)%inf))

Submission status: Runtime Error

Solution code:
import math

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

mod = 10**9 + 7

f = math.factorial(n-1)

l = []
for i in range(n-1):
    d = x[i+1] - x[i]
    l.append(d)

# print(l)

count = 0
k = f
plus = 0
for i in range(1,n):
    # print(k)
    k = f + plus
    count += l[i-1] * k
    plus += f / (i+1)

print(int(count)%mod)

Submission status: Runtime Error

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

low = 2**(N-2)
dif = 2**(N-3)
num = N-1
high = low+dif*(num)
#print(low, high, dif)
l = range(low, high ,dif)
#print(l)
li =np.array([i for i in l])
#print(li)

d = np.array([x[i+1]-x[i] for i in range(N-1)])
#print(d)
SUM = li@d
print(SUM)

Submission status: Runtime Error

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

a=math.factorial(N-1)
b=0
ans=0
for i in range(N-1):
  b+=a/(i+1)
  ans+=b*(X[i+1]-X[i])
  ans%=mod
  
print(int(ans))

Submission status: Runtime Error

Solution code:
import sys
from copy import copy
lines = sys.stdin.readlines()
ds = tuple([int(i) for i in lines[1].split(' ')])
#print(ds)

cache = dict()
def move(ds):
    if len(ds) == 1:
        return 0

    d = 0
    for i in range(len(ds) -1 ):
        nds = list(ds)
        d += nds[i+1] - nds[i]
        nds.pop(i)
        nds = tuple(nds)
        if nds in cache:
            d += cache[nds]
        else:
            t = move(nds)
            cache[nds] = t
            d += t
    #print(d, ds)
    return d / (len(ds) - 1)

r = move(ds)
#print(r)
for i in range( len(ds)-1):
    #print(i+1)
    r *= (i + 1)
r = r % (pow(10,9)+7)
print(int(r))

Submission status: Runtime Error

Solution code:
import math

denom = int(1e9+7)


def calc_coeff(n):
    if n == 1:
        return [1]
    elif n == 2:
        return[2,3]
    else:
        s = calc_coeff(n-1)
        ans = []
        #when first one is selected first
        ans.append(math.factorial(n) % denom)
        #otherwise
        for k in range(1,n-1):
            t = k*s[k-1] + (n-k-1)*s[k] + math.factorial(n-1) + s[k-1]
            ans.append(t % denom)
        t = (n-1)*s[n-2] + math.factorial(n-1) + s[n-2]
        ans.append(t % denom)
        return ans
    
    

N = int(input())
pos = [int(i) for i in input().split()]
x = []

coeff = calc_coeff(N-1)

tot = 0
for i in range(N-1):
    x.append(pos[i+1]-pos[i])
    tot += (coeff[i] * (pos[i+1]-pos[i])) % denom

print(tot % denom)

Submission status: Runtime Error

Solution code:
import math

dev = 10**9+7

def kai(n):
    if n == 1:
        return 1
    else:
        return (n * kai(n-1)) % dev

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

s = 0
now = 0
for i in range(N-1):
  now += kai(N-1)/(i+1)
#  now += math.factorial(N-1)/(i+1)%dev
  s += (((X[i+1]-X[i])%dev)*now) % dev

print(int(s%dev))

Submission status: Runtime Error

Solution code:
import math

def comb(a,b):
	if b==0:
		return 1
	else:
		return(math.factorial(a)/math.factorial(a-b)/math.factorial(b))

n=int(input())
l=list(map(int,input().split()))
ll=[]
for i in range(n-1):
    ll.append(l[i+1]-l[i])

m=0
for i in range(n-1):
    k=0
    for j in range(i+1):
        k+=(comb(n-2-j,n-2-i))*(sum(ll[j:]))
    m+=math.factorial(n-1)*(k/((n-1-i)*comb(n-1,n-1-i)))
print(int(m%(10**9+7)))

Submission status: Runtime Error

Solution code:
import math
n = int(input())
*x, = map(int, input().split())

d = [0]*(n-1)
for i in range(n-1):
    d[i] = abs(x[i+1]-x[i])

p = 1/(n-1)
ev = 0
sd = sum(d)
for i in range(len(d)):
    m = sd + sum(d[i+1:])
    ev += p*m

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

Submission status: Runtime Error

Solution code:
n=int(input())
x_list = list(map(int,input().split()))

import copy
ans = 0
list_place_init = copy.copy(x_list)

def fusing_slimes(n, list_place):
    global ans
    global list_place_init
    if n == 3:
        p1 = (list_place[1] - list_place[0]) + (list_place[2] - list_place[1])
        p2 = (list_place[2] - list_place[1]) + (list_place[2] - list_place[0])
        ans += p1 + p2
    else:
        for i in range(n-1):
            ans += (list_place[i+1] - list_place[i])
            list_place.pop(i)
            fusing_slimes(n-1, list_place)
            list_place = copy.copy(list_place_init)

fusing_slimes(n, x_list)

print(ans % 1000000007)