import itertools
import fractions

mod = 10 ** 9 + 7

N = int(input())

x = list(map(int, input().split()))
for i in range(N):
    x[i] %= mod

x = x[::-1]
x_head = x.pop(0)
ans = 0

for i in itertools.permutations(x):
    ar = list(i)
    ar.insert(0, x_head)
    for j in range(N-1):
        ans += abs(ar[j] - ar[j+1])
print(ans)