N = int(input())
P = list(map(int,input().split()))
num = 0
i = 0
import math
import copy

first = 1
second = 1
mod_list = [0] * (N-1)
mod_list[0] = first
mod_list[1] = second
for i,_ in enumerate(mod_list):
  if i < 2:
    pass
  else:
    mod_list[i] = math.factorial(i)

mod_list.reverse()

count = 0

while count < math.factorial(N-1):
  K = copy.deepcopy(P)
  k = count
  zero = [0] * (N-1)
  for i,j in enumerate(mod_list):
    q = k // mod_list[i]
    mod = k % mod_list[i]
    k = mod
    zero[i] = q
  #print("zero: {}".format(zero))
  for i in zero:
      num += K[i+1] - K[i]
      K.pop(i)

    
    
  count += 1

print(num % (10^9+7))
  