import sys

n = int(sys.stdin.readline().strip())
in_order = sys.stdin.readline().strip().split()

# 栈
stack = []
result = [] 
temp = []

def dfs(in_order, stack, temp):
    # 终止条件
    if len(temp) == n:
        result.append(" ".join(temp))
    
    # 进站
    if in_order:
        stack.append(in_order[0])
        x = in_order.pop(0)
        dfs(in_order, stack, temp)
        # 回溯
        stack.pop()
        in_order.insert(0, x)

    #出站
    if stack:
        x = stack.pop()
        temp.append(x)
        dfs(in_order, stack, temp)
        # 回溯
        stack.append(x)
        temp.pop()

dfs(in_order, stack, temp)
result.sort()
for i in result:
    print(i)

真急了