import java.util.*;
import java.io.*;

class genPerms
{

	private static BufferedReader stdin = new BufferedReader( new InputStreamReader( System.in ) );

	public static void swap(int[] perms, int i, int j)
	{
		int temp = perms[i];
		perms[i] = perms[j];
		perms[j] = temp;
	}
	
	public static void printArray(int[] data, int n)
	{
		for(int i = 0; i < n; i++)
			System.out.print(data[i]+" ");
		
		System.out.println();
	}

	public static void recursiveGenPerms(int n, int[] perms, int m)
	{

         	if (n-m == 0)
         	{
               		printArray(perms, n);
               		return;
         	}
		
		boolean[] placed = new boolean[n];
		for(int i = 0; i < n; i++)
			placed[i] = false;

        	for(int i = 0; i < n-m; i++)
        	{
			if(!placed[perms[m+i]])
			{
				placed[perms[m+i]] = true;
              			swap(perms, m, m+i);
              			recursiveGenPerms(n, perms, m+1);
              			swap(perms, m, m+i);
			}
         	}
	}


	public static void genPerms(int[] perms)
	{
		int n = perms.length;
		recursiveGenPerms(n, perms, 0);
	}

	public static void main(String[] args)
	{
       		// Prompt the user
        	System.out.println( "Type a number you want to insert. " );
			
		try{

            		// Read a line of text from the user.
            		String input = stdin.readLine();

			// converts a String into an int value
			int n = Integer.parseInt( input ); 

			int[] perms = new int[n];
			for(int i = 0; i < n; i++)
			{
            			input = stdin.readLine();
				perms[i] = Integer.parseInt( input );
			}

			genPerms(perms);

			}
                        catch(java.io.IOException e)
                        {
                                System.out.println(e);
                        }


	}
}
