#include <bits/stdc++.h>
using namespace std;
constexpr long long MOD = 1000000007;
constexpr long long INF = 2147483647;
constexpr long long INFF = 9223372036854775807;
signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  long long N;
  cin >> N;
  if (N > 2000) {
    cout << -1 << endl;
    return 0;
  }
  long long X[N];
  long long sum[N - 1];
  for (long long i = 0; i < N; ++i) {
    cin >> X[i];
  }
  for (long long i = 0; i < N; ++i) {
    sum[i] = X[N - 1] - X[i];
    sum[i] %= MOD;
  }
  long long ans = 0;
  long long ha[2000];
  ha[0] = 1;
  for (long long i = 1; i < 2000; ++i) {
    ha[i] = ha[i - 1] * 2 % MOD;
  }
  for (long long i = 0; i < N; ++i) {
    ans += ha[N - 2 - i] * sum[i] % MOD;
    ans %= MOD;
  }
  cout << ans << endl;
}
