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 Rust (1.15.1) (judge verdict: Accepted)

#[test]
fn test_solve() {
    assert_eq!(solve("3\n1 2 3\n"), "5\n");
    assert_eq!(solve("12\n161735902 211047202 430302156 450968417 628894325 707723857 731963982 822804784 880895728 923078537 971407775 982631932\n"), "750927044\n");
}

fn solve(src: &str) -> String {
    let mut sc = Scanner::from_str(src);

    let n: usize = sc.read();
    let x: Vec<u64> = (0..n).map(|_| sc.read()).collect();
    let d: Vec<u64> = (0..n-1).map(|i| x[i+1]-x[i]).collect();
    
    let mut ans = Finite(0);
    let mut acc = Finite(0);
    for i in 0..n-1 {
        acc += Finite(1) / (i+1) as u64;
        ans += acc * d[i];
    }
    for i in 1..n {
        ans *= i as u64;
    }

    format!("{}\n", ans)
}

const MOD: u64 = 1_000_000_007;
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
struct Finite(u64);
impl Finite {
    pub fn new(n: u64) -> Self {
        Finite(n % MOD)
    }
    pub fn pow(self, mut exp: u32) -> Self {
        let mut base = self.0;
        let mut acc = 1;
        while exp > 1 {
            if (exp & 1) == 1 {
                acc = acc * base % MOD;
            }
            exp >>= 1;
            base = base * base % MOD;
        }
        if exp == 1 {
            acc = acc * base % MOD;
        }
        Finite(acc)
    }
}
impl std::fmt::Display for Finite {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        write!(f, "{}", self.0)
    }
}
impl std::ops::Add for Finite {
    type Output = Self;
    fn add(self, rhs: Self) -> Self {
        Finite((self.0 + rhs.0) % MOD)
    }
}
impl std::ops::Add<u64> for Finite {
    type Output = Self;
    fn add(self, rhs: u64) -> Self {
        Finite((self.0 + rhs) % MOD)
    }
}
impl std::ops::AddAssign for Finite {
    fn add_assign(&mut self, rhs: Self) {
        self.0 = (self.0 + rhs.0) % MOD;
    }
}
impl std::ops::AddAssign<u64> for Finite {
    fn add_assign(&mut self, rhs: u64) {
        self.0 = (self.0 + rhs) % MOD;
    }
}
impl std::ops::Sub for Finite {
    type Output = Self;
    fn sub(self, rhs: Self) -> Self {
        Finite((self.0 + MOD - rhs.0) % MOD)
    }
}
impl std::ops::Sub<u64> for Finite {
    type Output = Self;
    fn sub(self, rhs: u64) -> Self {
        Finite((self.0 + MOD - rhs) % MOD)
    }
}
impl std::ops::SubAssign for Finite {
    fn sub_assign(&mut self, rhs: Self) {
        self.0 = (self.0 + MOD - rhs.0) % MOD;
    }
}
impl std::ops::SubAssign<u64> for Finite {
    fn sub_assign(&mut self, rhs: u64) {
        self.0 = (self.0 + MOD - rhs) % MOD;
    }
}
impl std::ops::Mul for Finite {
    type Output = Self;
    fn mul(self, rhs: Self) -> Self {
        Finite(self.0 * rhs.0 % MOD)
    }
}
impl std::ops::Mul<u64> for Finite {
    type Output = Self;
    fn mul(self, rhs: u64) -> Self {
        Finite(self.0 * rhs % MOD)
    }
}
impl std::ops::MulAssign for Finite {
    fn mul_assign(&mut self, rhs: Self) {
        self.0 = self.0 * rhs.0 % MOD;
    }
}
impl std::ops::MulAssign<u64> for Finite {
    fn mul_assign(&mut self, rhs: u64) {
        self.0 = self.0 * rhs % MOD;
    }
}
impl std::ops::Div for Finite {
    type Output = Self;
    fn div(self, rhs: Self) -> Self {
        self * rhs.pow((MOD - 2) as u32)
    }
}
impl std::ops::Div<u64> for Finite {
    type Output = Self;
    fn div(self, rhs: u64) -> Self {
        self * Finite(rhs).pow((MOD - 2) as u32)
    }
}
impl std::ops::DivAssign for Finite {
    fn div_assign(&mut self, rhs: Self) {
        *self = *self * rhs.pow((MOD - 2) as u32);
    }
}
impl std::ops::DivAssign<u64> for Finite {
    fn div_assign(&mut self, rhs: u64) {
        *self = *self * Finite(rhs).pow((MOD - 2) as u32);
    }
}

fn main() {
    let mut buf = String::new();
    use std::io;
    use std::io::{Read, Write};
    io::stdin().read_to_string(&mut buf).unwrap();
    io::stdout().write_all(solve(&buf).as_bytes()).unwrap();
}

struct Scanner<'a>(std::str::SplitWhitespace<'a>);
impl<'a> Scanner<'a> {
    fn from_str(src: &str) -> Scanner {
        Scanner(src.split_whitespace())
    }
    fn get(&mut self) -> &str {
        self.0.next().unwrap()
    }
    fn read<T: std::str::FromStr>(&mut self) -> T {
        self.get().parse().ok().unwrap()
    }
    fn chars(&mut self) -> Vec<char> {
        self.get().chars().collect()
    }
}