import sys n = int(input()) trains = input().split() allout = [] def dig(station,notin,outlist): #in if len(notin) >0: station_add = station+[notin[0]] dig(station_add, notin[1:],outlist) if len(station)>0: thisout = station.pop() outlist_add = outlist+[thisout] dig(station, notin, outlist_add) if len(outlist)==n: allout.append(outlist) dig([],trains,[]) allout = sorted(allout) for i in allout: print(' '.join(i))