from itertools import permutations

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

ans = 0
for L in permutations(range(N-1), N-1):
  C = X.copy()
  for l in L:
    ans += C[l+1] - C[l]
    c = C[l]
    while C[l] == c:
      C[l] = C[l+1]
      l -= 1
print(ans % (10**9+7))
