#include <bits/stdc++.h>
using namespace std;
int main() {
  int N;
  cin >> N;
  vector<long long> x(N);
  for (long long i = 0; i < (long long)(N); i++) cin >> x.at(i);
  long long inf = 1e9;
  vector<long long> p(N - 1);
  for (long long i = 0; i < (long long)(N - 1); i++) p.at(i) = i;
  vector<int> flag(N);
  for (long long i = 0; i < (long long)(N); i++) flag.at(i) = 0;
  long long ans = 0;
  do {
    for (long long j = 0; j < N - 1; j++) {
      int now = p.at(j);
      flag.at(now) = 1;
      int next = now;
      for (long long k = 1; k + now <= N; k++) {
        if (flag.at(now + k) == 0) {
          next = now + k;
          break;
        }
      }
      ans += (x.at(next) - x.at(now)) % inf;
    }
  } while (next_permutation(p.begin(), p.end()));
  cout << ans << endl;
  return 0;
}
