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:
from heapq import heappush,heappop,heapify
from collections import deque,defaultdict,Counter
import itertools
from itertools import permutations,combinations,groupby
import sys
import bisect
import string
import math
import time
import random
def S_():
    return input()
def LS():
    return [i for i in input().split()]
def I():
    return int(input())
def MI():
    return map(int,input().split())
def LI():
    return [int(i) for i in input().split()]
def LI_():
    return [int(i)-1 for i in input().split()]
def NI(n):
    return [int(input()) for i in range(n)]
def NI_(n):
    return [int(input())-1 for i in range(n)]
def StoI():
    return [ord(i)-97 for i in input()]
def ItoS(nn):
    return chr(nn+97)
def LtoS(ls):
    return ''.join([chr(i+97) for i in ls])
def GI(V,E,Directed=False,index=0):
    org_inp=[]
    g=[[] for i in range(n)]
    for i in range(E):
        inp=LI()
        org_inp.append(inp)
        if index==0:
            inp[0]-=1
            inp[1]-=1
        if len(inp)==2:
            a,b=inp
            g[a].append(b)
            if not Directed:
                g[b].append(a)
        elif len(inp)==3:
            a,b,c=inp
            aa=(inp[0],inp[2])
            bb=(inp[1],inp[2])
            g[a].append(bb)
            if not Directed:
                g[b].append(aa)
    return g,org_inp
def bit_combination(k,n=2):
    rt=[]
    for tb in range(n**k):
        s=[tb//(n**bt)%n for bt in range(k)]
        rt+=[s]
    return rt
def show(*inp,end='\n'):
    if show_flg:
        print(*inp,end=end)
YN=['Yes','No']
mo=10**9+7
inf=float('inf')
l_alp=string.ascii_lowercase
u_alp=string.ascii_uppercase
ts=time.time()
#sys.setrecursionlimit(10**5)
input=lambda: sys.stdin.readline().rstrip()

def ran_input():
    import random
    n=random.randint(4,16)
    rmin,rmax=1,10
    a=[random.randint(rmin,rmax) for _ in range(n)]
    return n,a

class Comb:
    def __init__(self,n,mo=10**9+7):
        self.fac=[0]*(n+1)
        self.inv=[1]*(n+1)
        self.fac[0]=1
        self.fact(n)
        for i in range(1,n+1):
            self.fac[i]=i*self.fac[i-1]%mo
            self.inv[n]*=i
            self.inv[n]%=mo
        self.inv[n]=pow(self.inv[n],mo-2,mo)
        for i in range(1,n):
            self.inv[n-i]=self.inv[n-i+1]*(n-i+1)%mo
        return
    
    def fact(self,n):
        return self.fac[n]
        
    def invf(self,n):
        return self.inv[n]

    def comb(self,x,y):
        if y<0 or y>x:
            return 0
        return self.fac[x]*self.inv[x-y]*self.inv[y]%mo

show_flg=False
show_flg=True

ans=0


n=I()
x=LI()
p=permutations(range(n-1))
cm=Comb(n+10)


StrN=[0]*(n+10)
StrN[1]=1
def St(n):
    if n==1:
        return 1
    if StrN[n]!=0:
        return StrN[n]
    else:
        #a(n+1)=(n+1)*a(n)+n!.
        StrN[n]=(n)*St(n-1)+cm.fac[n-1]
        return StrN[n]

St(n+9)

Pat=[0]*(n+1)
for i in range(n):
    Pat[i]=StrN[i]*cm.fac[n-1]*cm.inv[i]%mo
#show('Pat',Pat)
#show('St',StrN)

for i in range(n-1):
    ans+=(x[i+1]-x[i])*Pat[i+1]
    ans%=mo

print(ans%mo)
#show(SSS)

Submission status: Runtime Error

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

def modinv2(a,m):
    return pow(a,m-2,m)

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

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

ans = 0
P = 10**9+7

b = f(N-1,P)
#print(b)

for i in range(N-1):
    l = 0
    c = modinv2(N-1-i,P)
    #print(c)
    for j in range(i+1,N):
        l += x[j]-x[i]
        l %= P
    #print(l)
    l = l*b%P
    l = l*c%P
    ans += l

print(ans%P)

Submission status: Runtime Error

Solution code:
from heapq import heappush,heappop,heapify
from collections import deque,defaultdict,Counter
import itertools
from itertools import permutations,combinations,groupby
import sys
import bisect
import string
import math
import time
import random
def S_():
    return input()
def LS():
    return [i for i in input().split()]
def I():
    return int(input())
def MI():
    return map(int,input().split())
def LI():
    return [int(i) for i in input().split()]
def LI_():
    return [int(i)-1 for i in input().split()]
def NI(n):
    return [int(input()) for i in range(n)]
def NI_(n):
    return [int(input())-1 for i in range(n)]
def StoI():
    return [ord(i)-97 for i in input()]
def ItoS(nn):
    return chr(nn+97)
def LtoS(ls):
    return ''.join([chr(i+97) for i in ls])
def GI(V,E,Directed=False,index=0):
    org_inp=[]
    g=[[] for i in range(n)]
    for i in range(E):
        inp=LI()
        org_inp.append(inp)
        if index==0:
            inp[0]-=1
            inp[1]-=1
        if len(inp)==2:
            a,b=inp
            g[a].append(b)
            if not Directed:
                g[b].append(a)
        elif len(inp)==3:
            a,b,c=inp
            aa=(inp[0],inp[2])
            bb=(inp[1],inp[2])
            g[a].append(bb)
            if not Directed:
                g[b].append(aa)
    return g,org_inp
def bit_combination(k,n=2):
    rt=[]
    for tb in range(n**k):
        s=[tb//(n**bt)%n for bt in range(k)]
        rt+=[s]
    return rt
def show(*inp,end='\n'):
    if show_flg:
        print(*inp,end=end)
YN=['Yes','No']
mo=10**9+7
inf=float('inf')
l_alp=string.ascii_lowercase
u_alp=string.ascii_uppercase
ts=time.time()
#sys.setrecursionlimit(10**5)
input=lambda: sys.stdin.readline().rstrip()

def ran_input():
    import random
    n=random.randint(4,16)
    rmin,rmax=1,10
    a=[random.randint(rmin,rmax) for _ in range(n)]
    return n,a

class Comb:
    def __init__(self,n,mo=10**9+7):
        self.fac=[0]*(n+1)
        self.inv=[1]*(n+1)
        self.fac[0]=1
        self.fact(n)
        for i in range(1,n+1):
            self.fac[i]=i*self.fac[i-1]%mo
            self.inv[n]*=i
            self.inv[n]%=mo
        self.inv[n]=pow(self.inv[n],mo-2,mo)
        for i in range(1,n):
            self.inv[n-i]=self.inv[n-i+1]*(n-i+1)%mo
        return
    
    def fact(self,n):
        return self.fac[n]
        
    def invf(self,n):
        return self.inv[n]

    def comb(self,x,y):
        if y<0 or y>x:
            return 0
        return self.fac[x]*self.inv[x-y]*self.inv[y]%mo

show_flg=False
show_flg=True

ans=0


n=I()
x=LI()
p=permutations(range(n-1))
cm=Comb(n+2)


StrN=[0]*(n+1)
StrN[1]=1
def St(n):
    if n==1:
        return 1
    if StrN[n]!=0:
        return StrN[n]
    else:
        #a(n+1)=(n+1)*a(n)+n!.
        StrN[n]=(n)*St(n-1)+cm.fac[n-1]
        return StrN[n]

St(n)

Pat=[0]*(n+1)
for i in range(n):
    Pat[i]=StrN[i]*cm.fac[n-1]*cm.inv[i]%mo
#show('Pat',Pat)
#show('St',StrN)

for i in range(n-1):
    ans+=(x[i+1]-x[i])*Pat[i+1]
    ans%=mo

print(ans%mo)
#show(SSS)

Submission status: Runtime Error

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

def modinv2(a,m):
    return pow(a,m-2,m)

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

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

ans = 0
P = 10**9+7

b = f(N-1,P)
#print(b)

for i in range(N-1):
    l = 0
    c = modinv2(N-1-i,P)
    #print(c)
    for j in range(i+1,N):
        l += x[j]-x[i]
        l %= P
    #print(l)
    l = l*b%P
    l = l*c%P
    ans += l

print(ans%P)

Submission status: Runtime Error

Solution code:
import math
N=int(input())
X=list(map(int,input().split()))
kyori = []
kouho = 0
for i in range(N-1):
    kyori.append(abs(X[i+1]-X[i]))
for j in range(len(kyori)):
    if j == 0:
        kouho += sum(kyori)
    
    else:
        kouho += sum(kyori[j:])+sum(kyori[j-1:])+sum(kyori[:j-1])
        #print(sum(kyori)-sum(kyori[:j])+sum(kyori[j-1:]))
kouho /= 2
kaijo = 1
for k in range(N-1):
    kaijo *= N-1-k
print(math.ceil(kouho*kaijo)%(10**9+7))

Submission status: Runtime Error

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

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()

ruiseki = np.cumsum(new_l)
# print(ruiseki)
allSum = sum(ruiseki)
# ans = allSum/fractions.factorial(n)
# factorial = len(list(itertools.permutations(new_l)))
# print(factorial)

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

Submission status: Runtime Error

Solution code:
# -*- coding: utf-8 -*-

import sys
from math import factorial

def input(): return sys.stdin.readline().strip()
def list2d(a, b, c): return [[c] * b for i in range(a)]
def list3d(a, b, c, d): return [[[d] * c for j in range(b)] for i in range(a)]
def list4d(a, b, c, d, e): return [[[[e] * d for j in range(c)] for j in range(b)] for i in range(a)]
def ceil(x, y=1): return int(-(-x // y))
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(N=None): return list(MAP()) if N is None else [INT() for i in range(N)]
def Yes(): print('Yes')
def No(): print('No')
def YES(): print('YES')
def NO(): print('NO')
sys.setrecursionlimit(10 ** 9)
INF = 10 ** 18
MOD = 10 ** 9 + 7

N = INT()
A = LIST()
B = [0] + [A[i+1] - A[i] for i in range(N-1)]

add = factorial(N-1)
cnt = [0] * N
for i in range(1, N):
    cnt[i] = round(cnt[i-1] + add)
    add *= i / (i+1)

ans = 0
for i in range(1, N):
    ans += B[i] * cnt[i]
    ans %= MOD
print(ans)

Submission status: Runtime Error

Solution code:
import math

n = int(input())
li = list(map(int, input().split()))
last = li[-1]
wa = 0
x = 10**9+7
for i in range(n-1):
  temp = i
  if i == 0:
    wa += abs(li[i] - last)
  else:
    while temp >= 0:
      wa += abs(li[temp] - last)
      temp -= 1
      
print(int(((wa / (n-1)) % x) * math.factorial(n-1)))

Submission status: Runtime Error

Solution code:
import math

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

xrangeList = []
for i in range(0, N - 1):
    xrangeList.append(xList[i + 1] - xList[i])

tmp = math.factorial(N - 1)

resultin = 0
for i in range(0, N - 1):
    if i != 0 and i != N - 2:
        Table = tmp * (i / (N - 2) + 1)
        resultin += xrangeList[i] * Table
    elif i == 0:
        Table = tmp
        resultin += xrangeList[i] * Table
    else:
        Table = tmp * 2 - 1
        resultin += xrangeList[i] * Table
print(int(resultin % (10 ** 9 + 7)))

Submission status: Runtime Error

Solution code:
n=int(input())
X=[int(i) for i in input().split()]
mod=10**9+7
n_max=2*10**5
F=[0]*(n_max+1)
F[0]=1
for i in range(n_max):
  F[i+1]=(F[i]*(i+1))%mod

ans=0
DP=[[0]*n for _ in range(n)]
for i in range(1,n):
  for j in range(1,i+1):
    if j==1:
      DP[i][j]=F[i-1]
    elif j==i:
      DP[i][j]=1
    else:
      DP[i][j]=DP[i-1][j-1]+(i-1)*DP[i-1][j]
  for j in range(1,i+1):
    ans=(ans+j*DP[i][j]*(X[i]-X[i-1])*F[n-i])%mod
print(ans)

Submission status: Runtime Error

Solution code:
import math
import numpy as np
distance = 0
Esperance = 0
N = int(input())
L = [int(x) for x in input().split()]
for i in range(N):
    Dice = list(range(1,N-i))
    if Dice != []:
        k = np.random.choice(Dice) #ランダムにkを選ぶ。
        Pourcentage = 1/len(Dice) #kが選ばれる確率。
        distance += 1 #左からk番目にいるスライムを右隣にいるスライムの位置まで移動させる
        Esperance += distance*Pourcentage #確率*移動距離
        L.remove(L[k]) #k番目のスライムを消去
print((int(Esperance*math.factorial(N-1))) % (10**9 + 7))

Submission status: Runtime Error

Solution code:
def mod_inverse(n, mod=10**9+7):
    return pow(n, mod-2, mod)


def combination(n, k, mod=10**9+7):
    numer = denom = 1
    for i in range(k):
        numer = (numer * (n-i)) % mod
        denom = (denom * (i+1)) % mod
    return (numer * mod_inverse(denom, mod)) % mod


def factorial(n):
    if n == 0:
        return 1
    return n * factorial(n-1) % mod


mod = 10**9+7
N = int(input())
x = [0] + list(map(int, input().split()))
fact = factorial(N-1)

ans = 0
for k in range(2, N):
    ans = (ans + x[k] * (1 - mod_inverse(k)))

for k in range(1, N-1):
    ans = (ans - x[k] * (1 - mod_inverse(N-k))) % mod

ans = ans * fact % mod

ans = (ans + fact * sum((x[N] - x[k]) * mod_inverse(N-k) for k in range(1, N))) % mod

print(ans)

Submission status: Runtime Error

Solution code:
N = int(input())
x = list(reversed(list(map(int,input().split()))))
mod = 10**9+7
x = [x[0]-i for i in x]

def rep(x,mod):
    count = 0
    if len(x) == 2:
        return x[1]
    for i in range(1,len(x)):
        count += ((x[i]-x[i-1]) + rep([x[j] for j in range(len(x)) if j != i],mod))%mod
    return count
print(rep(x,mod))

Submission status: Runtime Error

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

mod = 10**9 + 7

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

# print(l)

import math
f = math.factorial(n-1)

count = 0

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

print(int(count)%mod)

Submission status: Runtime Error

Solution code:
import numpy as np
from math import factorial
MOD = 10**9 + 7

n = int(input())
dist = np.diff(list(map(int, input().split())))

L = np.cumsum(np.array([1 / x for x in range(1, n)]))
n_1 = factorial(n-1)
# L *= n_1

# print(int((np.sum(L * dist) * n_1) % MOD))

ans = 0
for l, d in zip(L, dist):
    ans += l * d % MOD

ans = (ans * n_1 % MOD)
print(int(ans))

Submission status: Runtime Error

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

def extgcd(a,b):
    r = [1,0,a]
    w = [0,1,b]
    while w[2] != 1:
        q = r[2]//w[2]
        r2 = w
        w2 = [r[0]-q*w[0],r[1]-q*w[1],r[2]-q*w[2]]
        r = r2
        w = w2
    #[x,y]
    return [w[0],w[1]]

def modinv(a,m):
    x = extgcd(a,m)[0]
    return (m+x%m)%m #負の値を返さないように

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

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

ans = 0
P = 10**9+7

b = f(N-1,P)
#print(b)

for i in range(N-1):
    l = 0
    c = modinv(N-1-i,P)
    for j in range(i,N):
        l += x[j]-x[i]
        l %= P
    #print(l)
    l = l*b%P
    l = l*c%P
    ans += l

print(ans%P)

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
n = int(input())
x = list(map(int, input().split()))
def fact(a):
    if a == 1:
        return 1
    else: return a * fact(a-1)
def xx(a):
    if a == 1:
        return [1]
    else: 
        b = xx(a-1)
        for i in range(a-1):
            b[i] *= a
        b.append(b[a-2] + fact(a-1))
        return b
y = xx(n-1)
ans = 0
for i in range(n-1):
    ans += y[i] * (x[i+1]-x[i])
print(ans%1000000007)

Submission status: Runtime Error

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

def mod_factional(n):
    if n <= 1:
        return 1
    return (n * mod_factional(n - 1)) % (10 ** 9 + 7)

ans = 0
p = mod_factional(N - 1)
m = p
for i, (x, y) in enumerate(zip(xs, xs[1:])):
    tmp = y - x
    ans += tmp * m
    ans %= 10 ** 9 + 7
    p = p // (i + 2) * (i + 1)
    m += p

print(ans)

Submission status: Runtime Error

Solution code:
import math

def kaijo(n):
    return math.factorial(n)

INF = 10 ** 9 + 7
N = int(input())
x = list(map(int, input().split()))
k = kaijo(N - 1)
tmp = 0
for i in range(N - 2):
    for j in range(i + 1, N - 1):
        tmp += ((x[j] - x[i]) / (N - 1 - j + 1)) % INF
    tmp += ((x[-1] - x[i]) / 2) % INF
tmp += ((x[-1] - x[-2])) % INF
ans = int(tmp * k)
ans %= INF
print(ans)

Submission status: Runtime Error

Solution code:
mod = 10**9+7
n = int(input())
x = [int(x) for x in input().split()]
x = x[::-1]

dp = [0 for _ in range(n)]
dp[1] = x[0]-x[1]

xx = [0]
for i in range(n):
    xx.append((xx[-1]+x[i]) % mod)

fact = [1]
for i in range(n+1):
    fact.append((fact[-1]*(i+1)) % mod)

for i in range(2, n):
    dp[i] += i*dp[i-1] + (x[0]-x[i])*fact[i-1]
    dp[i] %= mod
    for k in range(1, i):
        dp[i] += (x[k]-x[i]) * fact[i] // (fact[i-k+1]//fact[i-k-1])
        dp[i] %= mod

print(dp[n-1])

Submission status: Runtime Error

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


def main():
    sc = Scan()
    N = sc.intarr()[0]
    X = sc.intarr()
    Y = X[:-1]
    last = X[-1]
    tmp = perm(Y)

    pp = []
    for t in tmp:
        a = t
        a.append(last)
        pp.append(a)

    score = 0
    for p in pp:
        for i in range(len(p)-1):
            key = p[i]
            array = p[i+1:]
            near = array[-1]-key
            dist = array[-1]-key
            for j in range(len(array)-1):
                dist = array[j] - key
                if dist > 0 and dist < near:
                    near = dist
            score += near

    ans = (score * math.factorial(N-1) / len(pp)) % 1000000007

    print(int(ans))


def perm(p):
    if len(p) == 1:
        return [p]
    else:
        s = []
        for i in range(len(p)):
            q = p[i]
            r = perm(p[:i]+p[i+1:])
            for t in r:
                s.append([q] + t)
        return s


class Scan():

    def intarr(self):
        num_array = list(map(int, input().split()))
        return num_array

    def intarr_ver(self, n):
        return [int(input()) for _ in range(n)]

    def strarr(self):
        line = input()
        array = line.split(' ')
        array[-1] = array[-1].strip('\n')
        return array


def display(array):
    for a in range(len(array)):
        if len(array) - a != 1:
            print(array[a], end=' ')
        else:
            print(array[a])


def gcd(a, b):  # 最大公約数
    while b:
        a, b = b, a % b
    return a


def lcm(a, b):  # 最小公倍数
    return a * b // gcd(a, b)


main()

Submission status: Runtime Error

Solution code:
mod = 10**9+7
n = int(input())
x = [int(x) for x in input().split()]
x = x[::-1]

dp = [0 for _ in range(n)]
dp[0] = 0

dp[1] = x[0]-x[1]

xx = [0]
for i in range(n):
    xx.append((xx[-1]+x[i]) % mod)

fact = [1]
for i in range(n+1):
    fact.append((fact[-1]*(i+1)) % mod)

for i in range(2, n):
    dp[i] += i*dp[i-1] + (x[0]-x[i])*fact[i-1]
    for k in range(1, i):
        dp[i] += (x[k]-x[i]) * fact[i] // (fact[i-k+1]//fact[i-k-1])
    dp[i] %= mod

print(dp[n-1])

Submission status: Runtime Error

Solution code:
import math

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

    ret = 0
    for i in range(1, n):
        ret += sum(location[n-i:]) / i - location[n-i-1]

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


if __name__ == '__main__':
    main()

Submission status: Runtime Error

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

for i in range(N-1):
  kitai +=(x[i+1]*(1/(N-1)))
#  print(x[i+1])
y = math.factorial(N-1)
a = kitai*y
z = 10**9+7
#print(x)
print(int(a%z))
#print(a)
#print(y)
#print(z)
#print(kitai)
#print(kaijo)

Submission status: Runtime Error

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

for i in range(N-1):
  kitai +=(x[i+1]*(1/(N-1)))
  print(x[i+1])
y = math.factorial(N-1)
a = kitai*y
z = 10**9+7
#print(x)
print(int(a%z))
#print(a)
#print(y)
#print(z)
#print(kitai)
#print(kaijo)

Submission status: Runtime Error

Solution code:
import math
N = int(input())
xn = list(map(int, input().split()))

if N > 2000:
    print(0)
    exit()
else:
    ans = 0
    for i in range(N-1):
        for j in range(N-1-i):
            ans += (xn[i+1+j]-xn[i])/(N-1-j)
        for l in range(i):
            ans += (xn[i+1]-xn[i-j])/(N-1-l)
    print(int(ans*math.factorial(N-1)))

Submission status: Runtime Error

Solution code:
import numpy as np
from math import factorial
MOD = 10**9 + 7

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

L = np.array([1 / x for x in range(1, n)])

L = np.cumsum(L)
n_1 = factorial(n-1)
L *= n_1

print(int(np.sum(L * dist) % MOD))

Submission status: Runtime Error

Solution code:
N = int(input())
a = list( map(int, input().split() ) )
result = float()
for i in range(N-1):
    result += (a[N-1] - a[i]) / ( i+1 )

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

if result.is_integer():
    result = int(result)
else:
    result = int(result)+1
    
print( result % (10**9+7) )

Submission status: Runtime Error

Solution code:
import math
N = int(input())
xn = list(map(int, input().split()))

if N > 2000:
    print(0)
    exit()
else:
    ans = 0
    for i in range(N-1):
        for j in range(N-1-i):
            ans += (xn[i+1+j]-xn[i])/(N-1-j)
        for l in range(i):
            ans += (xn[i+1]-xn[i-j]/(N-1-l))
    print(int(ans*math.factorial(N-1)))

Submission status: Runtime Error

Solution code:
import sys
import math
#import numpy as np

stdin = sys.stdin

ri = lambda: int(rs())
rl = lambda: list(map(int, stdin.readline().split())) # applies to numbers only
rs = lambda: stdin.readline().rstrip()  # ignore trailing spaces

N = ri()
X = rl()
MOD = 10 ** 9 + 7
limit = math.factorial(N-1)
last = X[-1]
X = [last - x for x in X] #最後までの距離
answer = 0
for i in range(1, N):
    answer += (X[i-1] * limit / i)%MOD
    answer %= MOD

print(answer)

Submission status: Runtime Error

Solution code:
import math
N=int(input())
X=list(map(int,input().split()))
kyori = []
kouho = 0
for i in range(N-1):
    kyori.append(X[i+1]-X[i])
for j in range(len(kyori)):
    if j == 0:
        kouho += sum(kyori)
    else:
        kouho += sum(kyori[j:])+sum(kyori[j-1:])+sum(kyori[:j-1])
        #print(sum(kyori)-sum(kyori[:j])+sum(kyori[j-1:]))
kouho /= 2
print(math.ceil(kouho*math.factorial(N-1))%(10**9+7))

Submission status: Runtime Error

Solution code:
import math
N = int(input())
xn = list(map(int, input().split()))

if N > 2000:
    print(0)
    exit()
else:
    ans = 0
    for i in range(N-1):
        for j in range(N-1-i):
            ans += (xn[i+1+j]-xn[i])/(N-1-j)
        for l in range(i):
            ans += (xn[i]-xn[i-j]/(N-1-l))
    print(int(ans*math.factorial(N-1)))

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

print(solve(x) % a)

Submission status: Runtime Error

Solution code:
import math
N = int(input())
xn = list(map(int, input().split()))

if N > 2000:
    print(0)
    exit()
else:
    ans = 0
    for i in range(N-1):
        for j in range(N-1-i):
            ans += (xn[i+1+j]-xn[i])/(N-1-j)
        for l in range(j):
            ans += (xn[i]-xn[i-j]/(N-1-l))
    print(int(ans*math.factorial(N-1)))

Submission status: Runtime Error

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

print(solve(x) % a)

Submission status: Runtime Error

Solution code:
import math
N = int(input())
xn = list(map(int, input().split()))

if N > 2000:
    exit()
ans = 0
for i in range(N-1):
    for j in range(N-1-i):
        ans += (xn[i+1+j]-xn[i])/(N-1-j)
    for l in range(j):
        ans += (xn[i]-xn[i-j]/(N-1-l))
print(int(ans*math.factorial(N-1)))

Submission status: Runtime Error

Solution code:
import math
N = int(input())
xn = list(map(int, input().split()))

ans = 0
for i in range(N-1):
    for j in range(N-1-i):
        ans += (xn[i+1+j]-xn[i])/(N-1-j)
    for l in range(j):
        ans += (xn[i]-xn[i-j]/(N-1-l))
print(int(ans*math.factorial(N-1)))

Submission status: Runtime Error

Solution code:
import random
N = int(input())
X = list(map(int, input().split()))
result, sum = 0, 0
for i in range(N-1):
  x = random.choice(X[:-1])
  sum += (X[-1]-x)
  result += (sum)*(1/(N-1))
  X.remove(x)

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

print((result*val)%1000000007)

Submission status: Runtime Error

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

a = 10**9+7

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

print(solve(x) % a)

Submission status: Runtime Error

Solution code:
N = int(input())
X = list(map(int, input().split()))
result, sum = 0, 0
for i in range(N-1):
  sum += X[-1]-X[i]
  result += (sum)*(1/(N-1))

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

print((result*val)%1000000007)

Submission status: Runtime Error

Solution code:
import math
N=int(input())
X=list(map(int,input().split()))
kyori = []
kouho = 0
for i in range(N-1):
    kyori.append(X[i+1]-X[i])
for j in range(len(kyori)):
    if j == 0:
        kouho += sum(kyori)
    else:
        kouho += sum(kyori)-sum(kyori[:j])+sum(kyori[j-1:])
        #print(sum(kyori)-sum(kyori[:j])+sum(kyori[j-1:]))
kouho /= 2
print(math.ceil(kouho*math.factorial(N-1))%(10**9+7))

Submission status: Runtime Error

Solution code:
import math
n = int(input())
ls_x = list(map(int,input().split()))
c1 = math.factorial(n-1)
c = [c1]
ans = 0
for i in range(2,n):
    # print(i)
    c.append(c[0]//i)
cn = sum(c)
for i in range(n-1):
    ans -= ls_x[i] * c[i]
    
ans += cn * ls_x[n-1]
ans = int(ans % (1e9+7))
print(ans)

Submission status: Runtime Error

Solution code:
N = int(input())
A = [int(a) for a in input().split()]
    
def cmb(n, r, mod):
    if ( r<0 or r>n ):
        return 0
    r = min(r, n-r)
    return g1[n] * g2[r] * g2[n-r] % mod

mod = 10**9+7 #出力の制限
g1 = [1, 1] # 元テーブル
g2 = [1, 1] #逆元テーブル
inverse = [0, 1] #逆元テーブル計算用テーブル

for i in range( 2, N + 1 ):
    g1.append( ( g1[-1] * i ) % mod )
    inverse.append( ( -inverse[mod % i] * (mod//i) ) % mod )
    g2.append( (g2[-1] * inverse[-1]) % mod )

if N <= 2000:
    L = [1]*N
    for i in range(1, N):
        L[i] *= L[i-1]*i
        L[i] %= mod
    
    ans = 0
    for i in range(1, N):
        ans += cmb(N-1, N-i, mod)*L[i-1]*L[N-i-1]*(A[N-1]-A[i-1])
        ans %= mod
        for j in range(i+1, N):
            ans += cmb(N-1, j-i+1, mod)*L[N-j+i-2]*L[j-i-1]*(A[j-1]-A[i-1])
            ans %= mod
        
print(ans)

Submission status: Runtime Error

Solution code:
# -*- coding: utf-8 -*-
import math
N = int(input())
X = list(map(int, input().split()))

def solver(N,X):
    r = X[1:]
    result = sum(r)/len(r) * math.factorial(N-1)
    if result>=10**9 +7:
        result = result%10**9 +7
    return int(result)

print(solver(N,X))

Submission status: Runtime Error

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

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

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

Submission status: Runtime Error

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

p = 1/(n-1)
ev = 0
m = 0
for i in range(n-1):
    m += x[-1]-x[i]
    ev += p*m
    print(m)
inf = 10**9+7
print(ev*math.factorial(n-1)%inf)

Submission status: Runtime Error

Solution code:
# -*- coding: utf-8 -*-
import math
N = int(input())
X = list(map(int, input().split()))

def solver(N,X):
    r = X[1:]
    result = sum(r)/len(r) * math.factorial(N-1)
    return result

print(solver(N,X))

Submission status: Runtime Error

Solution code:
n = int(input())
x_lis = list(map(int, input().split()))
def dfs(x_lis, dis):
    if len(x_lis) == 1:
        return dis % (10 ** 9 + 7)
    cur_dis = 0
    for i in range(len(x_lis) - 1):
        cur_dis += dfs(x_lis[:i] + x_lis[i + 1:], dis + x_lis[i + 1] - x_lis[i])
    return cur_dis % (10 ** 9 + 7)
print(dfs(x_lis, 0))

Submission status: Runtime Error

Solution code:
import math

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

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

res = m / (n-1) * math.factorial(n-1)
res %= 10**9 + 7
print(int(res))