#!/usr/bin/ python3.8 import sys read = sys.stdin.buffer.read readline = sys.stdin.buffer.readline readlines = sys.stdin.buffer.readlines from bisect import bisect_left import itertools INF = 2 * 10 ** 9 + 10 N = int(readline()) A = (0,) + tuple(int(x) + 10 ** 9 + 1 for x in read().split()) LIS = [0] * (N + 1) dp = [INF] * (N + 1) dp[0] = 0 for n, x in enumerate(A[1:], 1): m = bisect_left(dp, x) dp[m] = x LIS[n] = m class BinaryIndexedTree(): def __init__(self, seq): self.size = len(seq) self.depth = self.size.bit_length() self.build(seq) def build(self, seq): data = seq size = self.size for i, x in enumerate(data): j = i + (i & (-i)) if j < size: data[j] += data[i] self.data = data def __repr__(self): return self.data.__repr__() def get_sum(self, i): data = self.data s = 0 while i: s += data[i] i -= i & -i return s def add(self, i, x): data = self.data size = self.size while i < size: data[i] += x i += i & -i def find_kth_element(self, k): data = self.data size = self.size x, sx = 0, 0 dx = 1 << (self.depth) for i in range(self.depth - 1, -1, -1): dx = (1 << i) if x + dx >= size: continue y = x + dx sy = sx + data[y] if sy < k: x, sx = y, sy return x + 1 M_LIS = max(LIS) LIS_cnt = [0] * (M_LIS + 1) for i, x in enumerate(LIS): LIS_cnt[x] += 1 LIS_cnt = tuple(itertools.accumulate(LIS_cnt)) keys = [(x << 32) + y for x, y in zip(LIS, A)] s_keys = sorted(keys) key_rank = {x: i for i, x in enumerate(s_keys)} bit = BinaryIndexedTree([0] * (N + 1)) # key の値に対する数え上げを管理 answer = 0 for L, k, a in zip(LIS[1:], keys[1:], A[1:]): if L == 1: x = 1 else: r_key = k - (1 << 32) right = bisect_left(s_keys, r_key) - 1 left = LIS_cnt[L - 2] x = bit.get_sum(right) - bit.get_sum(left - 1) if L == M_LIS: answer += x else: bit.add(key_rank[k], x) print(answer)