What did you learn? Monotonic stacks find "extension limits" for each element. How far can each bar extend before hitting something smaller?
The calculation happens at pop time, not push time. When you pop, you know both boundaries.
This problem combines next smaller on the right (why we pop) and previous smaller on the left (the new stack top). You've solved one of the hardest monotonic stack problems. If you understand it, you can handle most stack problems.