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

Solution in Kotlin (1.0.0) (judge verdict: Accepted)

fun main(args: Array<String>) {
    dwacon6thPrelimsB()
}

fun dwacon6thPrelimsB() {
    val n = readLine()!!.toInt()
    val xList = readLine()!!.split(' ').map { it.toLong() }.toLongArray()

    val diffs = LongArray(n - 1)
    for (i in 0 until n - 1) diffs[i] = xList[i + 1] - xList[i]

    var answer = Mint(0)
    var c = Mint(0)

    nCkInit()

    for (i in diffs.indices) {
        c += inv[i + 1]
        answer += c * diffs[i]
    }

    answer *= fac[n - 1]

    println(answer)
}

private const val MOD = (1e9 + 7).toLong()

private const val MAX = 1000000
private val fac = LongArray(MAX)
private val fInv = LongArray(MAX)
private val inv = LongArray(MAX)

private fun nCkInit() {
    fac[0] = 1
    fac[1] = 1
    fInv[0] = 1
    fInv[1] = 1
    inv[1] = 1
    for (i in 2 until MAX) {
        fac[i] = fac[i - 1] * i % MOD
        inv[i] = MOD - inv[(MOD % i).toInt()] * (MOD / i) % MOD
        fInv[i] = fInv[i - 1] * inv[i] % MOD
    }
}

private class Mint(value: Long) {
    private val value = if (value < 0) (value % MOD) + MOD else value % MOD
    operator fun unaryPlus() = this
    operator fun unaryMinus() = Mint(MOD - this.value)
    operator fun inc() = this.plus(1)
    operator fun dec() = this.minus(1)
    operator fun plus(other: Long) = Mint(this.value + other)
    operator fun plus(other: Mint) = this.plus(other.value)
    operator fun minus(other: Long) = Mint(this.value - other)
    operator fun minus(other: Mint) = this.plus(-other)
    operator fun times(other: Long) = Mint(this.value * other)
    operator fun times(other: Mint) = this.times(other.value)
    operator fun div(other: Long) = Mint(modPow(other, MOD - 2, MOD))
    operator fun div(other: Mint) = this.div(other.value)
    override fun toString(): String {
        return this.value.toString()
    }

    override fun equals(other: Any?): Boolean {
        if (this === other) return true
        if (javaClass != other?.javaClass) return false

        other as Mint

        if (value != other.value) return false

        return true
    }

    override fun hashCode(): Int {
        return value.hashCode()
    }

    @Suppress("SameParameterValue")
    companion object Mod {
        private fun modPow(n: Long, p: Long, m: Long): Long {
            var x = n
            var y = p
            var result = 1L
            x %= m
            while (y > 0) {
                if (y % 2 == 1L)
                    result = (result * x) % m
                y = y shr 1
                x = (x * x) % m
            }
            return result
        }
    }
}