Buggy python submission (verdict: Time Limit Exceeded):
from bisect import bisect_left

N = int(input())
*A, = map(int, input().split())
indices = {a: i for i, a in enumerate(A)}
seen_idx = [-1, N]
ans = 0
for a in range(1, N+1):
    idx = indices[a]
    t = bisect_left(seen_idx, idx)
    seen_idx = seen_idx[:t] + [idx] + seen_idx[t:]
    left = idx - seen_idx[t-1]
    right = seen_idx[t+1] - idx
    ans += left * right * a
print(ans)

Fixed submission:
N = int(input())
*A, = map(int, input().split())
indices = {a: i for i, a in enumerate(A)}
left = {i: i for i in range(N)} # "left[i] = l" is equivalent to "A[l], ..., A[i-1] > A[i]"
right = {i: i for i in range(N)} # "right[i] = r" is equivalent to "A[i] < A[i+1], ..., A[r]"
ans = 0
for a in range(N, 0, -1):
    idx = indices[a]
    l, r = left[idx], right[idx]
    ans += (idx - l + 1) * (r - idx + 1) * a
    left[r+1], right[l-1] = l, r # A[l-1] < A[l], ..., A[r] > A[r+1]
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
from bisect import bisect_left

N = int(input())
*A, = map(int, input().split())
indices = {a: i for i, a in enumerate(A)}
seen_idx = [-1, N]
ans = 0
for a in range(1, N+1):
    idx = indices[a]
    t = bisect_left(seen_idx, idx)
    seen_idx = seen_idx[:t] + [idx] + seen_idx[t:]
    left = idx - seen_idx[t-1]
    right = seen_idx[t+1] - idx
    ans += left * right * a
print(ans)

Fixed submission:
N = int(input())
*A, = map(int, input().split())
indices = {a: i for i, a in enumerate(A)}
left = {i: i for i in range(N)} # "left[i] = l" is equivalent to "A[l], ..., A[i-1] > A[i]"
right = {i: i for i in range(N)} # "right[i] = r" is equivalent to "A[i] < A[i+1], ..., A[r]"
ans = 0
for a in range(N, 0, -1):
    idx = indices[a]
    l, r = left[idx], right[idx]
    ans += (idx - l + 1) * (r - idx + 1) * a
    left[r+1], right[l-1] = l, r # A[l-1] < A[l], ..., A[r] > A[r+1]
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
from bisect import bisect_left

N = int(input())
*A, = map(int, input().split())
indices = {a: i for i, a in enumerate(A)}
seen_idx = [-1, N]
ans = 0
for a in range(1, N+1):
    idx = indices[a]
    t = bisect_left(seen_idx, idx)
    seen_idx.insert(t, idx)
    left = idx - seen_idx[t-1]
    right = seen_idx[t+1] - idx
    ans += left * right * a
print(ans)

Fixed submission:
N = int(input())
*A, = map(int, input().split())
indices = {a: i for i, a in enumerate(A)}
left = {i: i for i in range(N)} # "left[i] = l" is equivalent to "A[l], ..., A[i-1] > A[i]"
right = {i: i for i in range(N)} # "right[i] = r" is equivalent to "A[i] < A[i+1], ..., A[r]"
ans = 0
for a in range(N, 0, -1):
    idx = indices[a]
    l, r = left[idx], right[idx]
    ans += (idx - l + 1) * (r - idx + 1) * a
    left[r+1], right[l-1] = l, r # A[l-1] < A[l], ..., A[r] > A[r+1]
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
class SegmentTree:
    def __init__(self, lst, op, e):
        self.n = len(lst)
        self.N = 2**((self.n-1).bit_length())
        self.op = op # operation
        self.e = e # identity element
        self.v = [None]*(2*self.N-1)
        
        # construction
        for i in range(self.N):
            self.v[i + self.N - 1] = lst[i] if i < self.n else self.e
        for i in range(self.N - 2, -1, -1):
            self.v[i] = self.op(self.v[2*i + 1], self.v[2*i + 2])
        
    # update a_i to be x 
    def update(self, i, x):
        i += self.N - 1
        self.v[i] = x
        while i > 0:
            i -= 1
            i >>= 1
            self.v[i] = self.op(self.v[2*i + 1], self.v[2*i+2])
    
    # returns answer for the query interval [l, r)
    def query(self, l, r):
        assert l < r
        left = l + self.N - 1
        right = r + self.N - 1
        L = self.e
        R = self.e
        while left < right:
            if left % 2 == 0: # left is even
                L = self.op(L, self.v[left])
                left += 1
            if right % 2 == 0: # right is even
                right -= 1
                R = self.op(self.v[right], R)
            left -= 1
            left >>= 1
            right >>= 1
        return self.op(L, R)

N = int(input())
*A, = map(int, input().split())
INF = float('inf')
seg_tr = SegmentTree(A, min, INF)
ans = 0
for l in range(N):
    for r in range(l+1, N+1):
        ans += seg_tr.query(l, r)
print(ans)

Fixed submission:
N = int(input())
*A, = map(int, input().split())
indices = {a: i for i, a in enumerate(A)}
left = {i: i for i in range(N)} # "left[i] = l" is equivalent to "A[l], ..., A[i-1] > A[i]"
right = {i: i for i in range(N)} # "right[i] = r" is equivalent to "A[i] < A[i+1], ..., A[r]"
ans = 0
for a in range(N, 0, -1):
    idx = indices[a]
    l, r = left[idx], right[idx]
    ans += (idx - l + 1) * (r - idx + 1) * a
    left[r+1], right[l-1] = l, r # A[l-1] < A[l], ..., A[r] > A[r+1]
print(ans)