> For the complete documentation index, see [llms.txt](https://mayanktyagi3111.gitbook.io/interview-prep/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://mayanktyagi3111.gitbook.io/interview-prep/stacks-and-queues/sum-of-subarray-minimums.md).

# Sum of Subarray Minimums

Given an array of integers `A`, find the sum of `min(B)`, where `B` ranges over every (contiguous) subarray of `A`.

Since the answer may be large, **return the answer modulo `10^9 + 7`.**

**Example 1:**

```
Input: [3,1,2,4]
Output: 17
Explanation: Subarrays are [3], [1], [2], [4], [3,1], [1,2], [2,4], [3,1,2], [1,2,4], [3,1,2,4]. 
Minimums are 3, 1, 2, 4, 1, 1, 2, 1, 1, 1.  Sum is 17.
```

**Note:**

1. `1 <= A.length <= 30000`
2. `1 <= A[i] <= 30000`

```java
class Solution {
    public int sumSubarrayMins(int[] A) {
        // We will maintain an increasing stack
        Deque<Integer> stack = new LinkedList<>();
        int[] leftDistance = new int[A.length];
        int[] rightDistance = new int[A.length];

        for (int i = 0; i < A.length; i++) {
            // use ">=" to deal with duplicate elements
            while (!stack.isEmpty() && A[stack.peek()] >= A[i])
                stack.pop();

            leftDistance[i] = stack.isEmpty() ? (i + 1) : (i - stack.peek());
            stack.push(i);
        }
        stack.clear();
        for (int i = A.length - 1; i >= 0; i--) {
            while (!stack.isEmpty() && A[stack.peek()] > A[i])
                stack.pop();

            rightDistance[i] = stack.isEmpty() ? (A.length - i) : (stack.peek() - i);
            stack.push(i);
        }

        int ans = 0;
        int mod = 1000000007;
        for (int i = 0; i < A.length; i++)
            // Total number of subarrays with A[i] as min element
            ans = (ans + A[i] * leftDistance[i] * rightDistance[i]) % mod;
        return ans;
    }
}
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://mayanktyagi3111.gitbook.io/interview-prep/stacks-and-queues/sum-of-subarray-minimums.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
