Buggy python submission (verdict: Time Limit Exceeded):
from collections import defaultdict

#AVL

##search(0,x):O(logN)
# xがある場合indexを、ない場合Noneを返す
def search(root,key):
    if avl_key[root] > key:
        if avl_left[root] == None:
            return None
        else:
            return search(avl_left[root],key)
    if avl_key[root] < key:
        if avl_right[root] == None:
            return None
        else:
            return search(avl_right[root],key)
    return root


##end_lower/higher:search_lower/higherで使用
def end_lower(root):
    if avl_left[root] == None:
        return avl_key[root]
    else:
        return end_lower(avl_left[root])

def end_higher(root):
    if avl_right[root] == None:
        return avl_key[root]
    else:
        return end_higher(avl_right[root])


##search_lower(0,x,None):O(logN)
# xより小さいものの中で最も大きいものを出力。ない場合はNoneを出力
def search_lower(root,key,lower_key):
    if avl_key[root] > key:
        if avl_left[root] == None:
            return lower_key
        else:
            return search_lower(avl_left[root],key,lower_key)
    if avl_key[root] < key:
        lower_key = avl_key[root]
        if avl_right[root] == None:
            return lower_key
        else:
            return search_lower(avl_right[root],key,lower_key)
    
    # == 
    if avl_left[root] == None:
        return lower_key
    else:
        if lower_key == None:
            return end_higher(avl_left[root])
        else:
            return max(lower_key,end_higher(avl_left[root]))

##search_higher(0,x,None):O(logN)
# xより大きいものの中で最も小さいものを出力。ない場合はNoneを出力
def search_higher(root,key,higher_key):
    if avl_key[root] > key:
        higher_key = avl_key[root]
        if avl_left[root] == None:
            return higher_key
        else:
            return search_higher(avl_left[root],key,higher_key)
    if avl_key[root] < key:
        if avl_right[root] == None:
            return higher_key
        else:
            return search_higher(avl_right[root],key,higher_key)
    
    # == 
    if avl_right[root] == None:
        return higher_key
    else:
        if higher_key == None:
            return end_lower(avl_right[root])
        else:
            return min(higher_key,end_lower(avl_right[root]))



##Rotation,replace,insertx:insertで使用
def DoubleRightRotation(x):
    tl = avl_left[x]
    avl_left[x] = avl_right[avl_right[tl]]
    avl_right[avl_right[tl]] = x
    tlr = avl_right[tl]
    avl_right[tl] = avl_left[tlr]
    avl_left[tlr] = tl
    if balance[tlr] == -1:
        balance[avl_right[tlr]] = 1
        balance[avl_left[tlr]] = 0
    elif balance[tlr] == 1:
        balance[avl_right[tlr]] = 0
        balance[avl_left[tlr]] = -1
    else:
        balance[avl_right[tlr]] = 0
        balance[avl_left[tlr]] = 0
    balance[tlr] = 0
    return tlr

def DoubleLeftRotation(x):
    tr = avl_right[x]
    avl_right[x] = avl_left[avl_left[tr]]
    avl_left[avl_left[tr]] = x
    trl = avl_left[tr]
    avl_left[tr] = avl_right[trl]
    avl_right[trl] = tr
    if balance[trl] == 1:
        balance[avl_right[trl]] = 0
        balance[avl_left[trl]] = -1
    elif balance[trl] == -1:
        balance[avl_left[trl]] = 0
        balance[avl_right[trl]] = 1
    else:
        balance[avl_right[trl]] = 0
        balance[avl_left[trl]] = 0
    balance[trl] = 0
    return trl

def SingleLeftRotation(x):
    tr = avl_right[x]
    balance[tr] = 0
    balance[x] = 0
    avl_right[x] = avl_left[tr]
    avl_left[tr] = x
    return tr

def SingleRightRotation(x):
    tl = avl_left[x]
    balance[tl] = 0
    balance[x] = 0
    avl_left[x] = avl_right[tl]
    avl_right[tl] = x
    return tl

def replace(x,p,v):
    if avl_left[p] == x:
        avl_left[p] = v
    else:
        avl_right[p] = v

def insertx(root,p,key):
    if avl_key[root] > key:
        if avl_left[root] == None:
            avl_key.append(key)
            avl_left[root] = len(avl_key)-1
        else:
            if not insertx(avl_left[root],root,key):
                return False
        if balance[root] == 1:
            balance[root] = 0
            return False
        elif balance[root] == 0:
            balance[root] = -1
            return True
        else:
            if balance[avl_left[root]] == 1:
                replace(root,p,DoubleRightRotation(root))
            elif balance[avl_left[root]] == -1:
                replace(root,p,SingleRightRotation(root))
            return False
    if avl_key[root] < key:
        if avl_right[root] == None:
            avl_key.append(key)
            avl_right[root] = len(avl_key)-1
        else:
            if not insertx(avl_right[root],root,key):
                return False
        if balance[root] == -1:
            balance[root] = 0
            return False
        elif balance[root] == 0:
            balance[root] = 1
            return True
        else:
            if balance[avl_right[root]] == -1:
                replace(root,p,DoubleLeftRotation(root))
            elif balance[avl_right[root]] == 1:
                replace(root,p,SingleLeftRotation(root))
            return False
    return False


##insert(0,x):O(logN)
#x追加
def insert(root,key):
    if key < avl_key[root]:
        if avl_left[root] == None:
            avl_key.append(key)
            avl_left[root] = len(avl_key)-1
        else:
            insertx(avl_left[root],root,key)
    elif key > avl_key[root]:
        if avl_right[root] == None:
            avl_key.append(key)
            avl_right[root] = len(avl_key)-1
        else:
            insertx(avl_right[root],root,key)
    else:
        pass

#########################################
N = int(input())
A = [int(i) for i in input().split()]

IDX = defaultdict(int)
for idx,a in enumerate(A):
    IDX[a] = idx + 1

# AVL木の初期化
avl_key = [0]
avl_left = [None] * (N + 2)
avl_right = [None] * (N + 2)
balance = [0] * (N + 2)
insert(0, N + 1)

ans = 0
for n in range(1, N + 1):
    insert(0, IDX[n])
    H = search_higher(0, IDX[n], None)
    L = search_lower(0, IDX[n], None)
    ans += (IDX[n] - L) * (H - IDX[n]) *  n

print(ans)

Fixed submission:
import bisect
from collections import defaultdict
class SqrtSet:
    def __init__(self, block_limit=201):
        self.key = []
        self.child = [[]]
        self.block_limit = block_limit
 
    def search_lower(self, key):
        if key is None:
            return None
        ret = None
        i = bisect.bisect_left(self.key, key)
        if i != 0:
            ret = self.key[i - 1]
        block = self.child[i]
        i = bisect.bisect_left(block, key)
        if i != 0:
            ret = block[i - 1]
        return ret
        
    def search_higher(self, key):
        if key is None:
            return None
        ret = None
        i = bisect.bisect_right(self.key, key)
        if i != len(self.key):
            ret = self.key[i]
        block = self.child[i]
        i = bisect.bisect_right(block, key)
        if i != len(block):
            ret = block[i]
        return ret
 
    def insert(self, key):
        i = bisect.bisect(self.key, key)
        block = self.child[i]
        bisect.insort(block, key)
        if len(block) == self.block_limit:
            sep = self.block_limit // 2
            self.key.insert(i, block[sep])
            self.child.insert(i + 1, block[sep + 1:])
            self.child[i] = block[:sep]

def main():
    N = int(input())
    A = [int(i) for i in input().split()]

    # IDX[n] <-> A.index(n)
    IDX = defaultdict(int)
    for idx,a in enumerate(A):
        IDX[a] = idx

    SS = SqrtSet()
    SS.insert(-1)
    SS.insert(N)

    ans = 0
    for n in range(1, N + 1):
        H = SS.search_higher(IDX[n])
        L = SS.search_lower(IDX[n])
        ans += (IDX[n] - L) * (H - IDX[n]) *  n
        SS.insert(IDX[n])

    print(ans)

if __name__ == "__main__":
    main()

Buggy python submission (verdict: Time Limit Exceeded):
import sys
input = sys.stdin.readline
from bisect import bisect_left
from collections import defaultdict

def main():
    N = int(input())
    A = [int(i) for i in input().split()]

    # IDX[n]: nのindex
    IDX = defaultdict(int)
    for idx,a in enumerate(A):
        IDX[a] = idx

    ans = 0
    T = [-1, N]
    for n in range(1, N + 1):
        idx = bisect_left(T, IDX[n])
        T = T[:idx] + [IDX[n]] + T[idx:]
        ans += (T[idx] - T[idx-1]) * (T[idx+1] - T[idx]) * n

    print(ans)

if __name__ == '__main__':
    main()

Fixed submission:
import bisect
from collections import defaultdict
class SqrtSet:
    def __init__(self, block_limit=201):
        self.key = []
        self.child = [[]]
        self.block_limit = block_limit
 
    def search_lower(self, key):
        if key is None:
            return None
        ret = None
        i = bisect.bisect_left(self.key, key)
        if i != 0:
            ret = self.key[i - 1]
        block = self.child[i]
        i = bisect.bisect_left(block, key)
        if i != 0:
            ret = block[i - 1]
        return ret
        
    def search_higher(self, key):
        if key is None:
            return None
        ret = None
        i = bisect.bisect_right(self.key, key)
        if i != len(self.key):
            ret = self.key[i]
        block = self.child[i]
        i = bisect.bisect_right(block, key)
        if i != len(block):
            ret = block[i]
        return ret
 
    def insert(self, key):
        i = bisect.bisect(self.key, key)
        block = self.child[i]
        bisect.insort(block, key)
        if len(block) == self.block_limit:
            sep = self.block_limit // 2
            self.key.insert(i, block[sep])
            self.child.insert(i + 1, block[sep + 1:])
            self.child[i] = block[:sep]

def main():
    N = int(input())
    A = [int(i) for i in input().split()]

    # IDX[n] <-> A.index(n)
    IDX = defaultdict(int)
    for idx,a in enumerate(A):
        IDX[a] = idx

    SS = SqrtSet()
    SS.insert(-1)
    SS.insert(N)

    ans = 0
    for n in range(1, N + 1):
        H = SS.search_higher(IDX[n])
        L = SS.search_lower(IDX[n])
        ans += (IDX[n] - L) * (H - IDX[n]) *  n
        SS.insert(IDX[n])

    print(ans)

if __name__ == "__main__":
    main()

Buggy python submission (verdict: Time Limit Exceeded):
import sys
input = sys.stdin.readline
from bisect import insort_left, bisect_left
from collections import defaultdict

def main():
    N = int(input())
    A = [int(i) for i in input().split()]

    # IDX[n]: nのindex
    IDX = defaultdict(int)
    for idx,a in enumerate(A):
        IDX[a] = idx

    ans = 0
    T = [-1, N]
    for n in range(1, N + 1):
        insort_left(T, IDX[n])
        idx = bisect_left(T, IDX[n])
        ans += (T[idx] - T[idx-1]) * (T[idx+1] - T[idx]) * n

    print(ans)

if __name__ == '__main__':
    main()

Fixed submission:
import bisect
from collections import defaultdict
class SqrtSet:
    def __init__(self, block_limit=201):
        self.key = []
        self.child = [[]]
        self.block_limit = block_limit
 
    def search_lower(self, key):
        if key is None:
            return None
        ret = None
        i = bisect.bisect_left(self.key, key)
        if i != 0:
            ret = self.key[i - 1]
        block = self.child[i]
        i = bisect.bisect_left(block, key)
        if i != 0:
            ret = block[i - 1]
        return ret
        
    def search_higher(self, key):
        if key is None:
            return None
        ret = None
        i = bisect.bisect_right(self.key, key)
        if i != len(self.key):
            ret = self.key[i]
        block = self.child[i]
        i = bisect.bisect_right(block, key)
        if i != len(block):
            ret = block[i]
        return ret
 
    def insert(self, key):
        i = bisect.bisect(self.key, key)
        block = self.child[i]
        bisect.insort(block, key)
        if len(block) == self.block_limit:
            sep = self.block_limit // 2
            self.key.insert(i, block[sep])
            self.child.insert(i + 1, block[sep + 1:])
            self.child[i] = block[:sep]

def main():
    N = int(input())
    A = [int(i) for i in input().split()]

    # IDX[n] <-> A.index(n)
    IDX = defaultdict(int)
    for idx,a in enumerate(A):
        IDX[a] = idx

    SS = SqrtSet()
    SS.insert(-1)
    SS.insert(N)

    ans = 0
    for n in range(1, N + 1):
        H = SS.search_higher(IDX[n])
        L = SS.search_lower(IDX[n])
        ans += (IDX[n] - L) * (H - IDX[n]) *  n
        SS.insert(IDX[n])

    print(ans)

if __name__ == "__main__":
    main()

Buggy python submission (verdict: Time Limit Exceeded):
import sys
input = sys.stdin.readline
from bisect import insort_left, bisect_left
from collections import defaultdict

def main():
    N = int(input())
    A = [int(i) for i in input().split()]

    # IDX[n]: nのindex
    IDX = defaultdict(int)
    for idx,a in enumerate(A):
        IDX[a] = idx

    ans = 0
    T = [-1, N]
    for n in range(1, N + 1):
        insort_left(T, IDX[n])
        idx = bisect_left(T, IDX[n])
        ans += (T[idx] - T[idx-1]) * (T[idx+1] - T[idx]) * n

    print(ans)

if __name__ == '__main__':
    main()

Fixed submission:
import bisect
from collections import defaultdict
class SqrtSet:
    def __init__(self, block_limit=201):
        self.key = []
        self.child = [[]]
        self.block_limit = block_limit
 
    def search_lower(self, key):
        if key is None:
            return None
        ret = None
        i = bisect.bisect_left(self.key, key)
        if i != 0:
            ret = self.key[i - 1]
        block = self.child[i]
        i = bisect.bisect_left(block, key)
        if i != 0:
            ret = block[i - 1]
        return ret
        
    def search_higher(self, key):
        if key is None:
            return None
        ret = None
        i = bisect.bisect_right(self.key, key)
        if i != len(self.key):
            ret = self.key[i]
        block = self.child[i]
        i = bisect.bisect_right(block, key)
        if i != len(block):
            ret = block[i]
        return ret
 
    def insert(self, key):
        i = bisect.bisect(self.key, key)
        block = self.child[i]
        bisect.insort(block, key)
        if len(block) == self.block_limit:
            sep = self.block_limit // 2
            self.key.insert(i, block[sep])
            self.child.insert(i + 1, block[sep + 1:])
            self.child[i] = block[:sep]

def main():
    N = int(input())
    A = [int(i) for i in input().split()]

    # IDX[n] <-> A.index(n)
    IDX = defaultdict(int)
    for idx,a in enumerate(A):
        IDX[a] = idx

    SS = SqrtSet()
    SS.insert(-1)
    SS.insert(N)

    ans = 0
    for n in range(1, N + 1):
        H = SS.search_higher(IDX[n])
        L = SS.search_lower(IDX[n])
        ans += (IDX[n] - L) * (H - IDX[n]) *  n
        SS.insert(IDX[n])

    print(ans)

if __name__ == "__main__":
    main()

Buggy python submission (verdict: Time Limit Exceeded):
from bisect import insort_left, bisect_left
from collections import defaultdict

N = int(input())
A = [int(i) for i in input().split()]

N_IDX = defaultdict(int)
for idx,a in enumerate(A):
    N_IDX[a] = idx

ans = 0
T = [-1, N]
for n in range(1, N + 1):
    insort_left(T, N_IDX[n])
    idx = bisect_left(T, N_IDX[n])
    ans += (T[idx] - T[idx-1]) * (T[idx+1] - T[idx]) * n

print(ans)

Fixed submission:
import bisect
from collections import defaultdict
class SqrtSet:
    def __init__(self, block_limit=201):
        self.key = []
        self.child = [[]]
        self.block_limit = block_limit
 
    def search_lower(self, key):
        if key is None:
            return None
        ret = None
        i = bisect.bisect_left(self.key, key)
        if i != 0:
            ret = self.key[i - 1]
        block = self.child[i]
        i = bisect.bisect_left(block, key)
        if i != 0:
            ret = block[i - 1]
        return ret
        
    def search_higher(self, key):
        if key is None:
            return None
        ret = None
        i = bisect.bisect_right(self.key, key)
        if i != len(self.key):
            ret = self.key[i]
        block = self.child[i]
        i = bisect.bisect_right(block, key)
        if i != len(block):
            ret = block[i]
        return ret
 
    def insert(self, key):
        i = bisect.bisect(self.key, key)
        block = self.child[i]
        bisect.insort(block, key)
        if len(block) == self.block_limit:
            sep = self.block_limit // 2
            self.key.insert(i, block[sep])
            self.child.insert(i + 1, block[sep + 1:])
            self.child[i] = block[:sep]

def main():
    N = int(input())
    A = [int(i) for i in input().split()]

    # IDX[n] <-> A.index(n)
    IDX = defaultdict(int)
    for idx,a in enumerate(A):
        IDX[a] = idx

    SS = SqrtSet()
    SS.insert(-1)
    SS.insert(N)

    ans = 0
    for n in range(1, N + 1):
        H = SS.search_higher(IDX[n])
        L = SS.search_lower(IDX[n])
        ans += (IDX[n] - L) * (H - IDX[n]) *  n
        SS.insert(IDX[n])

    print(ans)

if __name__ == "__main__":
    main()