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:
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)
  b%=mod
  ans+=b*(X[i+1]-X[i])
  ans%=mod
  
print(int(ans))

Submission status: Wrong Answer

Solution code:
import numpy as np
ans=0
n=int(input())
kai=1
war=10**9+7
for i in range(1,n):
  kai=kai*i
  kai=kai%war
s=list(map(int,input().split()))
s.reverse()
a=s[-1]
l=[i-a for i in s]
b=np.array(l)
for i in range(1,n):
  ans+=((np.sum(b)*kai)/(n-i))%war
  b=np.delete(b,-1)
  b-=(s[n-i-1]-s[n-i])
print(int(ans%war))

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 += f*(i-1)+(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)//n)%mod
        y = (y+plus)%mod
    print(ans)

if __name__ == "__main__":
    main()

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)])
#print(d)
SUM = np.dot(li,d)
print(SUM)

Submission status: Wrong Answer

Solution code:
def factorial_cor(n):
    fact = 1
    for integer in range(1, n + 1):
        fact *= integer
    return fact
n = int(input())
X = list(map(int, input().split()))
sub = []
for i in range(1, n):
  sub.append(X[i]-X[i-1])
times = [factorial_cor(n-1)]
for i in range(2, n):
  a = 0
  for j in range(n-i):
    a += factorial_cor(j)
  times.append(times[i-2]+a)
ans = 0
ans = sum(sub[k]*times[k]%(1000000007) for k in range(n-1))
print(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)

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

print(ans)

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(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:
import numpy as np
N = int(input())
x = list(map(int, input().split()))

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

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

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)//n)%mod
        y = (y+plus)%mod
    print(ans)

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

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()))) % MOD
L = np.cumsum(np.array([1 / x for x in range(1, n)])) % MOD
n_1 = factorial(n-1) % MOD
 
# print(int(np.sum(L * dist * n_1)) % MOD)
# ans = 0
# for l, d in zip(L, dist):
#     ans += ((l % MOD) * (d % MOD) * (n_1 % MOD)) % MOD
# print(int(ans) % MOD)
print(int(np.sum(dist * L * (n_1)) % MOD))

Submission status: Wrong Answer

Solution code:
N, *X = map(int, open(0).read().split())

D = [X[i + 1] - X[i] for i in range(N - 1)]
S = [0] * N
for i in range(N - 1):
    S[i + 1] = S[i] + D[N - i - 2]
S = S[::-1]

MOD = 10 ** 9 + 7
ans = [0] * (N - 1)
for c, i in enumerate(reversed(range(N - 1))):
    for j in range(i + 1, N):
        ans[c] += S[i] - S[j]

for i in range(N - 1):
    for j in range(N - 1):
        if i == j:
            continue
        ans[i] *= (j + 1)

print(sum(ans) % 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
for i in range(N - 1):
    j = i + 1
    ans += Y[i] * modFacts[N - 1] // (N - j)
    ans %= MOD

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
    s,a = f,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 + beta)%mod
        plus = (plus + (alpha1*alpha2+beta)//n)%mod
        y = (y+plus)%mod
    print(ans)

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

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 )

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
        
L2 = [0]*(N-1)
for i in range(N-1):
    L2[i] = A[i+1]-A[i]

S = [0]*N
for i in range(N-1):
    S[i+1] = S[i] + L2[i]
    
L3 = [0]*N
for i in range(1, N):
    if N-i-1 >= i:
        L3[i] = L3[i-1] + S[N-i-1] - S[i-1]
    elif N%2 == 0:
        L3[i] = L3[N-i-1]
    else:
        L3[i] = L3[N-i]

for t in range(1, N-1):
    ans += cmb(N-1, t+1, mod)*L[N-t-2]*L[t-1]*L3[t]
    ans %= mod
        
print(ans)

Submission status: Wrong Answer

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

upp=[1]*(N+1)
for i in range(2, N+1):
  upp[i]=upp[i-1]*i
  upp[i]%=MOD
  
inv=[1]*(N+1)
inv[-1]=pow(upp[-1], MOD-2, MOD)
for i in range(N, 1, -1):
  inv[i-1]=inv[i]*i%MOD

#a=[0]*(N-1)
#a[0]=upp[N-1]
#for i in range(1, N-1):
#  a[i]=a[0]*inv[i+1]
#  a[i]%=MOD

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

b=[0]*(N-1)
b[0]=a[0]
for i in range(1, N-1):
  b[i]=b[i-1]+a[i]
  b[i]%=MOD
  
def moddot(v1, v2):
  n=len(v1)
  out=0
  for i in range(n):
    out+=v1[i]*v2[i]%MOD
    out%=MOD
  return out

ex=moddot(b, y)
print(ex)

Submission status: Wrong Answer

Solution code:
#coding UTF-8
N=int(input())
X=list(map(int,input().split()))
L=[0]*(N-1)
for i in range(N-1):
    L[i]=X[i+1]-X[i]
#print(L)

#累積和
W=[0]*(N-1)
W[0]=L[0]
#print(W)
R=[0]*(N-1)
R[0]=1
Seki=[0]*(N-1)
Seki[0]=1
for i in range(N-2):
    W[i+1]=L[i]+L[i+1]
    R[i+1]=R[i]+1
    Seki[i+1]=Seki[i]*(i+1)
ans=0
#print(R)
for i in range(N-1):
    ans+=W[i]*R[i]*Seki[i]
print(ans%(10**9+7))

Submission status: Wrong Answer

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) % MOD
    def calc(k,slimes):
        r = 0

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




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

print(int(kai*ans%mod))

Submission status: Wrong Answer

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

modn = 1
for i in range(1,n):
    modn *= i
    modn %= mod

ans = 0

for j in range(n-1):
    for i in range(j+1,n-1):
        ans+=(x[i]-x[j])*modn/((i-j)*(i-j+1))
        ans%=mod
    ans+=(x[n-1]-x[j])*modn/(n-1-j)
    ans%=mod
print(int(ans))

Submission status: Wrong Answer

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 )

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
        
L2 = [0]*(N-1)
for i in range(N-1):
    L2[i] = A[i+1]-A[i]

S = [0]*N
for i in range(N-1):
    S[i+1] = S[i] + L2[i]
    
L3 = [0]*N
for i in range(1, N):
    if N-i-1 >= i:
        L3[i] = L3[i-1] + S[N-i-1] - S[i-1]
    elif N%2 == 0:
        L3[i] = L3[N-i-1]
    else:
        L3[i] = L3[N-i]

for t in range(1, N):
    ans += cmb(N-1, t+1, mod)*L[N-t-2]*L[t-1]*L3[t]
    ans %= mod
        
print(ans)

Submission status: Wrong Answer

Solution code:
#coding UTF-8
N=int(input())
X=list(map(int,input().split()))
L=[0]*(N-1)
for i in range(N-1):
    L[i]=X[i+1]-X[i]
#print(L)

#累積和
W=[0]*(N-1)
W[0]=L[0]
for i in range(N-2):
    W[i+1]=L[i]+L[i+1]
#print(W)
R=[0]*(N-1)
R[0]=1
for i in range(N-2):
    R[i+1]=R[i]+1
ans=0
Seki=[0]*(N-1)
Seki[0]=1
for i in range(N-2):
    Seki[i+1]=Seki[i]*(i+1)
#print(R)
for i in range(N-1):
    ans+=W[i]*R[i]*Seki[i]
print(ans%(10**9+7))

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 "i,j,l,X[i],X[j] :: ",i,j,l,X[i],X[j]
        #print "(X[j]-X[i]),ncr(N-1,l),(X[j]-X[i])*ncr(N-1,l) :: ",(X[j]-X[i]),ncr(N-1,l),(X[j]-X[i])*ncr(N-1,l)
        ans+=(X[j]-X[i])*ncr(N-1,l)

print ans%mod

Submission status: Wrong Answer

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

facN_1=1
inv=[1]*N
for i in range(2,N):
    facN_1=facN_1*i%MOD
    inv[i]=MOD-inv[MOD%i]*(MOD//i)%MOD

ans=0
for i in range(N-1):
    tmp=X[i+1]-X[i]
    tmp*=(i+1)*(i+2)//2
    tmp*=facN_1*inv[i+1]%MOD
    ans+=tmp
    ans%=MOD
print(ans)

Submission status: Wrong Answer

Solution code:
import math

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

mod = 10**9 + 7

f = math.factorial(n-1)
f %= mod

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

# print(l)

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

print(int(count)%mod)

Submission status: Wrong Answer

Solution code:
import math
def makeInt(n):
    return int(n)

N = int(input())
line = list(map(makeInt,list(input().split())))

newList = []
for i in range(len(line) - 1):
    newList.append(line[i+1]-line[i])

ans = sum(newList)*len(newList)+sum(newList[1:len(newList)])
fact = math.factorial(N - 2)
answer = ans*fact

if(answer < 10**9):
    print(answer)
else:
    print(answer % (10**9+7))

Submission status: Wrong Answer

Solution code:
import sys
n=int(input())
X=[int(i) for i in input().split()]
mod=10**9+7
n_max=n
if n >2000:
  sys.exit()
n_max=2*10**5
F,FI=[0]*(n_max+1),[0]*(n_max+1)
F[0],FI[0]=1,1
for i in range(n_max):
  F[i+1]=(F[i]*(i+1))%mod
  FI[i+1]=pow(F[i+1],mod-2,mod)
def comb(x,y):
  return (F[x]*FI[x-y]*FI[y])%mod

ans=0
DP=[[0]*n for _ in range(n)]
for i in range(1,n):
  inc=0
  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])%mod
    inc=(inc+j*DP[i][j])%mod
  ans=(ans+inc*(X[i]-X[i-1])*F[n-1]*FI[i])%mod
print(ans)

Submission status: Wrong Answer

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 )

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
        
L2 = [0]*(N-1)
for i in range(N-1):
    L2[i] = A[i+1]-A[i]

S = [0]*N
for i in range(N-1):
    S[i+1] = S[i] + L2[i]
    
L3 = [0]*N
for i in range(1, N):
    if N-i-1 >= i:
        L3[i] = L3[i-1] + S[N-i-1] - S[i-1]
    elif N%2 == 0 and N-2*i-1 == -1:
        L3[i] = L3[i-1]
    elif N%2 == 0:
        L3[i] = L3[N-i-1]
    else:
        L3[i] = L3[N-i]

for t in range(1, N):
    ans += cmb(N-1, t+1, mod)*L[N-t-2]*L[t-1]*L3[t]
    ans %= mod
        
print(ans)

Submission status: Wrong Answer

Solution code:
import math
import copy
import random

N = int(input())
x_true = list(map(int, input().split()))
test = 10**4
tes = 0

for k in range(test):
  distance = 0
  x = copy.deepcopy(x_true)

  for i in range(1, N):
    pos = random.randint(1, N-i) -1
    a = x.pop(pos)
    distance += x[pos]-a
  
  tes += distance
  distance = 0
  
print(int((round(tes/test, 1) * math.factorial(N-1))%(10**9+7)))

Submission status: Wrong Answer

Solution code:
mod = 10**9+7

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

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

aa = [a]
for i in range(N-2):
    aa.append((aa[-1]+a//(i+2))%mod)


dx = [xx[i]-xx[i-1] for i in range(1,N)]

ans = 0
for a,d in zip(aa,dx):
    ans += a*d
    ans %= mod

print(ans%mod)

Submission status: Wrong Answer

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

modn = 1
modls = list()
for i in range(1,n):
    modn *= i
    modn %= mod
    modls.append(modn)

ans = 0

for j in range(n-1):
    for i in range(j+1,n-1):
        ans+=(x[i]-x[j])/((i-j)*(i-j+1))*modn
        ans%=mod
    ans+=(x[n-1]-x[j])/(n-1-j)*modn
    ans%=mod
print(int(ans))

Submission status: Wrong Answer

Solution code:
import sys
n=int(input())
X=[int(i) for i in input().split()]
mod=10**9+7
n_max=n
if n >2000:
  sys.exit()
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):
  inc=0
  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])%mod
    inc=(inc+j*DP[i][j])%mod
  ans=(ans+inc*(X[i]-X[i-1])*F[n-1]//F[i])%mod
print(ans)

Submission status: Wrong Answer

Solution code:
import numpy as np


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


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


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

mod = 10**9 + 7

v = 0
for i in range(N - 1):
    d = x[i + 1:] - x[i]
    v += d.sum() / d.shape[0]
fact, _ = make_fact(N, mod)
print(int(v * fact[-1]) % mod)

Submission status: Wrong Answer

Solution code:
import math

dev = 10**9+7

def kai(N):
  a = 1
  for i in range(1,N+1):
    a = a*i % dev
  return a




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

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

MOD = 10 ** 9 + 7

dif = [j - i for i, j in zip(st, st[1:])]

d = 1
for i in range(1, n):
  d = (d * i) % MOD  

li = []
ho = 0
for i in range(1, n):
  ho += d // i
  li.append(ho)

ans = 0
for i, j in zip(dif, li):
  ans += (i * j) % MOD

print(ans % MOD)

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

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

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

t += l * F

for i in range(0, N-1):
    x = X[i+1] - X[i]
    t += x * F * i

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

Submission status: Wrong Answer

Solution code:
N = int(input())
semi = 10**9+7
x = [int(num) for num in input().split()]
ans = 0
wa=[]
c=0
for i in range(N):
    c += x[N-i-1]
    wa.append(c)
#print(wa)

ans = 0
step = 1
for i in range(1,N):
    ans = (ans*(i) +wa[i-1]*step)#%semi
    step *= i
 #   print(ans)

ans -=sum(x[0:N-1])*step
ans%=semi
print(ans)

Submission status: Wrong Answer

Solution code:
from statistics import mean
from math import factorial
import numpy as np
N = int(input())
H = list(map(int, input().split()))
MOD = 1000000000+7
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))) for i in range(1,N)])
# print(multi)
# print(diff)
print(int(np.sum(diff*multi)*(factorial(N-1)%MOD)%MOD))

Submission status: Wrong Answer

Solution code:
N = int(input())
semi = 10**9+7
x = [int(num) for num in input().split()]
ans = 0
wa=[]
c=0
for i in range(N):
    c += x[N-i-1]
    wa.append(c)
print(wa)

ans = 0
step = 1
for i in range(1,N):
    ans = (ans*(i) +wa[i-1]*step)#%semi
    step *= i
    print(ans)

ans -=sum(x[0:N-1])*step
ans%=semi
print(ans)

Submission status: Wrong Answer

Solution code:
#coding UTF-8
N=int(input())
X=list(map(int,input().split()))
L=[0]*(N-1)
for i in range(N-1):
    L[i]=X[i+1]-X[i]
#print(L)

#累積和
W=[0]*(N-1)
W[0]=L[0]
for i in range(N-2):
    W[i+1]=L[i]+L[i+1]
#print(W)
R=[0]*(N-1)
R[0]=1
for i in range(N-2):
    R[i+1]=R[i]+1
ans=0
#print(R)
for i in range(N-1):
    ans+=W[i]*R[i]
print(ans%(10**9+7))

Submission status: Wrong Answer

Solution code:
from math import factorial

N = int(input())
x = list(map(int, input().split()))
mod = 10**9 + 7
x_dis = [x[i+1] - x[i] for i in range(N-1)]
ans = 0
m = N-1
fac = factorial(m) % mod
tmp = fac
for i in range(m):
    ans += x_dis[i] * tmp
    ans %= mod
    tmp += fac//(i+2)
print(ans)

Submission status: Wrong Answer

Solution code:
from collections import defaultdict,deque
import sys,heapq,bisect,math,itertools,string,queue,copy,time
sys.setrecursionlimit(10**8)
INF = float('inf')
mod = 10**9+7
eps = 10**-7
def inp(): return int(sys.stdin.readline())
def inpl(): return list(map(int, sys.stdin.readline().split()))
def inpl_str(): return list(sys.stdin.readline().split())

N = inp()
xx = inpl()

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

aa = [a]
for i in range(N-2):
    aa.append((aa[-1]+a//(i+2))%mod)


dx = [xx[i]-xx[i-1] for i in range(1,N)]

ans = 0
for a,d in zip(aa,dx):
    ans += a*d
    ans %= mod

print(ans%mod)

Submission status: Wrong Answer

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
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 // (i + 2)
    base % mod
print(ans)

Submission status: Wrong Answer

Solution code:
import sys
n=int(input())
X=[int(i) for i in input().split()]
mod=10**9+7
n_max=n
if n >2000:
  sys.exit()
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])%mod
    ans=(ans+j*DP[i][j]*(X[i]-X[i-1])*F[n-1]//F[i])%mod
print(ans)

Submission status: Wrong Answer

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

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

P=[0]*N
for i in range(1,N):
    P[i]=facN_1//i

ans=0
for i in range(N-1):
    tmp=X[i+1]-X[i]
    tmp*=(i+1)*(i+2)//2
    tmp*=P[i+1]%MOD
    ans+=tmp
    ans%=MOD
print(ans)

Submission status: Wrong Answer

Solution code:
from collections import defaultdict,deque
import sys,heapq,bisect,math,itertools,string,queue,copy,time
sys.setrecursionlimit(10**8)
INF = float('inf')
mod = 10**9+7
eps = 10**-7
def inp(): return int(sys.stdin.readline())
def inpl(): return list(map(int, sys.stdin.readline().split()))
def inpl_str(): return list(sys.stdin.readline().split())

# In[]:
N = inp()
xx = inpl()

# In[]:
a = 1
for i in range(2,N):
    a *= i
    a %= mod

aa = [a]
for i in range(N-2):
    aa.append((aa[-1]+a//(i+2))%mod)

# print(aa)


# In[]:
dx = [xx[i]-xx[i-1] for i in range(1,N)]

# In[]:
ans = 0
for a,d in zip(aa,dx):
    ans += a*d

print(ans%mod)

Submission status: Wrong Answer

Solution code:
import sys
def input():
    return sys.stdin.readline()[:-1]
pr=10**9+7
N=int(input())
x = tuple(map(int, input().split()))
s=1
for i in range(2,N):
    s=s*i%pr
l=[None]*(N-1)
t=s
for i in range(N-1):
    l[i]=t
    s=s*(i+1)//(i+2)%pr
    t=(t+s)%pr
a=0
for i in range(N-1):
    a+=(x[i+1]-x[i])*l[i]%pr
print(a%pr)

Submission status: Wrong Answer

Solution code:
import math
import random
from operator import itemgetter
import re
import sys
from itertools import accumulate
from collections import defaultdict
from collections import deque
from bisect import bisect_left,bisect
from heapq import heappop,heappush
from fractions import gcd
from copy import deepcopy
import functools
N = int(input())
x = [int(i) for i in input().split()]
mod = 10**9+7
start = 1
st = 1
st_list = [0] * N
for i in range(N-1):
    st *= (i+1)
    st %= mod
    st_list[i] = st
all_dis = 0
for i in range(N-1):
    far = x[i+1]-x[i]
    all_dis += far*start*st_list[N-2]//st_list[i]
    start = (start*(i+2)+st_list[i])%mod
    all_dis %= mod
print(all_dis)

Submission status: Wrong Answer

Solution code:
MOD = 10**9+7         # 問題に合わせて適宜書き換え
list_size = 252525    # 問題に合わせて適宜書き換え

f_list = [1] * list_size
f_r_list = [1] * list_size

for i in range(list_size - 1):
	f_list[i + 1] = ((f_list[i] * (i+2)) % MOD)

f_r_list[-1] = pow(f_list[-1], MOD - 2, MOD)

for i in range(-2, -list_size-1, -1):
	f_r_list[i] = ((f_r_list[i+1] * (list_size + 2 + i)) % MOD)

N = int(input())
if N > 2525:
    print(-1)
    exit()
X = list(map(int,input().split()))
# print(f_list[N-1],f_r_list[N-1],(f_list[N-1]*f_r_list[N-1])%MOD)
ans = 0
for i in range(N):
    hoge = f_list[N-2]
    for j in range(i+1,N-1):
        piyo= (hoge*pow(j-i+1,MOD-2,MOD))%MOD
        hoge -= piyo
        ans += (X[j]-X[i])*piyo

        ans %= MOD
    ans += (X[-1]-X[i])*hoge
    ans %= MOD
print(ans)

Submission status: Wrong Answer

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) % MOD

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

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

# print(int(ans) % MOD)

L = np.cumsum(np.array([1 / x for x in range(1, n)]))
n_1 = factorial(n-1) % MOD
L *= n_1
 
print(int(np.sum(L * (dist % MOD)) % MOD))