#include using namespace std; vectorB; long int x=1000000007; void printKMax(vectorarr, int n, int k) { // Create a Double Ended Queue, Qi that will store indexes of array elements // The queue will store indexes of useful elements in every window and it will // maintain decreasing order of values from front to rear in Qi, i.e., // arr[Qi.front[]] to arr[Qi.rear()] are sorted in decreasing order std::deque Qi(k); /* Process first k (or first window) elements of array */ int i; for (i = 0; i < k; ++i) { // For very element, the previous smaller elements are useless so // remove them from Qi while ( (!Qi.empty()) && arr[i] >= arr[Qi.back()]) Qi.pop_back(); // Remove from rear // Add new element at rear of queue Qi.push_back(i); } // Process rest of the elements, i.e., from arr[k] to arr[n-1] for ( ; i < n; ++i) { B.push_back(arr[Qi.front()]); // The element at the front of the queue is the largest element of // previous window, so print it //cout << arr[Qi.front()] << " "; // Remove the elements which are out of this window while ( (!Qi.empty()) && Qi.front() <= i - k) Qi.pop_front(); // Remove from front of queue // Remove all elements smaller than the currently // being added element (remove useless elements) while ( (!Qi.empty()) && arr[i] >= arr[Qi.back()]) Qi.pop_back(); // Add current element at the rear of Qi Qi.push_back(i); } // Print the maximum element of last window B.push_back(arr[Qi.front()]); // cout<arr, int n, int k) { // Create a Double Ended Queue, Qi that will store indexes of array elements // The queue will store indexes of useful elements in every window and it will // maintain decreasing order of values from front to rear in Qi, i.e., // arr[Qi.front[]] to arr[Qi.rear()] are sorted in decreasing order std::deque Qi(k); /* Process first k (or first window) elements of array */ int i; for (i = 0; i < k; ++i) { // For very element, the previous smaller elements are useless so // remove them from Qi while ( (!Qi.empty()) && arr[i] >= arr[Qi.back()]) Qi.pop_back(); // Remove from rear // Add new element at rear of queue Qi.push_back(i); } long int f=0; // Process rest of the elements, i.e., from arr[k] to arr[n-1] for ( ; i < n; ++i) { // B.push_back(arr[Qi.front()]); f=(f+arr[Qi.front()])%x; // The element at the front of the queue is the largest element of // previous window, so print it //cout << arr[Qi.front()] << " "; // Remove the elements which are out of this window while ( (!Qi.empty()) && Qi.front() <= i - k) Qi.pop_front(); // Remove from front of queue // Remove all elements smaller than the currently // being added element (remove useless elements) while ( (!Qi.empty()) && arr[i] >= arr[Qi.back()]) Qi.pop_back(); // Add current element at the rear of Qi Qi.push_back(i); } // Print the maximum element of last window f=(f+arr[Qi.front()])%x; //cout << arr[Qi.front()]; return f; } long int solve(vector A) { for(int i=1;i<=A.size();i++) { printKMax(A,A.size(),i); } long int v=0; for(int i=1;i<=B.size();i++) { v=(v+printK1Max(B,B.size(),i))%x; } return v; // Return the sum of S(S(A)) modulo 10^9+7. } int main() { int n; cin >> n; vector a(n); for(int a_i = 0; a_i < n; a_i++){ cin >> a[a_i]; } int result = solve(a); cout << result << endl; return 0; }