<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>

/* An amazing problem invloving data structures, logic, previous
 implementation practice and dragon level mathematics and deduction */
class Solution {
private:
    #define ll long long
    #define mod (ll)(pow(10, 9) + 7)
public:
    int totalStrength(vector<int>& strength) {
        ios::sync_with_stdio(false);
        cin.tie(nullptr);
        cout.tie(nullptr);

        ll n = strength.size();
        vector<int> next_smaller(n, -1), previous_smaller(n, -1);
        // next_smaller ---> strictly next smaller
        // previous_smaller ---> previous smaller or equal
        stack<pair<int, int>> st;
        for (int i = n - 1; i >= 0; i--) {
            while (!st.empty() and st.top().first >= strength[i])
                st.pop();
            if (!st.empty())
                next_smaller[i] = st.top().second;
            st.push({strength[i], i});
        }

        while (!st.empty())
            st.pop();
        for (int i = 0; i < n; i++) {
            while (!st.empty() and st.top().first > strength[i])
                st.pop();
            if (!st.empty())
                previous_smaller[i] = st.top().second;
            st.push({strength[i], i});
        }

        vector<ll> prefix(n, 0);
        for(int i = 0; i < n; i++)
        {
            prefix[i] = strength[i];
            if(i)
                prefix[i] += prefix[i - 1];
        }
        vector<ll> super_prefix(n, 0);
        for(int i = 0; i < n; i++)
        {
            super_prefix[i] = prefix[i];
            if(i)
                super_prefix[i] += super_prefix[i - 1], super_prefix[i] %= mod;
        }

        vector<ll> suffix(n, 0);
        for(int i = n - 1; i >= 0; i--)
        {
            suffix[i] = strength[i];
            if(i < n - 1)
                suffix[i] += suffix[i + 1]; 
        }
        vector<ll> super_suffix(n, 0);
        for(int i = n - 1; i >= 0; i--)
        {
            super_suffix[i] = suffix[i];
            if(i < n - 1)
                super_suffix[i] += super_suffix[i + 1], super_suffix[i] %= mod;
        }

        ll ans = 0;
        for(int i = 0; i < n; i++)
        {
            int left_index = previous_smaller[i], right_index = next_smaller[i];

            ll left = 0, right = 0;
            if(left_index != -1)
                left = i - left_index - 1;
            else
                left = i;
            
            if(right_index != -1)
                right = right_index - i - 1;
            else
                right = n - i - 1;

            ll left_sum = 0, right_sum = 0;
            if(left_index != -1)
                left_sum = super_suffix[left_index + 1];
            else
                left_sum = super_suffix[0];
            if(i < n - 1)
                left_sum -= (super_suffix[i + 1] + (left + 1) * suffix[i + 1]);
            left_sum %= mod;

            if(right_index != -1)
                right_sum =  super_prefix[right_index - 1];
            else
                right_sum = super_prefix[n - 1];
            if(i > 0)
                right_sum -= (prefix[i - 1] * (right + 1) + super_prefix[i - 1]);
            right_sum %= mod;

            if(left and right) // Please perform dry run to understand :)
            {
                ans += (((left + 1) * right_sum) % mod * (ll)strength[i]) % mod;
                ans %= mod;
                ans += (((right + 1) * left_sum) % mod * strength[i]) % mod;
                ans %= mod;
                ans -= ((((left + 1) * (right + 1)) % mod * (ll)strength[i]) % mod * strength[i]) % mod;
                ans = (ans + mod) % mod;
            }
            else if(left)
                ans += ((left_sum) * (ll)strength[i]) % mod, ans %= mod;
            else if(right)
                ans += ((right_sum) * (ll)strength[i]) % mod, ans %= mod;
            else
                ans += ((ll)strength[i] * (ll)strength[i]) % mod, ans %= mod;
        }
        ans = (ans + mod) % mod;
        return ans;
    }
};
119828