from bisect import * INF = 10 ** 9 def lis(x): dp = [INF] * (len(x) + 1) id = [0] * len(x) for i in range(len(x)): id[i] = bisect_left(dp, x[i]) dp[bisect_left(dp, x[i])] = x[i] m = max(id) b = [0] * (m + 1) for i in range(len(x) - 1, -1, -1): if id[i] == m: b[m] = x[i] m -= 1 return bisect_left(dp, INF), b def lds(x): dp = [-INF] * (len(x) + 1) id = [0] * len(x) for i in range(len(x)): id[i] = bisect_left(dp, x[i]) - 1 dp[bisect_left(dp, x[i]) - 1] = x[i] m = min(id) b = [0] * (max(id) + 1) for i in range(len(x))[::-1]: if id[i] == m: b[m] = x[i] m += 1 return dp[::-1].index(-INF), b n = int(input()) p = list(map(int, input().split())) leng, lis_min = lis(p) _, lis_max = lds(p[::-1]) lis_max = lis_max[::-1][:leng][::-1] ans2 = [] for i in range(len(lis_min)): if lis_min[i] == lis_max[i]: ans2.append(lis_min[i]) print(len(ans2)) if len(ans2) == 0: exit(print(" ")) print(*ans2)