N = gets.to_i
X = gets.split.map &:to_i
D = 10**9+7
t = 0
(1...N).to_a.permutation.each {|a|
  r = (1..N).to_a
  l = (-1..N-1).to_a
  a.each {|k|
	i = l[k]
    j = r[k]
	l[j] = i
	r[i] = j
	t -= D  if D <= t += X[j-1]-X[k-1]
  }
}
p t
