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:
#!/usr/bin/env python3

import sys
from typing import Any, Callable, Deque, Dict, List, Mapping, Optional, Sequence, Set, Tuple, TypeVar, Union
# import time
# import math, cmath
# import numpy as np
# import scipy.sparse.csgraph as cs            # csgraph_from_dense(ndarray, null_value=inf), bellman_ford(G, return_predecessors=True), dijkstra, floyd_warshall
# import random                                # random, uniform, randint, randrange, shuffle, sample
# import string                                # ascii_lowercase, ascii_uppercase, ascii_letters, digits, hexdigits
# import re                                    # re.compile(pattern) => ptn obj; p.search(s), p.match(s), p.finditer(s) => match obj; p.sub(after, s)
# from bisect import bisect_left, bisect_right # bisect_left(a, x, lo=0, hi=len(a)) returns i such that all(val<x for val in a[lo:i]) and all(val>-=x for val in a[i:hi]).
# from collections import deque                # deque class. deque(L): dq.append(x), dq.appendleft(x), dq.pop(), dq.popleft(), dq.rotate()
# from collections import defaultdict          # subclass of dict. defaultdict(facroty)
# from collections import Counter              # subclass of dict. Counter(iter): c.elements(), c.most_common(n), c.subtract(iter)
# from datetime import date, datetime          # date.today(), date(year,month,day) => date obj; datetime.now(), datetime(year,month,day,hour,second,microsecond) => datetime obj; subtraction => timedelta obj
# from datetime.datetime import strptime       # strptime('2019/01/01 10:05:20', '%Y/%m/%d/ %H:%M:%S') returns datetime obj
# from datetime import timedelta               # td.days, td.seconds, td.microseconds, td.total_seconds(). abs function is also available.
# from copy import copy, deepcopy              # use deepcopy to copy multi-dimentional matrix without reference
# from functools import reduce                 # reduce(f, iter[, init])
# from functools import lru_cache              # @lrucache ...arguments of functions should be able to be keys of dict (e.g. list is not allowed)
# from heapq import heapify, heappush, heappop # built-in list. heapify(L) changes list in-place to min-heap in O(n), heappush(heapL, x) and heappop(heapL) in O(lgn).
# from heapq import _heapify_max, _heappop_max, _siftdown_max
# from heapq import nlargest, nsmallest        # nlargest(n, iter[, key]) returns k-largest-list in O(n+klgn).
# from itertools import count, cycle, repeat   # count(start[,step]), cycle(iter), repeat(elm[,n])
# from itertools import groupby                # [(k, list(g)) for k, g in groupby('000112')] returns [('0',['0','0','0']), ('1',['1','1']), ('2',['2'])]
# from itertools import starmap                # starmap(pow, [[2,5], [3,2]]) returns [32, 9]
# from itertools import product, permutations  # product(iter, repeat=n), permutations(iter[,r])
# from itertools import combinations, combinations_with_replacement
from itertools import accumulate             # accumulate(iter[, f])
# from operator import itemgetter              # itemgetter(1), itemgetter('key')
# from fractions import Fraction               # Fraction(a, b) => a / b ∈ Q. note: Fraction(0.1) do not returns Fraciton(1, 10). Fraction('0.1') returns Fraction(1, 10)



def main():
    Num = Union[int, float]
    mod = 1000000007                # 10^9+7
    inf = float('inf')              # sys.float_info.max = 1.79e+308
    # inf = 2 ** 63 - 1             # (for fast JIT compile in PyPy) 9.22e+18
    sys.setrecursionlimit(10**6)    # 1000 -> 1000000
    def input(): return sys.stdin.readline().rstrip()
    def ii():    return int(input())
    def isp():   return input().split()
    def mi():    return map(int, input().split())
    def mi_0():  return map(lambda x: int(x)-1, input().split())
    def lmi():   return list(map(int, input().split()))
    def lmi_0(): return list(map(lambda x: int(x)-1, input().split()))
    def li():    return list(input())
    # def _heappush_max(h, item): h.append(item); _siftdown_max(h, 0, len(h)-1)


    class Combinatorics:
        def __init__(self, size: int, mod: int):
            '''
            >>> c = Combinatorics(size=10, mod=10**9+7)
            >>> c.fact
            [1, 1, 2, 6, 24, 120, 720, 5040, 40320, 362880, 3628800]
            >>> c.inv
            [1, 1, 500000004, 166666668, 41666667, 808333339, 301388891, 900198419, 487524805, 831947206, 283194722]
            '''
            self.size =  size
            self.mod = mod
            self.fact = self._build_factorial(size, mod)
            self.inv = self._build_inv_factorial(size, mod)
        
        def _build_factorial(self, size:int , mod: int) -> None:
            '''
            fact_mod[i] は i! % mod を表すとする
            O(n) で fact_mod[size] まで計算結果がメモされたテーブルを構築して返す
            '''
            fact = [1] * (size + 1)
            for i in range(1, size + 1):
                fact[i] = (fact[i-1] * i) % mod
            return fact
        
        def _build_inv_factorial(self, size: int, mod: int) -> None:
            '''
            inv_fact_mod[i] は 1/i! % mod を表すとする
            O(n+lgm) で inv_fact_mod[size] まで計算結果がメモされたテーブルを構築して返す
            Note:
                [フェルマーの小定理]
                a ^ p-1 ≡ 1 (mod p)
                a ^ p-2 ≡ 1/a (mod p) (逆元)
                nCr = (n!) / ((n-r)! * r!) だが、mod p の世界ではこの分母を逆元を用いて計算しておくことが可能
            '''
            inv_fact_mod = [1] * (size + 1)
            inv_fact_mod[size] = pow(self.fact[size], mod-2, mod)    # a ^ p-2 ≡ 1/a (mod p) において a = n! とする。 1/n! (mod p) を求める
            for i in range(size-1, -1, -1):
                inv_fact_mod[i] = (inv_fact_mod[i+1] * (i+1)) % mod    # 1/(n-1)! = 1/n! * n
            return inv_fact_mod
        
        def comb(self, n: int, r: int) -> int:
            '''
            nCr % mod を O(1) で計算する。
            Examples:
                >>> c = Combinatorics(size=100, mod=10**9+7)
                >>> c.comb(100, 50)
                538992043   
            '''
            numerator = self.fact[n]
            denominator = (self.inv[n-r] * self.inv[r]) % self.mod
            return (numerator * denominator) % self.mod
    

    n = ii()
    L = lmi()¥

    c = Combinatorics(10**5, mod)
    seq = [(c.fact[n-1] * pow(i, mod-2, mod)) % mod for i in range(1, n)]
    ac = list(accumulate(seq))

    ans = 0
    for i in range(n-1):
        tmp = (L[i+1] - L[i]) * ac[i] % mod
        ans = (ans + tmp) % mod
    print(ans)


if __name__ == "__main__":
    main()

Submission status: Runtime Error

Solution code:
mod=10**9+7
n=int(input())
arr=list(map(int,input().split()))
acum=[1]
for i in range(2,n+1):
  tmp=acum+pow(i,mod-2,mod)
  tmp%=mod
  acum.append(tmp)
ans=0
for i in range(n-1):
  ans+=(arr[i+1]-arr[i])*acum[i]
  ans%=mod
for i in range(1,n):
  ans*=i
  ans%=mod
print(ans)

Submission status: Runtime Error

Solution code:
import sys

sys.setrecursionlimit(100000)
input = lambda : sys.stdin.readline().rstrip()

def pow_mod(x, y, mod):
    tmp = x
    ret = 1
    while y > 0:
        if y % 2 == 1:
            ret = ret * tmp % mod
        tmp = tmp * tmp % mod
        y //= 2
    return ret

def inverse(x, mod):
    return pow_mod(x, mod - 2, mod)

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

MOD = 1000000007

def factorial(x):
    if x <= 1:
        return 1
    else:
        return x * factorial(x - 1) % MOD

f = factorial(N - 1)

h = [0]
for _ in range(N):
    h.append((h[-1] + f * inverse(len(h), MOD)) % MOD)

ans = 0
for i in range(N - 1):
    ans += h[i + 1] * (x[i + 1] - x[i])
    ans %= MOD

print(ans)

Submission status: Runtime Error

Solution code:
import sys

input = lambda : sys.stdin.readline().rstrip()

def pow_mod(x, y, mod):
    tmp = x
    ret = 1
    while y > 0:
        if y % 2 == 1:
            ret = ret * tmp % mod
        tmp = tmp * tmp % mod
        y //= 2
    return ret

def inverse(x, mod):
    return pow_mod(x, mod - 2, mod)

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

MOD = 1000000007

def factorial(x):
    if x <= 1:
        return 1
    else:
        return x * factorial(x - 1) % MOD

f = factorial(N - 1)

h = [0]
for _ in range(N):
    h.append((h[-1] + f * inverse(len(h), MOD)) % MOD)

ans = 0
for i in range(N - 1):
    ans += h[i + 1] * (x[i + 1] - x[i])
    ans %= MOD

print(ans)

Submission status: Runtime Error

Solution code:
n = int(input())
testimony_table = [[] for _ in range(n)]

for i_witness in range(n):
    ai = int(input())
    for _ in range(ai):
        xij, yij= map(int, input().split())
        testimony_table[i_witness].append((xij-1,yij))

ans = 0
for i in range(2**n):
    isConflict = False

    for j_witness in range(n): # それぞれの桁について0か1かを判断したいので
        if (i >> j_witness) & 1 == 0: # j桁目が1かどうかをジャッジ
            continue


        for x_suspect, y_testimony in testimony_table[j_witness]:
            if ((i >> x_suspect) & 1) != y_testimony:
                isConflict = True
                break

        if isConflict:
            break
    if isConflict == False:
        ans = max(ans, bin(i).count("1"))

print(ans)

Submission status: Runtime Error

Solution code:
def egcd(a, b):
    (x, lastx) = (0, 1)
    (y, lasty) = (1, 0)
    while b != 0:
        q = a // b
        (a, b) = (b, a % b)
        (x, lastx) = (lastx - q * x, x)
        (y, lasty) = (lasty - q * y, y)
    return (lastx, lasty, a)
def modinv(a, m):
    (inv, q, gcd_val) = egcd(a, m)
    return inv % m

n = int(input())
slime = [int(x) for x in input().split()]

md = 10 ** 9 + 7
fct = math.factorial(n - 1) % md
ans = 0
fr = Frc(1, 1)
for i in range(n - 1):
    c = fr.numerator
    p = fr.denominator
    ans = (ans + fct * modinv(p, md) * c % md * (slime[i + 1] - slime[i]) % md) % md
    fr += Frc(1, i + 2)
print(ans)

Submission status: Runtime Error

Solution code:
from fractions import Fraction as Frc
import math

n = int(input())
slime = [int(x) for x in input().split()]

md = 10 ** 9 + 7
fct = math.factorial(n - 1)
ans = 0
fr = Frc(1, 1)
for i in range(n - 1):
    c = fr.numerator
    p = fr.denominator
    ans = (ans + int(fct / p) % md * c % md * (slime[i + 1] - slime[i]) % md) % md
    fr += Frc(1, i + 2)
print(ans)

Submission status: Runtime Error

Solution code:
    from fractions import Fraction as Frc
    import math
     
    n = int(input())
    slime = [int(x) for x in input().split()]
     
    md = 10**9+7
    fct = math.factorial(n-1) % md
    ans = 0
    fr = Frc(1, 1)
     
    for i in range(n-1):
        ans = (ans + fct * (slime[i+1] - slime[i]) * fr) % md
        fr += Frc(1, i + 2)
    print(ans)

Submission status: Runtime Error

Solution code:
import sys
from itertools import count, accumulate
from operator import mul
from functools import reduce
lines = sys.stdin.readlines()

positions = [int(i) for i in lines[1].split(' ')]

distances = [ j - i for i, j in zip(positions, positions[1:])]

n = len(distances)
factorials = [1] + list(accumulate(range(1,n + 1), mul))

combinations = dict()
def combination(i, j):
    j = min(j, i - j)
    if (i, j) in combinations:
        return combinations((i, j))
    else:
        return factorials[i] // (factorials [j] * factorials[i - j])

permutations = dict()
def permutation(i, j):
    if (i, j) in permutations:
        return permutations((i, j))
    else:
        return reduce(mul, range(j, i+1))

s = 0
result = 0
for d, i in zip(distances, count(1)):
    lt = i - 1
    gt = n - i
    t = factorials[n] / i
    s += t
    result += d * s

print(int(result % (pow(10,9) + 7)))

Submission status: Runtime Error

Solution code:
import math
n = int(input())
slime = [int(x) for x in input().split()]
fct = math.factorial(n-1)
aa = 0
for a in range(n-1):
    for b in range(a+1,n):
        buf_c = b - a - 1
        buf_p = b - a + 1
        if b == n - 1:
            buf_p -= 1
        if buf_c == 0:
            buf_c = 1
        buf_sum = fct * math.factorial(buf_c) / math.factorial(buf_p)
        aa += buf_sum * (slime[b] - slime[a])

print(int(aa % 1000000007))

Submission status: Runtime Error

Solution code:
import math
n = int(input())
slime = [int(x) for x in input().split()]
fct = math.factorial(n-1)
aa = 0
for a in range(n-1):
    for b in range(a+1,n):
        buf_c = b - a - 1
        buf_p = b - a + 1
        if b == n - 1:
            buf_p -= 1
        if buf_c == 0:
            buf_c = 1
        buf_sum = fct * math.factorial(buf_c) / math.factorial(buf_p)
        aa += buf_sum * (slime[b] - slime[a])
        aa %= 1000000007

print(int(aa))

Submission status: Runtime Error

Solution code:
n = int(input())
slime = [int(x) for x in input().split()]
fct = math.factorial(n-1)
aa = 0
for a in range(n-1):
    for b in range(a+1,n):
        buf_c = b - a - 1
        buf_p = b - a + 1
        if b == n - 1:
            buf_p -= 1
        if buf_c == 0:
            buf_c = 1
        buf_sum = fct * math.factorial(buf_c) / math.factorial(buf_p)
        aa += buf_sum * (slime[b] - slime[a])

print(int(aa % 1000000007))

Submission status: Runtime Error

Solution code:
import sys
from itertools import count, accumulate
from operator import mul
from functools import reduce
lines = sys.stdin.readlines()

positions = [int(i) for i in lines[1].split(' ')]

distances = [ j - i for i, j in zip(positions, positions[1:])]

n = len(distances)
factorials = [1] + list(accumulate(range(1,n + 1), mul))

combinations = dict()
def combination(i, j):
    if (i, j) in combinations:
        return combinations((i, j))
    else:
        return factorials[i] / (factorials [j] * factorials[i - j])

permutations = dict()
def permutation(i, j):
    if (i, j) in permutations:
        return permutations((i, j))
    else:
        return reduce(mul, range(j, i+1))

s = 0
result = 0
for d, i in zip(distances, count(1)):
    lt = i - 1
    gt = n - i
    t = factorials[lt] * combination(n, gt) * factorials[gt]
    s += t
    result += d * s

print(int(result % (pow(10,9) + 7)))

Submission status: Runtime Error

Solution code:
import math

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

fact = math.factorial(N - 1)
fact_list = []
tmp = 0

for i in range(1, N):
    tmp += fact / i
    fact_list.append(int(tmp))

ans = 0
mod = 1e9 + 7

for i in range(N - 1):
    ans += (X[i + 1] - X[i]) * (fact_list[i])
    ans %= mod

print(int(ans))

Submission status: Runtime Error

Solution code:
import math

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

ans = 0
MOD = 10**9 + 7

factorial = math.factorial(n-1)

p = [0]*(n-1)
p[0] = 1 * factorial%MOD

for i in range(1,n-1):
    p[i] = p[i-1] + (factorial/(i+1))%MOD
    p[i] %= MOD

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

ans %= MOD

print(int(ans))

Submission status: Runtime Error

Solution code:
# 二項係数を"やるだけ"にしてくれるライブラリ
MAX = 10005
MOD = 1000000007
factrial = [0]*MAX
inverse = [0]*MAX
factrial_inverse = [0]*MAX


# テーブルを作る前処理
def COMinit():
    global factrial, inverse, factrial_inverse
    factrial[0] = 1
    factrial[1] = 1
    inverse[1] = 1
    factrial_inverse[0] = 1
    factrial_inverse[1] = 1
    for i in range(2, MAX):
        factrial[i] = factrial[i-1] * i % MOD
        inverse[i] = MOD - inverse[MOD % i] * (MOD//i) % MOD
        factrial_inverse[i] = factrial_inverse[i-1] * inverse[i] % MOD


# 二項係数計算
def COM(n, k):
    global factrial, inverse, factrial_inverse
    if n < k:
        return 0
    if n < 0 or k < 0:
        return 0
    return factrial[n] * (factrial_inverse[k] * factrial_inverse[n-k] % MOD) % MOD


# 前処理完了
COMinit()

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

ans = 0
prepro = [1]
for i in range(2, N+1):
    prepro.append((prepro[-1]+inverse[i]) % MOD)

for i in range(N-1):
    ans += (X[i+1]-X[i]) * prepro[i] % MOD
    
ans *= factrial[N-1]  # 期待値の総和に(N-1)!をかける

print(ans % MOD)

Submission status: Runtime Error

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

kaijo = 1
for i in range(1,N):
    kaijo *= i
    #kaijo %= MOD
"""for i in range(N-1):
    d[i] = x[i+1] - x[i]
    d[i] %= MOD
"""
prob = []
a = 0
for i in range(1,N):
    a += 1/i
    prob.append(a)
ans = 0
for i in range(1,N):

    """for j in range(1,i+1):
        prob += 1/j
    """
    ans += (x[i]-x[i-1]) * prob[i-1]
ans *= kaijo
ans %= MOD
ans = int(ans)
print(ans)

Submission status: Runtime Error

Solution code:
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
  
import sys
sys.setrecursionlimit(100000)
from math import factorial
mod = 10**9+7 #出力の制限
N = 10**5+1
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())
x=list(map(int,input().split()))
ans=0
for i in range(n-1):
  for j in range(i+1,n):
    if j!=n-1:
      ans+=((x[j]-x[i]))*(cmb(n-1,j-i+1,mod)*factorial(n-1-j+i-1)%mod*(factorial(j-i-1)%mod))
    else:
      ans+=((x[j]-x[i]))*(cmb(n-1,j-i,mod)*(factorial(n-1-j+i)%mod*(factorial(j-i-1)%mod))
print(ans%mod)

Submission status: Runtime Error

Solution code:
from math import factorial

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


x_dist=[x[i+1]-x[i] for i in range(N-1)]

def Cn(n,Cn_1):
    return n*Cn_1+1

ans=0
Cn_list=[1]

for n in range(2,N):
    temp=Cn(n,Cn_list[n-2])
    Cn_list.append(temp)

for i in range(N-1):
    ans+=Cn_list[i]*x_dist[i]*factorial(N-1)/(i+1)

print((ans)%MOD)

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
memo2={}
def sol(xs):
  if tuple(xs) in memo2:
    return memo2[tuple(xs)]
  if len(xs)==2:
    return(xs[1]-xs[0])
  res=0
  for i,x in enumerate(xs[:-1]):
    xs_=xs.copy()
    xs_.pop(i)
    res+=xs_[i]-x+sol(xs_)
  memo2[tuple(xs)]=res
  return res
print(sol(xs))

Submission status: Runtime Error

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

"""
計算式は以下のように示される
C_n = (1/n)*(1+C_(n-1)) + ((n-1)/n)*C_(n-1)  =  C_(n-1)+(1/n)
"""

import math,sys
sys.setrecursionlimit(10000)
N = int(input())
X = list(map(int, input().split()))

def calc(n):
    if n==1:
        return 1.0
    return calc(n-1)+(1.0/n)

ev = sum( list( map(lambda i: ((X[i]-X[i-1])*calc(i)), list(range(1,N)) )))

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

Submission status: Runtime Error

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

"""
計算式は以下のように示される
C_n = (1/n)*(1+C_(n-1)) + ((n-1)/n)*C_(n-1)  =  C_(n-1)+(1/n)
"""

import math,sys
sys.setrecursionlimit(100000)
N = int(input())
X = list(map(int, input().split()))

def calc(n):
    if n==1:
        return 1.0
    return calc(n-1)+(1.0/n)

ev = 0
for i in range(1,N):
    ev += (X[i]-X[i-1])*calc(i)
    if ev >= (10**9+7):
        ev = ev%(10**9+7)

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

Submission status: Runtime Error

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

"""
計算式は以下のように示される
C_n = (1/n)*(1+C_(n-1)) + ((n-1)/n)*C_(n-1)  =  C_(n-1)+(1/n)
"""

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

def calc(n):
    if n==1:
        return 1.0
    return calc(n-1)+(1.0/n)

ev = 0
for i in range(1,N):
    ev += (X[i]-X[i-1])*calc(i)
    if ev>=(10**9+7):
        ev = ev%(10**9+7)

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

Submission status: Runtime Error

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

"""
計算式は以下のように示される
C_n = (1/n)*(1+C_(n-1)) + ((n-1)/n)*C_(n-1)  =  C_(n-1)+(1/n)
"""
import math
N = int(input())
X = list(map(int, input().split()))

def calc(n):
    if n==1:
        return 1.0
    return calc(n-1)+(1.0/n)

ev=0
for i in range(1,N):
    ev += (X[i]-X[i-1])*calc(i)

print(ev*math.factorial(N-1)%(10**9+7))

Submission status: Runtime Error

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

"""
計算式は以下のように示される
C_n = (1/n)*(1+C_(n-1)) + ((n-1)/n)*C_(n-1)  =  C_(n-1)+(1/n)
"""
import math
N = int(input())
X = list(map(int, input().split()))

def calc(j,i):
    if j==i:
        return 1.0/i
    return calc(j+1,i)+(1.0/j)

ev=0
for i in range(1,N):
    ev += (X[i]-X[i-1])*calc(1,i)

print(round(ev*math.factorial(N-1))%(10**9+7))

Submission status: Runtime Error

Solution code:
from math import factorial

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


x_dist=[x[i+1]-x[i] for i in range(N-1)]

def Cn(n,Cn_1):
    return n*Cn_1+1

ans=0
Cn_list=[1]

for n in range(2,N):
    #fact=1
    #for i in range(1,N):
        #if i!=n:
    #    fact=fact*i
    
    temp=Cn(n,Cn_list[n-2])
    Cn_list.append(temp)

for i in range(N-1):
    ans+=Cn_list[i]*x_dist[i]*factorial(N-1)/(i+1)

print((ans)%MOD)

Submission status: Runtime Error

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

"""
計算式は以下のように示される
C_n = (1/n)*(1+C_(n-1)) + ((n-1)/n)*C_(n-1)  =  C_(n-1)+(1/n)
"""
import math
N = int(input())
X = list(map(int, input().split()))

def calc(j,i):
    if j==i:
        return 1.0/i
    return calc(j+1,i)+(1.0/j)

result = []
for i in range(1, N):
    temp = (X[i]-X[i-1])*calc(1,i)
    result.append(temp)

print(round(sum(result)*math.factorial(N-1))%(10**9+7))

Submission status: Runtime Error

Solution code:
import math

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

ans = 0
MOD = 10**9 + 7

fact = math.factorial(n-1)

p = [0]*(n-1)
p[0] = 1 * fact%MOD

for i in range(1,n-1):
    p[i] = p[i-1] + (fact/(i+1))%MOD
    p[i] %= MOD

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

ans %= MOD

print(int(ans))

Submission status: Runtime Error

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

"""
計算式は以下のように示される
C_n = (1/n)*(1+C_(n-1)) + ((n-1)/n)*C_(n-1)  =  C_(n-1)+(1/n)
"""
import math
N = int(input())
X = list(map(int, input().split()))

def calc(j):
    l = [1.0/i for i in range(1,j+1)]
    return sum(l)

result = []
for i in range(1, N):
    temp = (X[i]-X[i-1])*calc(i)
    result.append(temp)

print(round(sum(result)*math.factorial(N-1))%(10**9+7))

Submission status: Runtime Error

Solution code:
from math import factorial


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

kaijyo = factorial(n-1)
kaijyo_lis = [kaijyo//i for i in range(1, n)]
expect_sum = 0
kaijyo_sum = 0
for i in range(1, n):
    kaijyo_sum += kaijyo_lis[i-1]
    kaijyo_sum %= MOD
    expect_sum += (x[i] - x[i-1]) * kaijyo_sum
    expect_sum %= (10**9+7)
print(expect_sum%(10**9+7))

Submission status: Runtime Error

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

"""
計算式は以下のように示される
C_n = (1/n)*(1+C_(n-1)) + ((n-1)/n)*C_(n-1)  =  C_(n-1)+(1/n)
"""
import math
N = int(input())
X = list(map(int, input().split()))

def calc(i):
    if i==1:
        return 1
    return calc(i-1)+(1.0/i)

result = []
for i in range(1, N):
    temp = (X[i]-X[i-1])*calc(i)
    result.append(temp)
    
print(int(sum(result)*math.factorial(N-1))%(10**9+7))

Submission status: Runtime Error

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

"""
計算式は以下のように示される
C_n = (1/n)*(1+C_(n-1)) + ((n-1)/n)*C_(n-1)  =  C_(n-1)+(1/n)
"""
import math
N = int(input())
X = list(map(int, input().split()))

def calc(i):
    if i==1:
        return 1
    return calc(i-1)+(1.0/i)

result = []
for i in range(1, N):
    temp = (X[i]-X[i-1])*calc(i)
    result.append(temp)
    
print(int(sum(result)*math.factorial(N-1)))

Submission status: Runtime Error

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

"""
計算式は以下のように示される
C_n = (1/n)*(1+C_(n-1)) + ((n-1)/n)*C_(n-1)  =  C_(n-1)+(1/n)
"""
import math
N = int(input())
X = list(map(int, input().split()))

def calc(n):
    if n==1:
        return 1
    return calc(n-1)+(1.0/n)

result = []
for i in range(1, N):
    result.append((X[i]-X[i-1])*calc(i))
        
print(int(sum(result)*math.factorial(N-1)))

Submission status: Runtime Error

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

"""
計算式は以下のように示される
C_n = (1/n)*(1+C_(n-1)) + ((n-1)/n)*C_(n-1)  =  C_(n-1)+(1/n)
"""
import math
N = int(input())
X = list(map(int, input().split()))

def calc(n):
    if n==1:
        return 1
    return calc(n-1)+(1.0/n)

result = []
for i in range(1, N):
    result.append(calc(i))
        
print(sum(result)*math.factorial(N-1))

Submission status: Runtime Error

Solution code:
def Keisu(i):
    product = 0
    for k in range(0,i+1):
        product += 1/(k+1)
    return product

import math
N = int(input())
L = [int(x) for x in input().split()]
Res = 0
prod = 0
for i in range(0,N-1):    
    Res += (L[i+1]-L[i])*(Keisu(i))
    prod = 0
print ((Res*math.factorial(N-1))%(10**9+7))

Submission status: Runtime Error

Solution code:
import math

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

ans = 0
MOD = 10**9 + 7

p = [0]*(n-1)
p[0] = 1 * math.factorial(n-1)
for i in range(1,n-1):
    p[i] = p[i-1] + math.factorial(n-1)/(i+1)
    p[i] %= MOD

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

ans %= MOD

print(int(ans))

Submission status: Runtime Error

Solution code:
from math import factorial

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


x_dist=[x[i+1]-x[i] for i in range(N-1)]

def Cn(n,Cn_1):
    return Cn_1+1/n

ans=0
Cn_list=[1]

for n in range(2,N):
    temp=Cn(n,Cn_list[n-2])
    Cn_list.append(temp)

for i in range(N-1):
    ans+=Cn_list[i]*x_dist[i]

print(int((ans*factorial(N-1)))%MOD)

Submission status: Runtime Error

Solution code:
from math import factorial

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


x_dist=[x[i+1]-x[i] for i in range(N-1)]

def Cn(n,Cn_1):
    return Cn_1+1/n

ans=0
Cn_list=[1]

for n in range(2,N):
    temp=Cn(n,Cn_list[n-2])
    Cn_list.append(temp)

for i in range(N-1):
    ans+=Cn_list[i]*x_dist[i]

print((ans*factorial(N-1))%MOD)

Submission status: Runtime Error

Solution code:
import math
import numpy as np

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

x = np.array(x)

dist = x[1:] - x[:-1]

ans = 0
MOD = 10**9 + 7

p = [0]*(n-1)
p[0] = 1 * math.factorial(n-1)
for i in range(1,n-1):
    p[i] = p[i-1] + math.factorial(n-1)/(i+1)
    p[i] %= MOD

for i in range(n-1):
    ans += dist[i]*p[i]
    ans %= MOD

ans %= MOD

print(int(ans))

Submission status: Runtime Error

Solution code:
import math
N = int(input())
L = [int(x) for x in input().split()]
Res = 0
prod = 0
for i in range(0,N-1):    
    for k in range(0,i+1):
        prod += 1/(k+1)
    Res += (L[i+1]-L[i])*prod
    prod = 0
print ((Res*math.factorial(N-1))%(10**9+7))

Submission status: Runtime Error

Solution code:
import math
N = int(input())
L = [int(x) for x in input().split()]
Res = 0
prod = 0
for i in range(0,N-1):    
    for k in range(0,i+1):
        prod += 1/(k+1)
    print(prod)
    Res += (L[i+1]-L[i])*prod
    prod = 0
print ((Res*math.factorial(N-1))%(10**9+7))

Submission status: Runtime Error

Solution code:
import math

n = int(input())
x = list(map(int, input().split()))
ans = 0
mod = 10 ** 9 + 7
v = math.factorial(n - 1)


for i in range(1, n):
    w = (v//i)%mod
    ans += ((x[n - 1] - x[i - 1]) * w)mod


print(int(ans))

Submission status: Runtime Error

Solution code:
import math

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


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

ans*=math.factorial(n-1)
ans%=mod

print(int(ans))

Submission status: Runtime Error

Solution code:
import math

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


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

ans*=math.factorial(n-1)
ans%=mod

print(int(ans))

Submission status: Runtime Error

Solution code:
from decimal import Decimal

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

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

ans = 0
c = 0
a = 1
for i in range(n-1):
    c += Decimal("1")/Decimal(str((i+1)))
    ans += d[i] * c
    a *= i+1

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

Submission status: Runtime Error

Solution code:
#!/usr/bin/env python3
import sys

MOD = 1000000007  # type: int
import math
from itertools import accumulate

def solve(N: int, x: "List[int]"):
    gyakusu = []
    dif_x = []
    for i in range(1,N):
        gyakusu.append(1/i)
        dif_x.append(x[i]-x[i-1])
    
    accum_gyakusu = list(accumulate(gyakusu))
    answer = 0

    for i in range(N-1):
        answer += accum_gyakusu[i]*dif_x[i]
        answer %= MOD
    
    print(int(answer*math.factorial(N-1)%MOD))
    return


def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    x = [int(next(tokens)) for _ in range(N)]  # type: "List[int]"
    solve(N, x)

if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
#!/usr/bin/env python3
import sys

MOD = 1000000007  # type: int
import math
from itertools import accumulate

# def mod_factorial(n):
#     value = 1
#     for i in range(n,0,-1):
#         value*=i
#         value%=MOD

#     return value

def solve(N: int, x: "List[int]"):
    gyakusu = []
    dif_x = []
    for i in range(1,N):
        gyakusu.append(1/i)
        dif_x.append(x[i]-x[i-1])
    
    accum_gyakusu = list(accumulate(gyakusu))
    answer = 0
    for i in range(N-1):
        answer += accum_gyakusu[i]*dif_x[i]
        answer %= MOD
    # print(answer)
    
    print(int(answer*math.factorial(N-1)%MOD))
    return


def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    x = [int(next(tokens)) for _ in range(N)]  # type: "List[int]"
    solve(N, x)

if __name__ == '__main__':
    main()

Submission status: Runtime Error

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

ex = [1]
t = 1
for j in range(2, n):
    t += 1/j
    ex.append(t)
totalex = 0
for s, e in zip(sp, ex):
    totalex += (s*e)%mod
ans = (totalex*math.factorial(n-1)) % mod
print(int(ans))