<p>As the ruler of a kingdom, you have an army of wizards at your command.</p>

<p>You are given a <strong>0-indexed</strong> integer array <code>strength</code>, where <code>strength[i]</code> denotes the strength of the <code>i<sup>th</sup></code> wizard. For a <strong>contiguous</strong> group of wizards (i.e. the wizards&#39; strengths form a <strong>subarray</strong> of <code>strength</code>), the <strong>total strength</strong> is defined as the <strong>product</strong> of the following two values:</p>

<ul>
	<li>The strength of the <strong>weakest</strong> wizard in the group.</li>
	<li>The <strong>total</strong> of all the individual strengths of the wizards in the group.</li>
</ul>

<p>Return <em>the <strong>sum</strong> of the total strengths of <strong>all</strong> contiguous groups of wizards</em>. Since the answer may be very large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p>

<p>A <strong>subarray</strong> is a contiguous <strong>non-empty</strong> sequence of elements within an array.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> strength = [1,3,1,2]
<strong>Output:</strong> 44
<strong>Explanation:</strong> The following are all the contiguous groups of wizards:
- [1] from [<u><strong>1</strong></u>,3,1,2] has a total strength of min([1]) * sum([1]) = 1 * 1 = 1
- [3] from [1,<u><strong>3</strong></u>,1,2] has a total strength of min([3]) * sum([3]) = 3 * 3 = 9
- [1] from [1,3,<u><strong>1</strong></u>,2] has a total strength of min([1]) * sum([1]) = 1 * 1 = 1
- [2] from [1,3,1,<u><strong>2</strong></u>] has a total strength of min([2]) * sum([2]) = 2 * 2 = 4
- [1,3] from [<u><strong>1,3</strong></u>,1,2] has a total strength of min([1,3]) * sum([1,3]) = 1 * 4 = 4
- [3,1] from [1,<u><strong>3,1</strong></u>,2] has a total strength of min([3,1]) * sum([3,1]) = 1 * 4 = 4
- [1,2] from [1,3,<u><strong>1,2</strong></u>] has a total strength of min([1,2]) * sum([1,2]) = 1 * 3 = 3
- [1,3,1] from [<u><strong>1,3,1</strong></u>,2] has a total strength of min([1,3,1]) * sum([1,3,1]) = 1 * 5 = 5
- [3,1,2] from [1,<u><strong>3,1,2</strong></u>] has a total strength of min([3,1,2]) * sum([3,1,2]) = 1 * 6 = 6
- [1,3,1,2] from [<u><strong>1,3,1,2</strong></u>] has a total strength of min([1,3,1,2]) * sum([1,3,1,2]) = 1 * 7 = 7
The sum of all the total strengths is 1 + 9 + 1 + 4 + 4 + 4 + 3 + 5 + 6 + 7 = 44.
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> strength = [5,4,6]
<strong>Output:</strong> 213
<strong>Explanation:</strong> The following are all the contiguous groups of wizards: 
- [5] from [<u><strong>5</strong></u>,4,6] has a total strength of min([5]) * sum([5]) = 5 * 5 = 25
- [4] from [5,<u><strong>4</strong></u>,6] has a total strength of min([4]) * sum([4]) = 4 * 4 = 16
- [6] from [5,4,<u><strong>6</strong></u>] has a total strength of min([6]) * sum([6]) = 6 * 6 = 36
- [5,4] from [<u><strong>5,4</strong></u>,6] has a total strength of min([5,4]) * sum([5,4]) = 4 * 9 = 36
- [4,6] from [5,<u><strong>4,6</strong></u>] has a total strength of min([4,6]) * sum([4,6]) = 4 * 10 = 40
- [5,4,6] from [<u><strong>5,4,6</strong></u>] has a total strength of min([5,4,6]) * sum([5,4,6]) = 4 * 15 = 60
The sum of all the total strengths is 25 + 16 + 36 + 36 + 40 + 60 = 213.
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= strength.length &lt;= 10<sup>5</sup></code></li>
	<li><code>1 &lt;= strength[i] &lt;= 10<sup>9</sup></code></li>
</ul>

class Solution {
public:
    int mod=1e9+7;
    int Value(vector<int> a, int n){
        vector<long long>pre(n+2,0);
        for(int i=1;i<=n;i++)
            pre[i]=(pre[i-1]+a[i-1])%mod;
        for(int i=1;i<=n+1;i++)
            pre[i]=(pre[i-1]+pre[i])%mod;
        for(int i=n+1;i>0;i--)
            pre[i]=pre[i-1];

        vector<int> l(n,-1),r(n,n);
        stack<int> st;
        // Find all left index
        for(int i = 0; i < n; i++){
            while (!st.empty() && a[st.top()] >= a[i])
                st.pop();
            if (!st.empty())
                l[i] = st.top();

            st.push(i);
        }

        while(!st.empty())
        st.pop();

        // Find all right index
        for(int i = n - 1; i >= 0; i--){
            while (!st.empty() && a[st.top()] > a[i])
                st.pop();

                if (!st.empty())
                    r[i] = st.top();

            st.push(i);
        }
        // for(int i=0;i<n;i++) cout<<a[i]<<" "; cout<<"\n";
        // for(int i=0;i<n;i++) cout<<l[i]<<" "; cout<<"\n";
        // for(int i=0;i<n;i++) cout<<r[i]<<" "; cout<<"\n";
        
        long long ans = 0;
        for (int i = 0; i < n; ++i) {
            ans += (((pre[r[i] + 1] - pre[i + 1]) * (i - l[i]) % mod + mod * 2 - 
                (pre[i + 1] - pre[l[i] + 1]) * (r[i] - i) % mod) % mod * a[i]) % mod;
            ans %= mod;
        }
        return (int) ans;

    }
    int totalStrength(vector<int>& s) {
        int n=s.size();
        return Value(s,n);
    }
};











// class Solution {
// public:
//     int totalStrength(vector<int>& strength) {
//         long long ans = 0;
//         long long M = 1e9 + 7;
//         int n = strength.size();
//         vector<long long> ab(strength.size(), 0);
//         ab[strength.size() - 1] = strength[n-1];
//         for(int i=n-2;i>=0;i--){
//             ab[i] = (strength[i]+ab[i+1]);
//         }
//         vector<long long> prefix(n, 0);
//         prefix[0] = strength[0];
//         for(int i=1;i<n;i++){
//             prefix[i] = prefix[i-1] + strength[i];
//         }
//         stack<int> st;
//         vector<long long> sum(n, 0);
//         //
//         cout<<"ab[i]->"<<endl;
//         for(int i=0;i<n;i++){
//             cout<<ab[i]<<" ";
//         }
//         cout<<endl;
//         cout<<"prefix[i]->"<<endl;
//         for(int i=0;i<n;i++){
//             cout<<prefix[i]<<" ";
//         }
//         cout<<endl;
//         //
//         for(int i=strength.size()-1;i>=0;i--){
//             if(i == strength.size()-1){
//                 ans = strength[i]*strength[i];
//                 ans = ans%M;
//                 st.push(i);
//                 sum[i] = strength[i];
//             }
//             else{
//                 while(!st.empty() && strength[i] <= strength[st.top()]){
//                     sum[st.top()] = 0;
//                     st.pop();
//                 }
//                 int ind = n;
//                 if(!st.empty()) ind = st.top();
//                 cout<<"ind-> "<<ind<<endl;
//                 long long temp = ab[i];
//                 if(ind != n) temp = temp - ab[ind];
//                 cout<<"temp-> "<<temp<<endl; 

//                 temp = temp*strength[i];
//                 cout<<"temp-> "<<temp<<endl;
//                 long long pSum = prefix[ind-1];
//                 if(i != 0) pSum = pSum - prefix[i-1];
//                 if(ind != n)pSum = pSum*sum[ind];
//                 else pSum = 0;
//                 cout<<"psum-> "<<pSum<<endl;
//                 temp = temp + pSum;
//                 ans = ans + temp;
//                 long long t1 = ind - i;
//                 t1 = t1*strength[i];
//                 sum[i] = t1;
//                 cout<<"t1-> "<<t1<<endl;
//                 cout<<"i-> "<<i<<endl;
//                 st.push(i);
//             }
//             // cout<<mini<<" "<<miniSum<<" "<<last<<" "<<ans<<endl;
//             cout<<"ans-> "<<ans<<endl;
//             cout<<"sum-> "<<endl;
//             for(int i=0;i<n;i++) cout<<sum[i]<<" ";
//             cout<<endl;
//             cout<<"size of stack "<<st.size()<<endl;
//         }
//         return ans%M;
//     }
// };
102346