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:
import math
N = int(input())
x = [int(_) for _ in input().split()]
wari = 10 ** 9 + 7
X = 2 ** (N - 2)
xMax = max(x)
w = 0

for i in range(N - 1):
    val = xMax - x[i]
    if i == 0:
        w += val * X
    else:
        w += val * (X // 2)

left = (w / X)
right = math.factorial(N - 1)
ans = int(left * right)
print(ans % wari)

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+=k/((n-1-i)*comb(n-1,n-1-i))
print(int(m*(math.factorial(n-1))%(10**9+7)))

Submission status: Runtime Error

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

def inv(x):
    return pow(x, MOD-2, MOD)
    
def C(n, r):
    #return fact[n]*inv(fact[r])*inv(fact[n-r])
    return fact[n]*inv_table[r]*inv_table[n-r]

N = int(input())

if N>3000:
    1/0
    
x = list(map(int, input().split()))
ans = 0
MOD = 10**9+7
fact = [1]

for i in range(1, N+1):
    fact.append(fact[-1]*i%MOD)

inv_table = [-1]*(N+1)

for i in range(N+1):
    inv_table[i] = inv(fact[i])
    
for i in range(N-1):
    for j in range(i+1, N):
        #print(i, j)
        
        #r = j-i+1
        add = 1
        
        if j==N-1:
            r = N-2-i+1
            add *= (x[j]-x[i])
            add %= MOD
            add *= C(N-1, r)
            add %= MOD
            add *= fact[r-1]
            add %= MOD
            add *= fact[N-1-r]
            add %= MOD
            #ans += (x[j]-x[i])*C(N-1, r)*fact[r-1]*fact[N-1-r]
            ans += add
            #print(C(N-1, r-1)*fact[N-1-r+1]*fact[r-2])
        else:
            r = j-i+1
            add *= (x[j]-x[i])
            add %= MOD
            add *= C(N-1, r)
            add %= MOD
            add *= fact[j-i-1]
            add %= MOD
            add *= fact[N-1-r]
            add %= MOD
            ans += add
            #ans += (x[j]-x[i])*C(N-1, r)*fact[j-i-1]*fact[N-1-r]
        #print(x[j]-x[i])
            #print(C(N-1, r)*fact[N-1-r]*fact[r-2])
        #ans += (x[j]-x[i])*C(N-1, r)*fact[N-1-r]
        #print('ans:', ans)
        ans %= MOD

#ans *= fact[N-1]
ans %= MOD

print(ans)

Submission status: Runtime Error

Solution code:
n = int(input())
x = list(map(int, input().split()))
d = []
for x1, x2 in zip(x, x[1:]):
    d.append(x2 - x1)
mod = 10 ** 9 + 7
base = 1

P = 10**9 + 7
N = n
inv_t = [0]+[1]
for i in range(2,N):
  inv_t += [inv_t[P % i] * (P - int(P / i)) % P]

for i in range(1, n):
    base *= i
    base %= mod
ans = 0
b = base
for i, di in enumerate(d):
    ans += (base * di)  % mod
    ans %= mod
    base += b * inv_t[i+2]
    base % mod
print(ans)

Submission status: Runtime Error

Solution code:
import copy
def process(total, short_ls):
    lg = len(short_ls)
    if lg == 2:
        #print("returnきた")
        return short_ls[1]-short_ls[0]
    else:
        #print("short_ls", short_ls,"lg",lg)
        for i in range(lg-1):
            a = copy.copy(short_ls)
            #print("i",i,"a[1]",a[1])
            total+=(a[i+1]-a[i])
            total%=(10**9+7)
            a.pop(i)
            #print("a_pop_len",len(a))
            p = process(total, a)
            if type(p) is int:
                total+=p
                total%=(10**9+7)

        return total
if __name__ == "__main__":
    n = int(input())
    x_ls = list(map(int, input().split()))
    sub = process(0, x_ls)
    print(sub)

Submission status: Runtime Error

Solution code:
n = int(input())
x = list(map(int, input().split()))
d = []
for x1, x2 in zip(x, x[1:]):
    d.append(x2 - x1)
mod = 10 ** 9 + 7
base = 1

P = 10**9 + 7
N = 10000
inv_t = [0]+[1]
for i in range(2,N):
  inv_t += [inv_t[P % i] * (P - int(P / i)) % P]

for i in range(1, n):
    base *= i
    base %= mod
ans = 0
b = base
for i, di in enumerate(d):
    ans += (base * di)  % mod
    ans %= mod
    base += b * inv_t[i+2]
    base % mod
print(ans)

Submission status: Runtime Error

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

import math

num = int(input())
slimes = list(map(int, input().split()))

w = 0
for i in range(0,num-1):
    tmp = 0
    for j in range(0,num-1-i):
        tmp = tmp + (slimes[num-j-1] - slimes[i])
    
    w = w + (tmp * (math.factorial(num - 1) / (num - 1 - i)))

ans = w % (pow(10,9) + 7)
         
print(int(ans))

Submission status: Runtime Error

Solution code:
# give up
import math
n = int(input())
x = list(map(int,input().split()))

ans = 0
i = 1
while True:
    for k in range(n-i):
        ans += (1/(n-i)) * (x[k+i]-x[k])
    i += 1
    if i == (n-1):
        break
    else:
        ans += ans
for k in range(n-1):
    ans +=  (x[n-1] - x[k])/2

ans = (ans * math.factorial(n-1)) % 1000000007
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):
    count += 1/(i+1)
    kai *= (i+1)
    
    ans += sa[i]*count
    ans %= mod

print(int(kai*ans%mod))

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

D = list()
for i in range(N-1):
  D.append((X[i+1]-X[i])%dev)


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 += (D[i]*now) % dev

print(int(s%dev))

Submission status: Runtime Error

Solution code:
from itertools import combinations
n=int(input())
xs=list(map(int,input().split()))
ub=1000000007
res=0
memo={0:1}
def frac(n):
  if n in memo:
    return memo[n]
  return frac(n-1)*n
def sol(xs):
  if len(xs)==2:
    return(xs[1]-xs[0])
  return sum(xs[i+1]-xs[i]+sol(xs[:i]+xs[i+1:]) for i in range(len(xs)-1))
print(sol(xs))

Submission status: Runtime Error

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

move = 0


for i in range(n-1):
    ls = x.copy()
    move += abs(ls[i]-ls[i+1])
    ls.remove(ls[i])
    for j in range(n-2):
        move += abs(ls[j+1]-ls[j])
        
ans = move*(1/(n-1))
print(round((math.factorial(n-1)*ans)%(10**9+7)))

Submission status: Runtime Error

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

def inv(x):
    return pow(x, MOD-2, MOD)
    
def C(n, r):
    return fact[n]*inv(fact[r])*inv(fact[n-r])

N = int(input())

if N>3000:
    1/0
    
x = list(map(int, input().split()))
ans = 0
MOD = 10**9+7
fact = [1]

for i in range(1, N):
    fact.append(fact[-1]*i%MOD)
    
for i in range(N-1):
    for j in range(i+1, N):
        #print(i, j)
        
        #r = j-i+1
        add = 1
        
        if j==N-1:
            r = N-2-i+1
            add *= (x[j]-x[i])
            add %= MOD
            add *= C(N-1, r)
            add %= MOD
            add *= fact[r-1]
            add %= MOD
            add *= fact[N-1-r]
            add %= MOD
            #ans += (x[j]-x[i])*C(N-1, r)*fact[r-1]*fact[N-1-r]
            ans += add
            #print(C(N-1, r-1)*fact[N-1-r+1]*fact[r-2])
        else:
            r = j-i+1
            add *= (x[j]-x[i])
            add %= MOD
            add *= C(N-1, r)
            add %= MOD
            add *= fact[j-i-1]
            add %= MOD
            add *= fact[N-1-r]
            add %= MOD
            ans += add
            #ans += (x[j]-x[i])*C(N-1, r)*fact[j-i-1]*fact[N-1-r]
        #print(x[j]-x[i])
            #print(C(N-1, r)*fact[N-1-r]*fact[r-2])
        #ans += (x[j]-x[i])*C(N-1, r)*fact[N-1-r]
        #print('ans:', ans)
        ans %= MOD

#ans *= fact[N-1]
ans %= MOD

print(ans)

Submission status: Runtime Error

Solution code:
import math

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

MOD = 10 ** 9 + 7

diffList = [0] * (n - 1)
for i, position in enumerate(positionList):
  if i == 0:
    continue
  diffList[i - 1] = position - positionList[i - 1]

weightList = [0] * (n - 1)
for i in range(n - 1):
  for j in range(n - i - 1):
    weightList[i + j] += int(math.factorial(n - 1) / (i + 1))

result = 0
for i in range(n - 1):
  result = (result + diffList[i] * weightList[i]) % MOD
print(result)

Submission status: Runtime Error

Solution code:
# -*- coding: utf-8 -*-
 
import math
import itertools
import sys
import copy
 
# 入力
#A, B, C, D = map(int, input().split())
#L = list(map(int, input().split()))
#S = list(str(input()))
N = int(input())
L = list(map(int, input().split()))

dd = (10 ** 9) + 7
sum = 0
def srch(L, DelIndex) :
  global sum
  #print("srch IN")
  #print(len(L))
  if DelIndex != None :
    L.pop(DelIndex)
  if len(L) == 1 :
    #print("return")
    return 0
  else :
    #print(L)
    #print(len(L)-1)
    for i in range(len(L)-1) :
      #print("i")
      #print(i)
      #print("L[i]")
      #print(L[i])
      #print("L[i+1]")
      #print(L[i+1])
      sum += L[i+1]-L[i]
      copyL = copy.deepcopy(L)
      srch(copyL, i)

DelIndex = None
srch(L, DelIndex)

print (sum%dd)

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 = np.zeros((N-1,N-1), dtype=np.int64)
dif = np.fmax(x[1:].reshape(-1, N-1) - x[:N-1].reshape(N-1, -1), 0)
# print(dif)

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

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

Submission status: Runtime Error

Solution code:
from itertools import combinations
n=int(input())
xs=list(map(int,input().split()))
ub=1000000007
res=0
memo={0:1}
def frac(n):
  if n in memo:
    return memo[n]
  return frac(n-1)*n
def sol(xs):
  if len(xs)==2:
    return(xs[1]-xs[0])
  return sum(xs[i+1]-xs[i]+sol(xs[:i]+xs[i+1:]) for i in range(len(xs)))
print(sol(xs))

Submission status: Runtime Error

Solution code:
import math

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)

f = math.factorial(n-1)

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:
import math

MOD = 10**9 + 7
N = int(input())
x = [int(i) for i in input().split()]
x.reverse()
f_n_1 = math.factorial(N - 1)

ans = 0
for i, x_i in enumerate(x):
    if i == 0:
        continue
    tmp = 0
    for j in range(i):
        diff = x[j] - x_i
        tmp += diff
    tmp = tmp * f_n_1 / i
    ans += tmp
    ans %= MOD

ans = int(ans)
print(ans)

Submission status: Runtime Error

Solution code:
import sys
import numpy as np
from itertools import permutations
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(500000)

MOD = 1000000000 + 7

N, *X = map(int, read().split())
cnt = 1
for i in range(1, N):
    cnt *= i
ans = 0

"""
l = list(permutations(range(1, N)))
for per in l:
    ans += np.abs(np.diff(np.array(list(per) + [N]))).sum()
    ans %= MOD
    print(ans, per)
"""

for i in range(1, N):
    for j in range(1, i+1):
        ans += j * (cnt / i)

print(int(ans % MOD))

Submission status: Runtime Error

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

def inv(x):
    return pow(x, MOD-2, MOD)
    
def C(n, r):
    return fact[n]*inv(fact[r])*inv(fact[n-r])

N = int(input())

if N>3000:
    1/0
    
x = list(map(int, input().split()))
ans = 0
MOD = 10**9+7
fact = [1]

for i in range(1, N):
    fact.append(fact[-1]*i%MOD)
    
for i in range(N-1):
    for j in range(i+1, N):
        #print(i, j)
        
        r = j-i+1
        
        if j==N-1:
            r = N-2-i+1
            ans += (x[j]-x[i])*C(N-1, r)*fact[r-1]*fact[N-1-r]
            #print(C(N-1, r-1)*fact[N-1-r+1]*fact[r-2])
        else:
            r = j-i+1
            ans += (x[j]-x[i])*C(N-1, r)*fact[j-i-1]*fact[N-1-r]
        #print(x[j]-x[i])
            #print(C(N-1, r)*fact[N-1-r]*fact[r-2])
        #ans += (x[j]-x[i])*C(N-1, r)*fact[N-1-r]
        #print('ans:', ans)
        ans %= MOD

#ans *= fact[N-1]
ans %= MOD

print(ans)

Submission status: Runtime Error

Solution code:
import math

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)

f = math.factorial(n-1)

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:
#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[:]
    #     print(i,slimes2)
    #     r = (slimes2[i+1]-slimes2[i]) * f / (N-k-1) % MOD
    #     slimes2.pop(i)
    #     r += calc(k+1,slimes2) 
    #     return r % MOD
    f = factorial(N-1)
    def calc(k,slimes):
        if k == N-1:
            return 0
        r = 0


        for k in range(N-1):
            # print(slimes)
            i = N-k-2
            r += (slimes[-1]-slimes[-2-k]) * f / (N-k-1) % MOD
            # slimes.pop(i)
            # r += calc(k+1,slimes) 
        return r % MOD
    
    print(int(calc(0,X)))




if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
import math

dev = 10**9+7

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

D = list()
for i in range(N-1):
  D.append((X[i+1]-X[i])%dev)


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

print(int(s%dev))

Submission status: Runtime Error

Solution code:
def per(n, r, mod):
    if ( r<0 or r>n ):
        return 0
    r=n-r
    return g1[n] * g2[r] % mod

mod = 10**9+7 #出力の制限
N = 10**4
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 )


n=int(input())
A=[1]
for i in range(1,n-1):
  A.append(A[-1]*(i+1)+per(i,i,mod))
X=list(map(int,input().split()))
Y=[X[1]-X[0]]
for i in range(2,n):
  Y.append(X[i]-X[i-1])
ans=0
for i in range(n-1):
  if n-2-i==0:
    ans=ans+A[i]*Y[i]
  else:
    ans=(ans+A[i]*per(n-1,n-2-i,mod)*Y[i])%(10**9+7)
print(ans%mod)

Submission status: Runtime Error

Solution code:
from statistics import mean
from math import factorial
import numpy as np
N = int(input())
H = list(map(int, input().split()))
MOD = 1000000007
diff=[]
for i in range(N-1):
    diff.append(H[i+1]-H[i])

diff=np.array(diff)
multi=np.array([mean(list(range(1,i+1)))*factorial(N-1) for i in range(1,N)])
# print(multi)
# print(diff)
print(int(np.sum(diff*multi)%MOD))

Submission status: Runtime Error

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

dic = {}

class Node:
    def __init__(self, elm):
        self.elm = elm
        self.nexts = []
        self.next_costs = []
        self.prev = []
        
    def __str__(self):
        return str(self.elm)

root = Node(X)
dic[str(root)] = root

def reg(curr, co):
    if len(curr) == 1:
        return;
    cn = dic[str(curr)]
    costs = []
    for idx, x in enumerate(curr[:-1]):
        cost = curr[idx+1] - x + co
        cn.next_costs.append(cost)
        n = curr[:idx] + curr[idx+1:]
        if str(n) in dic:
            dic[str(n)].prev.append(cn)
            continue
        nn = Node(n)
        nn.prev.append(cn)
        dic[str(n)] = nn
        costs.append(reg(n, cost))
reg(X, 0)
cs = []
c = 0
for n in dic[str([max(X)])].prev:
    cs.append(n.next_costs[0])
c = np.mean(cs)
for i in range(2, N):
    c = c* i
print(int(c % ((10 ** 9) +7)))

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) 
 
print(int(np.sum(L * dist * n_1)) % MOD)

Submission status: Runtime Error

Solution code:
import math

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

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

dev = 10**9+7
s = 0
now = 0
for i in range(N-1):
  now += math.factorial(N-1)/(i+1)%dev
  s += (D[i]*now) % dev

print(int(s%dev))

Submission status: Runtime Error

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

k = N - 1

MoveDist = 0

def Dist(count, DistSum = 0):
  for i in reversed(range(count)):
    DistSum = x[count] - x[count - 1]
    DistSum *= K - count
  
MoveDist = Dist(k)

Submission status: Runtime Error

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

s = 1
for i in range(2, n):
    s *= i
ans = sum(d) * s % MOD

for i in range(m):
    p = 1
    for j in range(i+1, n-1):
        c = j - i
        p *= c / (c+1)
        ans += d[j] * int(p * s)
        ans %= MOD

print(int(ans))

Submission status: Runtime Error

Solution code:
N = int(input())
X = list(map(int, input().split()))
d = [dj - di for di, dj in zip(X, X[1:])][::-1]

MOD = 10**9 + 7
count_d = [[0] * (N) for i in range(N-1)]
count_d[0][1] = 1

fac = 1
for n in range(1, N-1):
  fac = fac*n % MOD
  for i in range(1, n+2):
    count_d[n][i] = ((n+1-i+1)*count_d[n-1][i] + (i-1)*count_d[n-1][i-1] + fac)%MOD

ans = 0
for di, ci in zip(d, count_d[-1][1:]):
  ans += di*ci % MOD
  ans %= MOD

print(ans)

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 / (N - 1) * tmp) % (10 ** 9 + 7))

Submission status: Runtime Error

Solution code:
from fractions import Fraction
import math
def main():
    N = int(input())
    x = list(map(int, input().split()))
    j = 10**9 + 7
    s = 0
    for i in range(1,len(x)):
        s += x[i]
    ex_val = float(s)/float(N-1)
    fact = math.factorial(N-1)
    print(int(ex_val*fact)%j)

if __name__ == '__main__':
    main()

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 = np.zeros((N-1,N-1), dtype=np.int64)
dif = np.fmax(x[1:].reshape(-1, N-1) - x[:N-1].reshape(N-1, -1), 0)
# print(dif)

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

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

Submission status: Runtime Error

Solution code:
import math

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

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

dev = 10**9+7
s = 0
now = 0
for i in range(N-1):
  now += math.factorial(N-1)/(i+1)
  s += (D[i]*now) % dev

print(int(s%dev))

Submission status: Runtime Error

Solution code:
def all_sum(num):
  return num * (num + 1) // 2
def all_multi(num):
  if num == 1:
    return 1
  else:
    return num * all_multi(num-1)
  
n = int(input())
x_list = [int(i) for i in input().split()]
l_list = []
for i in range(1, n):
  l_list.append(x_list[i] - x_list[~-i])
n_kaizyo = all_multi(~-n)
print(n_kaizyo)
a_list = [n_kaizyo//i * all_sum(i) for i in range(1, n)]

ans = 0
for a, l in zip(a_list, l_list):
  print(a, l)
  ans += a*l

print(ans%(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(N+2)]
dp[1][0] = -1
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
    for j in range(1,i+2):
        if i+1==j:
            dp[i][j] = (dp[i-1][j-1]*(i-0) + a)%MOD
        elif j==1:
            dp[i][j] = (dp[i-1][j]*(i-1) - a)%MOD
        else:
            dp[i][j] = (dp[i-1][j]*(i-j) + dp[i-1][j-1] * (j-1))%MOD
    #print(dp[i])
res = 0
for i in range(N):
    res += dp[N-1][i+1]*x[i]
#for i in range(1,N):
#    res = (res*i)%MOD
print(res%MOD)

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+20)

StrN=[0]*(n+10)
StrN[1]=1
StrN[0]=0
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 math

a = int(input())
b = list(map(int, input().split())) 

print(int((((sum(b) - b[0]) / (a - 1)) * math.factorial(a - 1)) % (1000000000 + 7) ))

Submission status: Runtime Error

Solution code:
import copy

def function(xs):
    res=None
    l=len(xs)
    if l==2:
        res=xs[1]-xs[0]
    else:
        res=0
        for i in range(l-1):
            xstemp=copy.copy(xs)
            v=xstemp[i+1]-xstemp[i]
            xstemp.pop(i)
            res+=v+function(xstemp)
            
            
    return res
    
N=int(input())

data=input().split(" ")

Xs=[]

for x in data:
    Xs.append(int(x))



ans=function(Xs)

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

Submission status: Runtime Error

Solution code:
N = int(input())
x = list(map(int, input().split()))
import math
import numpy as np
mod = 10**9+7
Y = 0
Z = 0
n = 1
x = np.array(x)
w = np.diff(x)
A = math.factorial(N-1)
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=[int(i) for i in input().split()]
mod=10**9+7
n_max=n
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]
    ans=(ans+j*DP[i][j]*(X[i]-X[i-1])*F[n-1]//F[i])%mod
print(ans)

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 = 1
# factorial = len(list(itertools.permutations(new_l)))
for i in range(1, n):
    factorial *= i
# print(factorial)

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