#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  long long n;
  cin >> n;
  long long arr[n];
  for (long long i = 0; i < n; i++) {
    cin >> arr[i];
  }
  long long s = 0;
  for (long long i = 0, j = i + 1; j < n; j++, i++) {
    long long c = 0;
    if (arr[j] < arr[i]) {
      c = (c % 1000000007 + (arr[i] - arr[j]) % 1000000007) % 1000000007;
      arr[j] = (arr[j] % 1000000007 + c % 1000000007) % 1000000007;
      s = (s % 1000000007 + c % 1000000007) % 1000000007;
    } else {
      c = 0;
      s = (s % 1000000007 + c % 1000000007) % 1000000007;
    }
  }
  cout << s << endl;
}
