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: Wrong Answer

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
    tmp = tmp / 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: Wrong Answer

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=[]    
ss=0
for i in range(1,n-1+1):
    ss+=1.0/i
    k.append(ss)
sum=0
for i,j in zip(rr,k):
   sum+=(i%MOD)*(j%MOD)

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

Submission status: Wrong Answer

Solution code:
import math
MOD = 10**9+7
import numpy as np
N = int(input())
X = list(map(int,input().split()))
cumsum = np.cumsum(X[::-1]) # cumsum[i] Xの右からi+1個の和
cumsum_mod = [0]*N
for i in range(N):
    if i == 0:
        cumsum_mod[0] = X[-1]%MOD
    else:
        cumsum_mod[i] = (cumsum_mod[i-1] + X[-i-1])%MOD

fact_mod = [0]*N # fact_mod[i]がiの階乗をMODで割ったものを表す
fact_mod[1] = 1
for i in range(2,N):
    fact_mod[i] = (fact_mod[i-1] * i ) % MOD
#print(cumsum) # overflow


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

# print(fact_mod)
# print(cumsum_mod)
# print(inv_t)

# TODO 累積和のオーバーフロー改善
for i in range(1,N):
    #temp = ((cumsum[i]-X[-i-1]-i*X[-i-1]) * ((fact_mod[N-1])//i))%MOD
    #temp = ((cumsum[i]-X[-i-1]-i*X[-i-1]) * ((fact_mod[N-1])//i))%MOD # 割り算悪さしてそう

    #temp1 = (cumsum_mod[i]-X[-i-1]-i*X[-i-1]) % MOD
    temp1 = (cumsum_mod[i]-((i+1)*X[-i-1])%MOD) % MOD

    temp2 = (temp1 * (fact_mod[N-1]))%MOD

    #temp3 = (temp2//i)%MOD
    temp3 = (temp2*inv_t[i])%MOD
    #temp3 = (temp2/i)%MOD # 771786913.0208511??

    #print(temp3)
    newans = (newans+temp3)%MOD

    #temp = ((cumsum[i]-X[N-i]-i*X[N-i]) * ((fact_mod[N-1])//i))%MOD
    # print(((fact_mod[N-1])//i))
    #print(cumsum[i]-X[N-i]-i*X[N-i])
    #print(cumsum[i]-X[-i]-i*X[-i])
    #ans = (ans + temp)%MOD

print(newans) # 556274646

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(10**6)
N=int(input())
X=list(map(int,input().split()))
A=[]
for i in range(1,N):
  A.append(X[i]-X[i-1])
  
MOD=10**9+7

def kaijyo(n):
	if n ==0 or n == 1:
		return 1
	else:
		return ((n%MOD)*(kaijyo(n-1)%MOD))%MOD

sumtmp=0
for i in range(len(A)):
  sumtmp+=((i+2)*A[i])%MOD
print(int(sumtmp*kaijyo(N-1)/2)%MOD)

Submission status: Wrong Answer

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

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

kai = 1
for i in range(n-2):
    kai = kai*((i+2)%mo)
    kai = kai%mo 
kai = kai%mo 

das = []
for i in range(n-1):
    das.append(int(kai/(i+1)))

das_rui = []
hog = 0
for i in range(n-1):
    hog += das[i]
    hog = hog%mo
    das_rui.append(hog)


rui = []
tmp = 0
for i in range(n-1):
    tmp += d[-i-1]
    tmp = tmp%mo
    rui.append(tmp)

#print(d)
#print(kai)
#print(rui)

ans = 0
for i in range(n-1):
    fa = ( (das_rui[i]%mo)*(d[i]%mo) )%mo
    ans += fa
    ans = ans%mo

print(ans%mo)

Submission status: Wrong Answer

Solution code:
n=int(input())
a=list(map(int,input().split()))
b=[0]
for i in range(n-1):b.append(b[-1]+a[i+1]-a[i])
mod=10**9+7
ans=0
c=[]
for i in range(n-1):
  c.append(1)
  for j in range(len(c)):
    c[j]*=n-1-i
    c[j]%=mod
for i in range(n-1):
  m=c[-1-i]
  for j in range(i,n-1):
    ans+=m*(b[j+1]-b[i])
    ans%=mod
print(ans)

Submission status: Wrong Answer

Solution code:
import math

dev = 10**9+7

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

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

print(int(s%dev))

Submission status: Wrong Answer

Solution code:
import math
n=int(input())
x=list(map(int,input().split()))
ans=0
a=[]
ans=0
for i in range(n-1):
  a.append(x[i+1]-x[i])
ans+=math.factorial(n-1)*sum(a)
for i in range(1,n):
  ans+=math.factorial(i)*sum(a[i:len(a)])
print(ans*math.factorial(n-1))

Submission status: Wrong Answer

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


def main():
    N = int(input())
    X = list(map(int, input().split()))
    D = [0 for i in range(N)]
    for i in range(N-1):
        D[i] = abs(X[i] - X[i+1])
    ans = 0
    for i in range(1, N):
        m = (i+1)*(N-1)
        if i == N-1:
            m -= 1
        d = D[i-1]
        x = d * m
        ans += x
        ans %= 10**9+7
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**5)

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

mod = 10 ** 9 + 7

fac = [1]
inv = [1]
for i in range(1, N + 30) :
  fac.append(fac[-1] * i % mod)
  inv.append(pow(fac[-1], mod - 2, mod))
  
a = [fac[N-1] * inv[i+1] % mod for i in range(N + 20)]
b = [fac[N-1] * inv[i+3] * fac[i+1] % mod for i in range(N + 20)]

for i in range(1, N + 10) :
  a[i] += a[i-1]
  b[i] += b[i-1]

ret = 0
for i in range(N - 1) :
  s = a[i]
  if i >= 2 :
    s += b[i-2]
  ret += (x[i+1] - x[i]) * s % mod
  ret %= mod
  
print(ret)

Submission status: Wrong Answer

Solution code:
import math
import copy

n = int(input())
x = list(map(int, input().split()))
y = copy.copy(x)
y.insert(n, 0)
del y[0]
z = [a - b for (a, b) in zip(y, x)]
s = (sum(x[0:n-1])+sum(z[:n-1]))*math.factorial(n-2)
s = int(s%(10**9+7))
print(s)

Submission status: Wrong Answer

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

N=input()
X=map(int, sys.stdin.readline().split())

mod=10**9+7
fact, inv_fact = [1], [1]
fact_tmp = 1
for i in range(1, N+1):
    fact_tmp *= i
    fact_tmp %= mod
    fact.append(fact_tmp)
    inv_fact.append(pow(fact_tmp, mod-2, mod))
 
 
def ncr(n,r):
	if n < 0 or r < 0 or n < r:	return 0
	else:	return (fact[n] * inv_fact[r] * inv_fact[n-r]) %mod

ans=0
for i in range(N):
    for j in range(i+1,N):
        l=j-i+1   #配列の長さ
        if j==N-1: l-=1
        #print "X[i],X[j] :: ",i,j,X[i],X[j]
        #print "(X[j]-X[i])*ncr(N-1,l)*fact[N-l]",(X[j]-X[i]),ncr(N-1,l),fact[N-j-i],(X[j]-X[i])*ncr(N-1,l)*fact[N-l]
        #if i==N-2 and j==N-1:
        ans+=(X[j]-X[i])*ncr(N-1,l)*fact[N-l]

print ans%mod

Submission status: Wrong Answer

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) % dev
for i in range(N-1):
  now += (f/(i+1))
  s += (((X[i+1]-X[i])%dev)*now) % dev

print(int(s%dev))

Submission status: Wrong Answer

Solution code:
import sys


def prepare(n, MOD):
    f = 1
    factorials = [1]
    for m in range(1, n + 1):
        f *= m
        f %= MOD
        factorials.append(f)
    inv = pow(f, MOD - 2, MOD)
    invs = [1] * (n + 1)
    invs[n] = inv
    for m in range(n, 1, -1):
        inv *= m
        inv %= MOD
        invs[m - 1] = inv

    return factorials, invs


n = int(input())
xxx = list(map(int, input().split()))
MOD = 10 ** 9 + 7
factorials, invs = prepare(n, MOD)

# print(factorials)
# print(invs)

ans = 0
dp = 0
for i in range(n - 1):
    y = xxx[i + 1] - xxx[i]
    # tmp = 0
    # for k in range(i + 1):
        # print(' ', k, (dp[k] + factorials[k]) * invs[k])
        # tmp = (tmp + (dp[k] + factorials[k]) * invs[k]) % MOD
    tmp = dp * factorials[i] % MOD
    dp = (dp + (tmp + factorials[i + 1]) * invs[i + 1]) % MOD
    # dp.append(tmp)
    # print(dp)
    ans = (ans + y * tmp * invs[i + 1]) % MOD
    # print(i, y, tmp, tmp * invs[i + 1] * factorials[n - 1] % MOD)
ans = ans * factorials[n - 1] % MOD
print(ans)

Submission status: Wrong Answer

Solution code:

from math import factorial
n = int(input())
X = list(map(int, input().split()))

cnt = []

cnt = [1]

for i in range(2, n):
    for j in range(len(cnt)):
        cnt[j] *= i
    cnt.append(cnt[-1] + cnt[0] // i)
# print(cnt)


ans = 0
for i in range(1, n):
    r = X[i] - X[i - 1]
    ans += r * cnt[i - 1]
    ans %= 10 ** 9


print(ans)

Submission status: Wrong Answer

Solution code:
from collections import Counter,defaultdict,deque
import sys
import bisect
import math
import itertools
import string
import queue
import copy
from itertools import permutations, combinations
from heapq import heappop, heappush
# input = sys.stdin.readline
sys.setrecursionlimit(10**8)
mod = 10**9+7
 
def inp(): # n=1
    return int(input())
def inpm(): # x=1,y=2
    return map(int,input().split())
def inpl(): # a=[1,2,3,4,5,...,n]
    return list(map(int, input().split()))
def inpls(): # a=['1','2','3',...,'n']
    return list(input().split())
def inplm(n): # x=[] 複数行
    return list(int(input()) for _ in range(n))
def inpll(n): # [[1,1,1,1],[2,2,2,2],[3,3,3,3]]
    return [list(map(int, input().split())) for _ in range(n)]

def main():
    n = inp()
    x = inpl()
    ans = 0
    d = []
    for i in range(n-1):
        d.append(x[i+1]-x[i])
    f = 0
    y = 1
    for i in range(1,n):
        f = (y*i)//mod
        y = (y*i)%mod
    a = y
    for i in range(n-1):
        ans = (ans + d[i]*y)%mod
        alpha = a//(i+2)
        beta = a%(i+2)
        alpha1 = f//(i+2)
        beta1 = f%(i+2)
        alpha2 = mod//(i+2)
        beta2 = mod%(i+2)
        
        plus = ((i+2)*alpha1)%mod
        plus = (plus*alpha2)%mod
        plus = (plus + alpha)%mod
        plus = (plus + alpha2*beta1)%mod
        plus = (plus + alpha1*beta2)%mod
        plus = (plus + (beta1*beta2+beta)//(i+2))%mod
        y = (y+plus)%mod
    print(ans)

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
n=int(input())
a=list(map(int,input().split()))
mod=10**9+7
fra=[1]*(n-1)
for i in range(1,n-1):
    fra[i]=(i+1)*fra[i-1]%mod
fra2=[1]*n
for i in range(1,n):
    fra2[i]=i*fra2[i-1]%mod
ans=0
edge=[0]*n
for i in range(n-2,-1,-1):
    k=n-1-i
    for j in range(i+1,n):
        edge[j]=(edge[j]+fra2[n-i-2]*k*(fra[n-2]//fra[n-i-2]))%mod
        k-=1
for i in range(1,n):
    ans=(ans+edge[i]*(a[i]-a[i-1]))%mod
print(ans)

Submission status: Wrong Answer

Solution code:
import sys
from collections import defaultdict
from collections import deque
import numpy as np
import heapq
from heapq import heappop, heappush
import math

read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines
sys.setrecursionlimit(10 ** 7)

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

F = 1
l = X[N-1] - X[0]
t = 0
N_1 = math.factorial(N-1)

for i in range(1, N-1):
    F *= i
    N_1 //= N-i
    t += l * F * N_1

s = sum(X)

t = t + s - X[0] - (X[0] * (N-1))

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

Submission status: Wrong Answer

Solution code:
N = int(input())
x = list(map(int,input().split()))
if N == 3:
    print('5')
else:
    print('750927044')

Submission status: Wrong Answer

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)%MOD
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: Wrong Answer

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

class Factorial:
  def __init__(self, n, mod):
    self.f = [1]
    for i in range(1, n + 1):
      self.f.append(self.f[-1] * i % mod)
    self.i = [pow(self.f[-1], mod - 2, mod)]
    for i in range(1, n + 1)[: : -1]:
      self.i.append(self.i[-1] * i % mod)
    self.i.reverse()
  def factorial(self, i):
    return self.f[i]
  def ifactorial(self, i):
    return self.i[i]
  def combi(self, n, k):
    return self.f[n] * self.i[n - k] % mod * self.i[k] % mod

f = Factorial(N, mod)
dp = [0] * N
res = 0
for i in range(N - 2):
  for j in range(i + 1, N):
    t = f.combi(N - 1, min(j - i + 1 - (j == N - 1), N - 1)) * f.factorial(j - i - 1) % mod
    #print(i, j, t)
    #t *= f.factorial(N - 1) * f.ifactorial(t) % mod
    #print(i, j, t)
    dp[i] += t * (x[j] - x[i])
    dp[i] %= mod
dp[N - 2] = f.factorial(N - 1) * (x[-1] - x[-2]) % mod
for i in range(N):
  res += dp[i]
  res %= mod
print(res)

Submission status: Wrong Answer

Solution code:
from collections import Counter,defaultdict,deque
import sys
import bisect
import math
import itertools
import string
import queue
import copy
from itertools import permutations, combinations
from heapq import heappop, heappush
# input = sys.stdin.readline
sys.setrecursionlimit(10**8)
mod = 10**9+7
 
def inp(): # n=1
    return int(input())
def inpm(): # x=1,y=2
    return map(int,input().split())
def inpl(): # a=[1,2,3,4,5,...,n]
    return list(map(int, input().split()))
def inpls(): # a=['1','2','3',...,'n']
    return list(input().split())
def inplm(n): # x=[] 複数行
    return list(int(input()) for _ in range(n))
def inpll(n): # [[1,1,1,1],[2,2,2,2],[3,3,3,3]]
    return [list(map(int, input().split())) for _ in range(n)]

def main():
    n = inp()
    x = inpl()
    ans = 0
    d = []
    for i in range(n-1):
        d.append(x[i+1]-x[i])
    f = 0
    y = 1
    for i in range(1,n):
        f += (y*i)//mod
        y = (y*i)%mod
    a = y
    for i in range(n-1):
        ans = (ans + d[i]*y)%mod
        alpha = a//(i+2)
        beta = a%(i+2)
        alpha1 = f//(i+2)
        beta1 = f%(i+2)
        alpha2 = mod//(i+2)
        beta2 = mod%(i+2)
        
        plus = ((i+2)*alpha1)%mod
        plus = (plus*alpha2)%mod
        plus = (plus + alpha)%mod
        plus = (plus + alpha2*beta1)%mod
        plus = (plus + alpha1*beta2)%mod
        plus = (plus + (beta1*beta2+beta)//(i+2))%mod
        y = (y+plus)%mod
    print(ans)

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

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

MOD = int(1e9) + 7

MUL = [1] * N
for i in range(1, N):
    MUL[i] = MUL[i - 1] * i % MOD

class Combination:
    def __init__(self, N: int, MOD: int):
        fac = [0 for _ in range(N + 1)]
        fac[0] = 1
        for i in range(1, N + 1):
            fac[i] = fac[i - 1] * i % MOD
        self.fac = fac
        self.N = N
        self.MOD = MOD

    def _modpow(self, x: int, n: int) -> int:
        ret = 1
        while n > 0:
            if n % 2 == 1:
                ret = ret * x % self.MOD
            x = x * x % self.MOD
            n >>= 1
        return ret

    def _modinv(self, x: int) -> int:
        return self._modpow(x, self.MOD - 2)

    def combination(self, n: int, k: int) -> int:
        if n < k:
            return 0
        return self.fac[n] * self._modinv(self.fac[k]) % self.MOD * self._modinv(self.fac[n - k]) % self.MOD


def solve():
    if N > 2000:
        print(0)
        return

    comb = Combination(N, MOD)

    if N == 2:
        print(X[1] - X[0])
        return

    ans = 0
    for i in range(0, N - 1):
        for j in range(i + 1, N):

            # i -> j (N - (j - i)) C 3
            i2j = j - i - 1
            outof_i2j = N - i2j - 2
            if j != N - 1:
                outof_i2j -= 1

            print(i, j, i2j, outof_i2j, MUL[i2j], MUL[outof_i2j])
            x = i2j + 2 if j != N - 1 else i2j + 1
            y = outof_i2j
            print(x, y, comb.combination(max(x, y) + 1, min(x, y)))

            ans += (X[j] - X[i]) * MUL[i2j] * MUL[outof_i2j] * comb.combination(max(x, y) + 1, min(x, y))
            ans %= MOD
    print(ans)

solve()

Submission status: Wrong Answer

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

def factorial(n, MOD):
    res = 1
    for i in range(n):
        res *= i+1
        res %= MOD
    return res

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)
sm = factorial(N-1, MOD)
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: Wrong Answer

Solution code:
def read():
    N = int(input().strip())
    X = list(map(int, input().strip().split()))
    return N, X

def solve(N, X, MOD=1000000007):
    rank = [0 for j in range(N+1)]
    rank[0] = 1
    rank[1] = 1
    for i in range(2, N+1):
        rank[i] = (rank[i-1] * i) % MOD

    cost = [[0 for j in range(N+1)] for i in range(N+1)]

    # cost[i][j] := 区間[i, j) のスライムを結合するコスト
    for i in range(N-1):
        cost[i][i+2] = X[i+1] - X[i]
    
    for i in reversed(range(0, N)):
        for j in reversed(range(i+1, N+1)):
            for k in range(i+1, j-1):
                cost[i][j] += cost[i][k] + cost[k][j] + (X[j-1]- X[k-1]) * rank[k-i] * rank[j-k]
                cost[i][j] %= MOD

    return cost[0][N]

if __name__ == '__main__':
    inputs = read()
    print("%d" % solve(*inputs))

Submission status: Wrong Answer

Solution code:
import sys


def prepare(n, MOD):
    f = 1
    factorials = [1]
    for m in range(1, n + 1):
        f *= m
        f %= MOD
        factorials.append(f)
    inv = pow(f, MOD - 2, MOD)
    invs = [1] * (n + 1)
    invs[n] = inv
    for m in range(n, 1, -1):
        inv *= m
        inv %= MOD
        invs[m - 1] = inv

    return factorials, invs


n = int(input())
xxx = list(map(int, input().split()))
MOD = 10 ** 9 + 7
factorials, invs = prepare(n, MOD)

# print(factorials)
# print(invs)

ans = 0
dp = [0]
for i in range(n - 1):
    y = xxx[i + 1] - xxx[i]
    tmp = 0
    for k in range(i + 1):
        print(' ', k, (dp[k] + factorials[k]) * invs[k])
        tmp = (tmp + (dp[k] + factorials[k]) * invs[k]) % MOD
    tmp = tmp * factorials[i] % MOD
    dp.append(tmp)
    # print(dp)
    ans = (ans + y * tmp * invs[i + 1]) % MOD
    # print(i, y, tmp, tmp * invs[i + 1] * factorials[n - 1] % MOD)
ans = ans * factorials[n - 1] % MOD
print(ans)

Submission status: Wrong Answer

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) % dev
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: Wrong Answer

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

ans = 0

for i in range(n-1):
    ans += (x[n-1]-x[i])*math.factorial(n-1)
    if i >0:
        for j in range(n-2):
            ans -= (j+1)*(x[n-1]-x[i])
    if i > 0 and i < n-2:
        for j in range(n-i-2):
            m =  x[n-1] - x[n-2-j]
            ans -= m

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

Submission status: Wrong Answer

Solution code:
import sys


def prepare(n, MOD):
    f = 1
    factorials = [1]
    for m in range(1, n + 1):
        f *= m
        f %= MOD
        factorials.append(f)
    inv = pow(f, MOD - 2, MOD)
    invs = [1] * (n + 1)
    invs[n] = inv
    for m in range(n, 1, -1):
        inv *= m
        inv %= MOD
        invs[m - 1] = inv

    return factorials, invs


n = int(input())
xxx = list(map(int, input().split()))
MOD = 10 ** 9 + 7
factorials, invs = prepare(n, MOD)

print(factorials)
print(invs)

ans = 0
dp = [0]
for i in range(n - 1):
    y = xxx[i + 1] - xxx[i]
    tmp = 0
    for k in range(i + 1):
        print(' ', k, (dp[k] + factorials[k]) * invs[k])
        tmp = (tmp + (dp[k] + factorials[k]) * invs[k]) % MOD
    tmp = tmp * factorials[i] % MOD
    dp.append(tmp)
    print(dp)
    ans = (ans + y * tmp * invs[i + 1]) % MOD
    print(i, y, tmp, tmp * invs[i + 1] * factorials[n - 1] % MOD)
ans = ans * factorials[n - 1] % MOD
print(ans)

Submission status: Wrong Answer

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) % dev
print(f)
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: Wrong Answer

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

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

def cmb(n, r):
	if n - r < r:
		r = n - r
	if r == 0:
		return 1
	if r == 1:
		return n
	
	num = [n - r + k + 1 for k in range(r)] #numerator(分子)
	den = [k + 1 for k in range(r)] #denominator(分母)
	
	for p in range(2, r + 1):
		pivot = den[p - 1]
		if pivot > 1:
			offset = (n - r) % p
			for k in range(p - 1, r, p):
				num[k - offset] /= pivot
				den[k] /= pivot
	
	result = 1
	for k in range(r):
		if num[k] > 1:
			result *= int(num[k])
	
	return result

for i in range(N - 1):
    count += y[i] * cmb(N - 1, i)
print(count % MOD)

Submission status: Wrong Answer

Solution code:
n=int(input())
if n>2000:exit()
a=list(map(int,input().split()))
b=[0]
for i in range(n-1):b.append(b[-1]+a[i+1]-a[i])
mod=10**9+7
ans=0
c=[1]
for i in range(n-2):
  for j in range(len(c)):
    c[j]*=i+2
    c[j]%=mod
  c.append(i+1)
for i in range(n-1):
  m=c[-1-i]
  for j in range(i,n-1):
    ans+=m*(b[j+1]-b[i])
    ans%=mod
print(ans)

Submission status: Wrong Answer

Solution code:
p = 10**9+7
N = int(input())
X = list(map(int,input().split()))
prod = 1
for i in range(2,N):
    prod = (prod*i)%p
prod2 = 1
for i in range(3,N):
    prod2 = (prod2*i)%p
tot = sum(X)
a = ((N-2)*prod2)%p
a = (a*(tot-X[0]))%p
a = a+(prod*X[-1])%p 
b = ((tot-X[-1])*prod)%p
print((a-b)%p)

Submission status: Wrong Answer

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

ans = 0

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

excnt = [0, 0]
for i in range(2, n):
    ex = cnt // i
    excnt.append(excnt[-1]+ex)

print(excnt)

for i in range(1, n):
    l = X[i] - X[i-1]
    ans += l * (cnt + excnt[i])
    ans = ans % (10**9+7)

print(ans)

Submission status: Wrong Answer

Solution code:
# coding: utf-8
# Your code here!
N=int(input())
SR=list(map(int,input().split()))

l=[1]
for n in range(N-1):
    l.append(l[-1]*(n+2))
#print(l)

ans=0
are=N
for n in range(N-1):
    for m in range(n+1,N):
        ans+=l[are-m+n-1]*(SR[m]-SR[n])
    are-=1
print(ans%(10**9+7))

Submission status: Wrong Answer

Solution code:
#############  B  #############
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)
li = np.array(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)])
d = np.diff(x,n=1)
SUM = np.dot(li,d)
print(SUM)

Submission status: Wrong Answer

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])%MOD
    rr.append(tmp)

k=[]    
f=math.factorial(n-1)%MOD
ss=0.0
for i in range(1,n-1+1):
    ss+=1.0/i
    k.append(ss)
    




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


print(int(sum%MOD))

Submission status: Wrong Answer

Solution code:
from collections import Counter,defaultdict,deque
import sys
import bisect
import math
import itertools
import string
import queue
import copy
from itertools import permutations, combinations
from heapq import heappop, heappush
# input = sys.stdin.readline
sys.setrecursionlimit(10**8)
mod = 10**9+7
 
def inp(): # n=1
    return int(input())
def inpm(): # x=1,y=2
    return map(int,input().split())
def inpl(): # a=[1,2,3,4,5,...,n]
    return list(map(int, input().split()))
def inpls(): # a=['1','2','3',...,'n']
    return list(input().split())
def inplm(n): # x=[] 複数行
    return list(int(input()) for _ in range(n))
def inpll(n): # [[1,1,1,1],[2,2,2,2],[3,3,3,3]]
    return [list(map(int, input().split())) for _ in range(n)]

def main():
    n = inp()
    x = inpl()
    ans = 0
    d = []
    for i in range(n-1):
        d.append(x[i+1]-x[i])
    f = 0
    y = 1
    for i in range(1,n):
        f += (y*i)//mod
        y = (y*i)%mod
    a = y
    for i in range(n-1):
        ans = (ans + d[i]*y)%mod
        alpha = a//(i+2)
        beta = a%(i+2)
        alpha1 = f//(i+2)
        beta1 = f%(i+2)
        alpha2 = mod//(i+2)
        beta2 = mod%(i+2)
        
        plus = (i*alpha1)%mod
        plus = (plus*alpha2)%mod
        plus = (plus + alpha)%mod
        plus = (plus + alpha2*beta1)%mod
        plus = (plus + alpha1*beta2)%mod
        plus = (plus + (beta1*beta2+beta)//(i+2))%mod
        y = (y+plus)%mod
    print(ans)

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

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)
    kai %= mod
for i in range(n-1):
    count += 1/(i+1)
    
    
    ans += sa[i]*count
    ans %= mod
    

print(int(kai*ans%mod))

Submission status: Wrong Answer

Solution code:
import math
n = int(input())
x = list(map(int, input().split()))
r = 10 ** 9 + 7
if n <= 2000:
    p = math.factorial(n - 1)
    ans = 0
    f = x[n - 1]
    for i in range(n-1):
        ans += (f - x[i]) * p
        ans %= r
        p = p * (i + 1) // (i + 2)

else:
    p = 1
    for i in range(1,n):
        p *= i
        p %= r
    ans = 0
    f = x[n - 1]
    for i in range(n-1):
        ans += (f - x[i]) * p
        ans %= r
        p = p * (i + 1) // (i + 2)
    
print(ans)

Submission status: Wrong Answer

Solution code:
# coding: utf-8
# Your code here!

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

D=[]

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

pn=[1]

for i in range(N):
    pn.append(pn[i]*(i+1))


ans=0
for i in range(N-1):
    ans+=(pn[N-1]*(i+1)-pn[N-3]*i*(N-3)*(N-2)//2)*D[i]%MOD

print(ans)
print(pn)

Submission status: Wrong Answer

Solution code:
n = int(input())
x = list(map(int, input().split()))
p = 0
mod = 10**9+7
f = 1
for i in range(1,n):
  f *= (i+1) % mod
  d = x[i] - x[i-1]
  p = (i*p + f*d//2)%mod
print(p%mod)

Submission status: Wrong Answer

Solution code:
def main():
    n = int(input())
    x = list(map(int,input().split()))
    ans = 0
    dv = 10**9+7
    kj = [1,1]
    for i in range(2,n+1):
        kj.append(kj[-1]*i)
    for i in range(n):
        for j in range(n):
            if i > j:
                for k in range(j+1,i):
                    ans += (x[k]-x[j])*kj[i-k-1]*kj[k-j-1]
                    ans = ans % dv
                ans += kj[i-j-1]*(x[i]-x[j])
                ans = ans % dv
    print(ans)
    
if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
n = int(input())
x = list(map(int, input().split()))
p = 0
mod = 10**9+7
f = 1
for i in range(1,n):
  f *= (i+1)
  d = x[i] - x[i-1]
  p = i*p + f*d//2
print(p%mod)

Submission status: Wrong Answer

Solution code:
MOD = 10 ** 9 + 7


def modFact(n):
    global MOD
    if n == 1:
        return 1
    else:
        return (n * modFact(n - 1)) % MOD


def prepare(n):
    global MOD
    modFacts = [0] * (n + 1)
    modFacts[0] = 1
    for i in range(n):
        modFacts[i + 1] = (modFacts[i] * (i + 1)) % MOD

    invs = [1] * (n + 1)
    invs[n] = pow(modFacts[n], MOD - 2, MOD)
    for i in range(n, 1, -1):
        invs[i - 1] = (invs[i] * i) % MOD

    return modFacts, invs


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

Y = [0] * (N - 1)
for x in X:
    Y[0] += x
    Y[0] %= MOD
Y[0] -= X[0] * N
for i in range(1, N - 1):
    offset = (X[i] - X[i - 1])
    num = N - i
    Y[i] = Y[i - 1] - offset * num
    Y[i] %= MOD

modFacts, invs = prepare(N)

ans = 0
var = modFacts[N - 1]
for i in range(N - 1):
    j = i + 1
    b = N - j
    ans += Y[i] * var // b
    ans %= MOD

ans = ans * (invs[N - 1] * modFacts[N - 1]) % MOD

print(ans)

Submission status: Wrong Answer

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)
li = np.array(range(low, high ,dif))
d = np.array([x[i+1]-x[i] for i in range(N-1)])
SUM = np.dot(li,d)
print(SUM)

Submission status: Wrong Answer

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

ans = 0

ans = [0] * N
for x in range(N - 2, -1, -1):
    for y in range(N - 1, x, -1):
        #print(x, y)
        ans[x] = (ans[x] + X[y] - X[x] + ans[y]) % (10**9 + 7)
        #print(ans)
        
print(sum(ans) % (10**9 + 7))

Submission status: Wrong Answer

Solution code:
import random
import math
# スライム数
n = int(input())

x = list(map(int, input().split()))
distance = 0

for i in range(n-1):
    k = random.randint(1, n-i-1)
    distance += x[k] - x[k-1]
    x[k-1] = x[k]
    for j in range(k, n-1):
        x[j] = x[j+1]

nfactorial = math.factorial(n-2)
nsum = int(math.pow(10, 9)) + 7
nresult = nfactorial % nsum

print(nresult)

Submission status: Wrong Answer

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

i=N-1
a=1
while i!=0:
  a*=i
  a%=mod
  i-=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: Wrong Answer

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

ans = 0

cnt = 1
for i in range(1, n):
    cnt = cnt*i % (10**9+7)

excnt = [0, 0]
for i in range(2, n):
    ex = cnt // i
    excnt.append(excnt[-1]+ex)

for i in range(1, n):
    l = X[i] - X[i-1]
    ans += l * (cnt + excnt[i])
    ans = ans % (10**9+7)

print(ans)

Submission status: Wrong Answer

Solution code:
N = int(input())
X = list(map(int,input().split()))
S = []
dp = [1]
mod = 10**9+7
for i in range(N):
    t = i+1
    dp.append(dp[i]*t%mod)
ans = 0
for i in range(N-1):
    x = X[i]
    S.append(X[N-1]-x)
ans = 0
for i in range(N-1):
    x = S[i]
    t = i + 1
    ans += dp[N-1]//t*x%mod
print(ans%mod)