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

public class utkarsh {

    BufferedReader br;
    PrintWriter out;

    long mod = (long) (1e9 + 7), inf = (long) (3e18);

    void solve() {
        int n = ni();
        int a[] = new int[n];
        for(int i = 0; i < n; i++)  a[i] = ni();
        int bit[] = new int[30];
        for(int i = 0; i < n; i++) {
            for(int j = 0; j < 30; j++) {
                bit[j] += ((a[i] >> j) & 1);
            }
        }
        int x = -1;
        for(int i = 29; i >= 0; i--)    if(bit[i] == 1) {
            x = i;  break;
        }
        if(x == -1) {
            for(int d : a)  out.print(d + " ");
        } else {
            for(int i = 0; i < n; i++) {
                if(((a[i] >> x) & 1) == 1) {
                    out.print(a[i]);
                    x = i;
                    break;
                }
            }
            for(int i = 0; i < n; i++) {
                if(i != x)  out.print(" " + a[i]);
            }
        }
    }

    long mp(long b, long e) {
        long r = 1;
        while(e > 0) {
            if( (e&1) == 1 )    r = (r * b) % mod;
            b = (b * b) % mod;
            e >>= 1;
        }
        return r;
    }

    // -------- I/O Template -------------

    char nc() {
        return ns().charAt(0);
    }

    String nLine() {
        try {
            return br.readLine();
        } catch(IOException e) {
            return "-1";
        }
    }

    double nd() {
        return Double.parseDouble(ns());
    }

    long nl() {
        return Long.parseLong(ns());
    }

    int ni() {
        return Integer.parseInt(ns());
    }

    StringTokenizer ip;

    String ns() {
        if(ip == null || !ip.hasMoreTokens()) {
            try {
                ip = new StringTokenizer(br.readLine());
            } catch(IOException e) {
                throw new InputMismatchException();
            }
        }
        return ip.nextToken();
    }

    void run() {
        br = new BufferedReader(new InputStreamReader(System.in));
        out = new PrintWriter(System.out);
        solve();
        out.flush();
    }

    public static void main(String[] args) {
        new utkarsh().run();
    }
}


import java.util.*;
import java.io.*;
import java.math.BigInteger;
 public class Solution
{
     static class Pair<A,B>{
        A parent;
        B rank;
        Pair(A parent,B rank)
        {
            this.rank=rank;
            this.parent=parent;
        }
    }
//    static int find(Pair pr[],int i)
//    {
//        if(i==pr[i].parent)
//            return i;
//        else
//        {
//            pr[i].parent=find(pr,pr[i].parent);
//            return pr[i].parent;
//        }
//    }
//    static void join(Pair[] pr,int i,int j)
//    {
//        i=find(pr,i);
//        j=find(pr,j);
//        if(i==j)
//            return;
//        if(pr[i].rank>=pr[j].rank)
//        {
//            pr[j].parent=i;
//            pr[i].rank+=pr[j].rank;
//        }
//        else
//        {
//            pr[i].parent = j;
//            pr[j].rank+=pr[i].rank;
//        }
//    }
    static void swap(char ar[],int i,int j)
    {
        char f = ar[i];
        ar[i]=ar[j];
        ar[j]=f;
    }
    static ArrayList<Integer> graph[] ;
    static TreeSet<Long> ts;
    public static void main (String[] args) throws IOException
    {
        StringBuilder sb = new StringBuilder();
        FastReader s1 = new FastReader();
        int n=s1.I();
        long ar[] = new long[n];
        for(int i=0;i<n;i++)
        {
            ar[i]=s1.L();
        }
        HashMap<Integer,ArrayList<Integer>> hm = new HashMap<>();
        for(int i=32;i>=1;i--)
        {
            hm.put(i,new ArrayList<Integer>());
        }
        for(int i=0;i<n;i++)
        {
            long temp=ar[i];
            long demo=1;
            for(int j=1;j<=32;j++)
            {
                if((demo&temp)==1)
                {
                    hm.get(j).add(i);
                }
                temp=(temp>>1);
            }
        }
        int ans=-1;
        int index=0;
        for(int k:hm.keySet())
        {
            if(hm.get(k).size()==1 && k>ans)
            {
                ans=k;
                index=hm.get(k).get(0);
            }
        }
//        if(in)
        sb.append(ar[index]+" ");
        for(int i=0;i<n;i++)
        {
            if(i!=index)
            {
                sb.append(ar[i]+" ");
            }
        }
        System.out.println(sb);
    }
    static int dayofweek(long d, int m, long y)
{
    int t[] = { 0, 3, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4 };
    y -= (m < 3) ? 1 : 0;
    return (int)( y + y/4 - y/100 + y/400 + t[(m-1)] + d) % 7;
}
        static String preprocess(String s)
    {
        StringBuilder sb = new StringBuilder();
        sb.append("@#");
        for(int i=0;i<s.length();i++)
        {
            sb.append(s.charAt(i)).append("#");
        }
        sb.append("$");
        return sb.toString();
    }
    static String LPS(String s)
    {
        String snew = preprocess(s);
        int n = snew.length();
//        System.out.println(snew);
        int c = 0,r = 0;
        int maxlen = 0;
        int index=0;
        int len[] = new int[n];
        for(int i=1;i<n-1;i++)
        {
            int mirr = c-(i-c);
            if(i<r)
            {
                len[i]=Math.min(len[mirr],r-i);
            }
      //      System.out.println(i+" ");
            while(snew.charAt(i+len[i])==snew.charAt(i-len[i]))
                len[i]++;
            if(i+len[i]>r)
            {
                c = i;
                r = i+len[i];
            }
//            System.out.println(c+" "+i+" "+snew.charAt(i)+" "+len[i]+" "+maxlen+" "+index);
            if(len[i]>maxlen)
            {
//                System.out.println("inside");
                maxlen = len[i];
                index = i;
            }
        }
        maxlen--;
        int left = (index-maxlen-1)/2;
        int right = left+maxlen;
        return s.substring(left, right);
    }
    static long phi(long n)
    {
        long result=n;
        for(int i=2;i<=Math.sqrt(n);i++)
        {
            if(n%i==0)
            {
                result-=result/i;
            }
            while(n%i==0)
            {
                n=n/i;
            }
        }
        if(n>1)
        {
            result-=(result/n);
        }
        return result;
    }
    static ArrayList<Integer> primeFactor(int n)
    {
        ArrayList<Integer> ans = new ArrayList<>();
        if(n%2==0)
            ans.add(2);
        while (n%2==0)
        {
            n /= 2;
        }
        for (int i = 3; i <= Math.sqrt(n); i+= 2)
        {
            if(n%i==0)
                ans.add(i);
            while (n%i == 0)
            {
                n /= i;
            }
        }
        if (n > 2)
            ans.add(n);
        return ans;
    }
    static int longestSubSeg(int a[], int n,int k)
    {
        int cnt0 = 0;
        int l = 0;
        int max_len = 0;
        for (int i = 0; i < n; i++) {
            if (a[i] == 0)
                cnt0++;
            while (cnt0 > k) {
                if (a[l] == 0)
                    cnt0--;
                l++;
            }
            max_len = Math.max(max_len, i - l + 1);
        }

        return max_len;
    }
    static void dfs(int index,boolean ar[])
    {
        ar[index]=true;
        for(int i=0;i<graph[index].size();i++)
        {
            if(!ar[graph[index].get(i)])
            {
                dfs(graph[index].get(i),ar);
            }
        }
    }
    static int binary(int left,int right,int ar[],int no)
    {
        int mid=(left+right)/2;
        if(Math.abs(right-left)<=1)
        {
            if(no>=ar[right])
            {
                return right+1;
            }
            else if(no<ar[left])
            {
                return left;
            }
            else
            {
                return left+1;
            }
        }
        if(ar[mid]>no)
        {
            right=mid-1;
            return binary(left, right, ar, no);
        }
        else{
            left=mid;
            return binary(left, right, ar, no);
        }
    }
    static class FastReader{
        BufferedReader br;
        StringTokenizer st;
        public FastReader()
        {
            br = new BufferedReader(new
                             InputStreamReader(System.in));
        }
        String next()
        {
            while (st == null || !st.hasMoreElements())
            {
                try
                {
                    st = new StringTokenizer(br.readLine());
                }
                catch (IOException  e)
                {
                    e.printStackTrace();
                }
            }
            return st.nextToken();
        }

        int I()
        {
            return Integer.parseInt(next());
        }

        long L()
        {
            return Long.parseLong(next());
        }

        double D()
        {
            return Double.parseDouble(next());
        }

        String nextLine()
        {
            String str = "";
            try
            {
                str = br.readLine();
            }
            catch (IOException e)
            {
                e.printStackTrace();
            }
            return str;
        }
    }
    static long gcd(long a,long b)
    {
        if(a%b==0)
            return b;
        return gcd(b,a%b);
    }
    static float power(float x, int y)
    {
        float temp;
        if( y == 0)
            return 1;
        temp = power(x, y/2);

        if (y%2 == 0)
            return temp*temp;
        else
        {
            if(y > 0)
                return x * temp * temp;
            else
                return (temp * temp) / x;
        }
    }
    static long pow(long x, long y)
    {
        int p = 1000000007;
        long res = 1;
        x = x % p;
        while (y > 0)
        {
            if((y & 1)==1)
                res = (res * x) % p;
            y = y >> 1;
            x = (x * x) % p;
        }
        return res;
    }
    static ArrayList<Integer> sieveOfEratosthenes(int n)
    {
        ArrayList<Integer> arr=new ArrayList<Integer>();
        boolean prime[] = new boolean[n+1];
        for(int i=2;i<n;i++)
            prime[i] = true;

        for(int p = 2; p*p <=n; p++)
        {
            if(prime[p] == true)
            {
                arr.add(p);
                for(int i = p*p; i <= n; i += p)
                    prime[i] = false;
            }
        }
        return arr;
    }
}


import java.util.*;
import java.io.*;
import java.math.BigInteger;
 public class Solution
{
     static class Pair<A,B>{
        A parent;
        B rank;
        Pair(A parent,B rank)
        {
            this.rank=rank;
            this.parent=parent;
        }
    }
//    static int find(Pair pr[],int i)
//    {
//        if(i==pr[i].parent)
//            return i;
//        else
//        {
//            pr[i].parent=find(pr,pr[i].parent);
//            return pr[i].parent;
//        }
//    }
//    static void join(Pair[] pr,int i,int j)
//    {
//        i=find(pr,i);
//        j=find(pr,j);
//        if(i==j)
//            return;
//        if(pr[i].rank>=pr[j].rank)
//        {
//            pr[j].parent=i;
//            pr[i].rank+=pr[j].rank;
//        }
//        else
//        {
//            pr[i].parent = j;
//            pr[j].rank+=pr[i].rank;
//        }
//    }
    static void swap(char ar[],int i,int j)
    {
        char f = ar[i];
        ar[i]=ar[j];
        ar[j]=f;
    }
    static ArrayList<Integer> graph[] ;
    static TreeSet<Long> ts;
    public static void main (String[] args) throws IOException
    {
        StringBuilder sb = new StringBuilder();
        FastReader s1 = new FastReader();
        int n=s1.I();
        long ar[] = new long[n];
        for(int i=0;i<n;i++)
        {
            ar[i]=s1.L();
        }
        HashMap<Integer,ArrayList<Integer>> hm = new HashMap<>();
        for(int i=32;i>=1;i--)
        {
            hm.put(i,new ArrayList<Integer>());
        }
        for(int i=0;i<n;i++)
        {
            long temp=ar[i];
            long demo=1;
            for(int j=1;j<=32;j++)
            {
                if((demo&temp)==1)
                {
                    hm.get(j).add(i);
                }
                temp=(temp>>1);
            }
        }
        int ans=-1;
        int index=-1;
        for(int k:hm.keySet())
        {
            if(hm.get(k).size()==1 && k>ans)
            {
                ans=k;
                index=hm.get(k).get(0);
            }
        }
        sb.append(ar[index]+" ");
        for(int i=0;i<n;i++)
        {
            if(i!=index)
            {
                sb.append(ar[i]+" ");
            }
        }
        System.out.println(sb);
    }
    static int dayofweek(long d, int m, long y)
{
    int t[] = { 0, 3, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4 };
    y -= (m < 3) ? 1 : 0;
    return (int)( y + y/4 - y/100 + y/400 + t[(m-1)] + d) % 7;
}
        static String preprocess(String s)
    {
        StringBuilder sb = new StringBuilder();
        sb.append("@#");
        for(int i=0;i<s.length();i++)
        {
            sb.append(s.charAt(i)).append("#");
        }
        sb.append("$");
        return sb.toString();
    }
    static String LPS(String s)
    {
        String snew = preprocess(s);
        int n = snew.length();
//        System.out.println(snew);
        int c = 0,r = 0;
        int maxlen = 0;
        int index=0;
        int len[] = new int[n];
        for(int i=1;i<n-1;i++)
        {
            int mirr = c-(i-c);
            if(i<r)
            {
                len[i]=Math.min(len[mirr],r-i);
            }
      //      System.out.println(i+" ");
            while(snew.charAt(i+len[i])==snew.charAt(i-len[i]))
                len[i]++;
            if(i+len[i]>r)
            {
                c = i;
                r = i+len[i];
            }
//            System.out.println(c+" "+i+" "+snew.charAt(i)+" "+len[i]+" "+maxlen+" "+index);
            if(len[i]>maxlen)
            {
//                System.out.println("inside");
                maxlen = len[i];
                index = i;
            }
        }
        maxlen--;
        int left = (index-maxlen-1)/2;
        int right = left+maxlen;
        return s.substring(left, right);
    }
    static long phi(long n)
    {
        long result=n;
        for(int i=2;i<=Math.sqrt(n);i++)
        {
            if(n%i==0)
            {
                result-=result/i;
            }
            while(n%i==0)
            {
                n=n/i;
            }
        }
        if(n>1)
        {
            result-=(result/n);
        }
        return result;
    }
    static ArrayList<Integer> primeFactor(int n)
    {
        ArrayList<Integer> ans = new ArrayList<>();
        if(n%2==0)
            ans.add(2);
        while (n%2==0)
        {
            n /= 2;
        }
        for (int i = 3; i <= Math.sqrt(n); i+= 2)
        {
            if(n%i==0)
                ans.add(i);
            while (n%i == 0)
            {
                n /= i;
            }
        }
        if (n > 2)
            ans.add(n);
        return ans;
    }
    static int longestSubSeg(int a[], int n,int k)
    {
        int cnt0 = 0;
        int l = 0;
        int max_len = 0;
        for (int i = 0; i < n; i++) {
            if (a[i] == 0)
                cnt0++;
            while (cnt0 > k) {
                if (a[l] == 0)
                    cnt0--;
                l++;
            }
            max_len = Math.max(max_len, i - l + 1);
        }

        return max_len;
    }
    static void dfs(int index,boolean ar[])
    {
        ar[index]=true;
        for(int i=0;i<graph[index].size();i++)
        {
            if(!ar[graph[index].get(i)])
            {
                dfs(graph[index].get(i),ar);
            }
        }
    }
    static int binary(int left,int right,int ar[],int no)
    {
        int mid=(left+right)/2;
        if(Math.abs(right-left)<=1)
        {
            if(no>=ar[right])
            {
                return right+1;
            }
            else if(no<ar[left])
            {
                return left;
            }
            else
            {
                return left+1;
            }
        }
        if(ar[mid]>no)
        {
            right=mid-1;
            return binary(left, right, ar, no);
        }
        else{
            left=mid;
            return binary(left, right, ar, no);
        }
    }
    static class FastReader{
        BufferedReader br;
        StringTokenizer st;
        public FastReader()
        {
            br = new BufferedReader(new
                             InputStreamReader(System.in));
        }
        String next()
        {
            while (st == null || !st.hasMoreElements())
            {
                try
                {
                    st = new StringTokenizer(br.readLine());
                }
                catch (IOException  e)
                {
                    e.printStackTrace();
                }
            }
            return st.nextToken();
        }

        int I()
        {
            return Integer.parseInt(next());
        }

        long L()
        {
            return Long.parseLong(next());
        }

        double D()
        {
            return Double.parseDouble(next());
        }

        String nextLine()
        {
            String str = "";
            try
            {
                str = br.readLine();
            }
            catch (IOException e)
            {
                e.printStackTrace();
            }
            return str;
        }
    }
    static long gcd(long a,long b)
    {
        if(a%b==0)
            return b;
        return gcd(b,a%b);
    }
    static float power(float x, int y)
    {
        float temp;
        if( y == 0)
            return 1;
        temp = power(x, y/2);

        if (y%2 == 0)
            return temp*temp;
        else
        {
            if(y > 0)
                return x * temp * temp;
            else
                return (temp * temp) / x;
        }
    }
    static long pow(long x, long y)
    {
        int p = 1000000007;
        long res = 1;
        x = x % p;
        while (y > 0)
        {
            if((y & 1)==1)
                res = (res * x) % p;
            y = y >> 1;
            x = (x * x) % p;
        }
        return res;
    }
    static ArrayList<Integer> sieveOfEratosthenes(int n)
    {
        ArrayList<Integer> arr=new ArrayList<Integer>();
        boolean prime[] = new boolean[n+1];
        for(int i=2;i<n;i++)
            prime[i] = true;

        for(int p = 2; p*p <=n; p++)
        {
            if(prime[p] == true)
            {
                arr.add(p);
                for(int i = p*p; i <= n; i += p)
                    prime[i] = false;
            }
        }
        return arr;
    }
}


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

public class Main {
    void solve(){
        int n=ni();
        int a[]=new int[n+1];
        for(int i=1;i<=n;i++) a[i]=ni();
        for(int b=30;b>=0;b--){
            int cnt=0,idx=-1;
            for(int i=1;i<=n;i++){
                if((a[i]&(1<<b))==1){
                    idx=i;
                    cnt++;
                }
            }
            if(cnt==1){
                int tmp=a[1]; a[1]=a[idx]; a[idx]=tmp;
                break;
            }
        }
        for(int i=1;i<=n;i++) pw.print(a[i]+" ");
        pw.println("");

    }

    long M = (long) 1e9 + 7;
    InputStream is;
    PrintWriter pw;
    String INPUT = "";

    void run() throws Exception {
        is = INPUT.isEmpty() ? System.in : new ByteArrayInputStream(INPUT.getBytes());
        pw = new PrintWriter(System.out);
        long s = System.currentTimeMillis();
        solve();
        pw.flush();
        if (!INPUT.isEmpty()) tr(System.currentTimeMillis() - s + "ms");

    }
    public static void main(String[] args) throws Exception {
        new Main().run();
    }

    private byte[] inbuf = new byte[1024];
    public int lenbuf = 0, ptrbuf = 0;

    private int readByte() {
        if (lenbuf == -1) throw new InputMismatchException();
        if (ptrbuf >= lenbuf) {
            ptrbuf = 0;
            try {
                lenbuf = is.read(inbuf);
            } catch (IOException e) {
                throw new InputMismatchException();
            }
            if (lenbuf <= 0) return -1;
        }
        return inbuf[ptrbuf++];
    }

    private boolean isSpaceChar(int c) {
        return !(c >= 33 && c <= 126);
    }

    private int skip() {
        int b;
        while ((b = readByte()) != -1 && isSpaceChar(b)) ;
        return b;
    }

    private double nd() {
        return Double.parseDouble(ns());
    }

    private char nc() {
        return (char) skip();
    }

    private String ns() {
        int b = skip();
        StringBuilder sb = new StringBuilder();
        while (!(isSpaceChar(b))) { // when nextLine, (isSpaceChar(b) && b != ' ')
            sb.appendCodePoint(b);
            b = readByte();
        }
        return sb.toString();
    }

    private char[] ns(int n) {
        char[] buf = new char[n];
        int b = skip(), p = 0;
        while (p < n && !(isSpaceChar(b))) {
            buf[p++] = (char) b;
            b = readByte();
        }
        return n == p ? buf : Arrays.copyOf(buf, p);
    }

    private char[][] nm(int n, int m) {
        char[][] map = new char[n][];
        for (int i = 0; i < n; i++) map[i] = ns(m);
        return map;
    }

    private int[] na(int n) {
        int[] a = new int[n];
        for (int i = 0; i < n; i++) a[i] = ni();
        return a;
    }

    private int ni() {
        int num = 0, b;
        boolean minus = false;
        while ((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')) ;
        if (b == '-') {
            minus = true;
            b = readByte();
        }

        while (true) {
            if (b >= '0' && b <= '9') {
                num = num * 10 + (b - '0');
            } else {
                return minus ? -num : num;
            }
            b = readByte();
        }
    }

    private long nl() {
        long num = 0;
        int b;
        boolean minus = false;
        while ((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')) ;
        if (b == '-') {
            minus = true;
            b = readByte();
        }

        while (true) {
            if (b >= '0' && b <= '9') {
                num = num * 10 + (b - '0');
            } else {
                return minus ? -num : num;
            }
            b = readByte();
        }
    }

    private boolean oj = System.getProperty("ONLINE_JUDGE") != null;

    private void tr(Object... o) {
        if (INPUT.length() > 0) System.out.println(Arrays.deepToString(o));
    }
}


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

public class Main {
    void solve(){
        int n=ni();
        long a[]=new long[n+1];
        for(int i=1;i<=n;i++) a[i]=ni();
        for(int b=31;b>=0;b--){
            int cnt=0,idx=-1;
            for(int i=1;i<=n;i++){
                if((a[i]&(1l<<b))==1){
                    idx=i;
                    cnt++;
                }
            }
            if(cnt==1){
                long tmp=a[1]; a[1]=a[idx]; a[idx]=tmp;
                break;
            }
        }
        for(int i=1;i<=n;i++) pw.print(a[i]+" ");
        pw.println("");

    }

    long M = (long) 1e9 + 7;
    InputStream is;
    PrintWriter pw;
    String INPUT = "";

    void run() throws Exception {
        is = INPUT.isEmpty() ? System.in : new ByteArrayInputStream(INPUT.getBytes());
        pw = new PrintWriter(System.out);
        long s = System.currentTimeMillis();
        solve();
        pw.flush();
        if (!INPUT.isEmpty()) tr(System.currentTimeMillis() - s + "ms");

    }
    public static void main(String[] args) throws Exception {
        new Main().run();
    }

    private byte[] inbuf = new byte[1024];
    public int lenbuf = 0, ptrbuf = 0;

    private int readByte() {
        if (lenbuf == -1) throw new InputMismatchException();
        if (ptrbuf >= lenbuf) {
            ptrbuf = 0;
            try {
                lenbuf = is.read(inbuf);
            } catch (IOException e) {
                throw new InputMismatchException();
            }
            if (lenbuf <= 0) return -1;
        }
        return inbuf[ptrbuf++];
    }

    private boolean isSpaceChar(int c) {
        return !(c >= 33 && c <= 126);
    }

    private int skip() {
        int b;
        while ((b = readByte()) != -1 && isSpaceChar(b)) ;
        return b;
    }

    private double nd() {
        return Double.parseDouble(ns());
    }

    private char nc() {
        return (char) skip();
    }

    private String ns() {
        int b = skip();
        StringBuilder sb = new StringBuilder();
        while (!(isSpaceChar(b))) { // when nextLine, (isSpaceChar(b) && b != ' ')
            sb.appendCodePoint(b);
            b = readByte();
        }
        return sb.toString();
    }

    private char[] ns(int n) {
        char[] buf = new char[n];
        int b = skip(), p = 0;
        while (p < n && !(isSpaceChar(b))) {
            buf[p++] = (char) b;
            b = readByte();
        }
        return n == p ? buf : Arrays.copyOf(buf, p);
    }

    private char[][] nm(int n, int m) {
        char[][] map = new char[n][];
        for (int i = 0; i < n; i++) map[i] = ns(m);
        return map;
    }

    private int[] na(int n) {
        int[] a = new int[n];
        for (int i = 0; i < n; i++) a[i] = ni();
        return a;
    }

    private int ni() {
        int num = 0, b;
        boolean minus = false;
        while ((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')) ;
        if (b == '-') {
            minus = true;
            b = readByte();
        }

        while (true) {
            if (b >= '0' && b <= '9') {
                num = num * 10 + (b - '0');
            } else {
                return minus ? -num : num;
            }
            b = readByte();
        }
    }

    private long nl() {
        long num = 0;
        int b;
        boolean minus = false;
        while ((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')) ;
        if (b == '-') {
            minus = true;
            b = readByte();
        }

        while (true) {
            if (b >= '0' && b <= '9') {
                num = num * 10 + (b - '0');
            } else {
                return minus ? -num : num;
            }
            b = readByte();
        }
    }

    private boolean oj = System.getProperty("ONLINE_JUDGE") != null;

    private void tr(Object... o) {
        if (INPUT.length() > 0) System.out.println(Arrays.deepToString(o));
    }
}


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

public class Main {
    void solve(){
        int n=ni();
        long a[]=new long[n+1];
        for(int i=1;i<=n;i++) a[i]=ni();
        for(int b=31;b>=0;b--){
            int cnt=0,idx=-1;
            for(int i=1;i<=n;i++){
                if(((a[i]>>b)&1)==1){
                    idx=i;
                    cnt++;
                }
            }
            if(cnt==1){
                //pw.println(b+" "+idx);
                long tmp=a[1]; a[1]=a[idx]; a[idx]=tmp;
                break;
            }
        }
        for(int i=1;i<=n;i++) pw.print(a[i]+" ");
        pw.println("");

    }

    long M = (long) 1e9 + 7;
    InputStream is;
    PrintWriter pw;
    String INPUT = "";

    void run() throws Exception {
        is = INPUT.isEmpty() ? System.in : new ByteArrayInputStream(INPUT.getBytes());
        pw = new PrintWriter(System.out);
        long s = System.currentTimeMillis();
        solve();
        pw.flush();
        if (!INPUT.isEmpty()) tr(System.currentTimeMillis() - s + "ms");

    }
    public static void main(String[] args) throws Exception {
        new Main().run();
    }

    private byte[] inbuf = new byte[1024];
    public int lenbuf = 0, ptrbuf = 0;

    private int readByte() {
        if (lenbuf == -1) throw new InputMismatchException();
        if (ptrbuf >= lenbuf) {
            ptrbuf = 0;
            try {
                lenbuf = is.read(inbuf);
            } catch (IOException e) {
                throw new InputMismatchException();
            }
            if (lenbuf <= 0) return -1;
        }
        return inbuf[ptrbuf++];
    }

    private boolean isSpaceChar(int c) {
        return !(c >= 33 && c <= 126);
    }

    private int skip() {
        int b;
        while ((b = readByte()) != -1 && isSpaceChar(b)) ;
        return b;
    }

    private double nd() {
        return Double.parseDouble(ns());
    }

    private char nc() {
        return (char) skip();
    }

    private String ns() {
        int b = skip();
        StringBuilder sb = new StringBuilder();
        while (!(isSpaceChar(b))) { // when nextLine, (isSpaceChar(b) && b != ' ')
            sb.appendCodePoint(b);
            b = readByte();
        }
        return sb.toString();
    }

    private char[] ns(int n) {
        char[] buf = new char[n];
        int b = skip(), p = 0;
        while (p < n && !(isSpaceChar(b))) {
            buf[p++] = (char) b;
            b = readByte();
        }
        return n == p ? buf : Arrays.copyOf(buf, p);
    }

    private char[][] nm(int n, int m) {
        char[][] map = new char[n][];
        for (int i = 0; i < n; i++) map[i] = ns(m);
        return map;
    }

    private int[] na(int n) {
        int[] a = new int[n];
        for (int i = 0; i < n; i++) a[i] = ni();
        return a;
    }

    private int ni() {
        int num = 0, b;
        boolean minus = false;
        while ((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')) ;
        if (b == '-') {
            minus = true;
            b = readByte();
        }

        while (true) {
            if (b >= '0' && b <= '9') {
                num = num * 10 + (b - '0');
            } else {
                return minus ? -num : num;
            }
            b = readByte();
        }
    }

    private long nl() {
        long num = 0;
        int b;
        boolean minus = false;
        while ((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')) ;
        if (b == '-') {
            minus = true;
            b = readByte();
        }

        while (true) {
            if (b >= '0' && b <= '9') {
                num = num * 10 + (b - '0');
            } else {
                return minus ? -num : num;
            }
            b = readByte();
        }
    }

    private boolean oj = System.getProperty("ONLINE_JUDGE") != null;

    private void tr(Object... o) {
        if (INPUT.length() > 0) System.out.println(Arrays.deepToString(o));
    }
}


import javax.swing.*;
import java.util.*;
import java.io.*;
import static java.lang.Math.*;

public class C
{
    /*
     * This FastReader code is taken from GeeksForGeeks.com
     * https://www.geeksforgeeks.org/fast-io-in-java-in-competitive-programming/
     *
     * The article was written by Rishabh Mahrsee
     */
    public static class FastReader
    {
        BufferedReader br;
        StringTokenizer st;

        public FastReader()
        {
            br = new BufferedReader(new
                    InputStreamReader(System.in));
        }

        String next()
        {
            while (st == null || !st.hasMoreElements())
            {
                try
                {
                    st = new StringTokenizer(br.readLine());
                }
                catch (IOException  e)
                {
                    e.printStackTrace();
                }
            }
            return st.nextToken();
        }

        int nextInt()
        {
            return Integer.parseInt(next());
        }

        long nextLong()
        {
            return Long.parseLong(next());
        }

        double nextDouble()
        {
            return Double.parseDouble(next());
        }

        String nextLine()
        {
            String str = "";
            try
            {
                str = br.readLine();
            }
            catch (IOException e)
            {
                e.printStackTrace();
            }
            return str;
        }
    }

    public static FastReader in = new FastReader();
    public static PrintWriter out = new PrintWriter(System.out);
    public static final int MOD = (int)1e9 + 7;

    public static void main(String[] args)
    {
        int n = in.nextInt();
        int[] arr = new int[n];
        int[] counts = new int[32];

        for (int i = 0; i < n; i++)
        {
            arr[i] = in.nextInt();
            int next = ~arr[i];

            for (int bitNum = 0; next != 0; bitNum++)
            {
                counts[bitNum] += (next & 1) != 0 ? 1 : 0;
                next >>>= 1;
            }
        }
        int firstBit = -1;
        for (int i = counts.length - 1; i >= 0; i--)
        {
            if (counts[i] == n - 1)
            {
                firstBit = i;
                break;
            }
        }

        if (firstBit == -1)
        {
            for (int i : arr) out.print(i + " ");
            out.println();
        }
        else
        {
            long min = 1l << firstBit;
            int indexLookingFor = 0;
            for (int i = 0; i < n; i++)
            {
                if (arr[i] >= min)
                {
                    indexLookingFor = i;
                    break;
                }
            }

            out.print(arr[indexLookingFor] + " ");

            for (int i = 0; i < n; i++)
            {
                if (i == indexLookingFor)
                    continue;
                out.print(arr[i] + " ");
            }
        }

        //out.println(Arrays.toString(counts));
        out.close();
    }

}


import javax.swing.*;
import java.util.*;
import java.io.*;
import static java.lang.Math.*;

public class C
{
    /*
     * This FastReader code is taken from GeeksForGeeks.com
     * https://www.geeksforgeeks.org/fast-io-in-java-in-competitive-programming/
     *
     * The article was written by Rishabh Mahrsee
     */
    public static class FastReader
    {
        BufferedReader br;
        StringTokenizer st;

        public FastReader()
        {
            br = new BufferedReader(new
                    InputStreamReader(System.in));
        }

        String next()
        {
            while (st == null || !st.hasMoreElements())
            {
                try
                {
                    st = new StringTokenizer(br.readLine());
                }
                catch (IOException  e)
                {
                    e.printStackTrace();
                }
            }
            return st.nextToken();
        }

        int nextInt()
        {
            return Integer.parseInt(next());
        }

        long nextLong()
        {
            return Long.parseLong(next());
        }

        double nextDouble()
        {
            return Double.parseDouble(next());
        }

        String nextLine()
        {
            String str = "";
            try
            {
                str = br.readLine();
            }
            catch (IOException e)
            {
                e.printStackTrace();
            }
            return str;
        }
    }

    public static FastReader in = new FastReader();
    public static PrintWriter out = new PrintWriter(System.out);
    public static final int MOD = (int)1e9 + 7;

    public static void main(String[] args)
    {
        int n = in.nextInt();
        int[] arr = new int[n];
        int[] pref = new int[n];
        int[] suff = new int[n];

        for (int i = 0; i < n; i++)
            arr[i] = in.nextInt();

        pref[0] = ~arr[0];
        suff[n - 1] = ~arr[n - 1];

        for (int i = 1; i < n; i++)
        {
            pref[i] = ~arr[i] & pref[i - 1];
        }

        for (int i = n - 2; i >= 0; i--)
        {
            suff[i] = ~arr[i] & suff[i + 1];
        }

        int maxIndex = -1;
        int max = Integer.MIN_VALUE;

        for (int i = 0; i < n; i++)
        {
            int cur = (i - 1 >= 0 ? pref[i - 1] : ~0) & (i + 1 < n ? suff[i + 1] : ~0) & arr[i];

            if (cur > max)
            {
                max = cur;
                maxIndex = i;
            }
        }

        swap(arr, 0, maxIndex);

        for (int i : arr) out.print(i + " ");

        out.println();
        out.close();
    }

    private static void swap(int[] arr, int a, int b)
    {
        int temp = arr[a];
        arr[a] = arr[b];
        arr[b] = temp;
    }

}


import javax.swing.*;
import java.util.*;
import java.io.*;
import static java.lang.Math.*;

public class C
{
    /*
     * This FastReader code is taken from GeeksForGeeks.com
     * https://www.geeksforgeeks.org/fast-io-in-java-in-competitive-programming/
     *
     * The article was written by Rishabh Mahrsee
     */
    public static class FastReader
    {
        BufferedReader br;
        StringTokenizer st;

        public FastReader()
        {
            br = new BufferedReader(new
                    InputStreamReader(System.in));
        }

        String next()
        {
            while (st == null || !st.hasMoreElements())
            {
                try
                {
                    st = new StringTokenizer(br.readLine());
                }
                catch (IOException  e)
                {
                    e.printStackTrace();
                }
            }
            return st.nextToken();
        }

        int nextInt()
        {
            return Integer.parseInt(next());
        }

        long nextLong()
        {
            return Long.parseLong(next());
        }

        double nextDouble()
        {
            return Double.parseDouble(next());
        }

        String nextLine()
        {
            String str = "";
            try
            {
                str = br.readLine();
            }
            catch (IOException e)
            {
                e.printStackTrace();
            }
            return str;
        }
    }

    public static FastReader in = new FastReader();
    public static PrintWriter out = new PrintWriter(System.out);
    public static final int MOD = (int)1e9 + 7;

    public static void main(String[] args)
    {
        int n = in.nextInt();
        int[] arr = new int[n];
        int[] counts = new int[32];

        for (int i = 0; i < n; i++)
        {
            arr[i] = in.nextInt();
            int next = ~arr[i];

            for (int bitNum = 0; next != 0; bitNum++)
            {
                counts[bitNum] += (next & 1) != 0 ? 1 : 0;
                next >>>= 1;
            }
        }
        int firstBit = -1;
        for (int i = counts.length - 1; i >= 0; i--)
        {
            if (counts[i] == n - 1)
            {
                firstBit = i;
                break;
            }
        }

        if (firstBit == -1)
        {
            for (int i : arr) out.print(i + " ");
            out.println();
        }
        else
        {
            long mask = 1l << firstBit;
            int indexLookingFor = 0;
            for (int i = 0; i < n; i++)
            {
                if (((long)arr[i] & mask) != 0)
                {
                    indexLookingFor = i;
                    break;
                }
            }

            out.print(arr[indexLookingFor] + " ");

            for (int i = 0; i < n; i++)
            {
                if (i == indexLookingFor)
                    continue;
                out.print(arr[i] + " ");
            }
        }

        //out.println(Arrays.toString(counts));
        out.close();
    }

}


import java.util.*;

import java.io.*;

public class K {

	static int n, k, x;
	static long memo[][];
	static int[] a;

	static long dp(int s, int l) {
		if (s == x) {
			if (n - l - 1 >= k) {
				return (long) (-1 * 1e16);
			}
			return 0;
		}
		if (l == n - 1)
			return (long) (-1 * 1e16);

		if (memo[s][l] != -2)
			return memo[s][l];
		long max = (long) (-1 * 1e16);
		for (int i = l + 1; i <= l + k && i < n; i++) {
			max = Math.max(max, dp(s + 1, i) + a[i]);
		}
		return memo[s][l] = max;
	}

	public static void main(String[] args) throws IOException {
		// TODO Auto-generated method stub
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		PrintWriter pw = new PrintWriter(System.out);
		int n = Integer.parseInt(br.readLine());
		int[] a = new int[n];
		StringTokenizer st = new StringTokenizer(br.readLine());
		int max = 0;
		int ans = -1;
		for (int i = 0; i < a.length; i++) {
			a[i] = Integer.parseInt(st.nextToken());
			if (Integer.bitCount(a[i]) > max) {
				max = Integer.bitCount(a[i]);
				ans = a[i];
			}
		}
		boolean f = true;
		pw.print(ans);
		for (int i = 0; i < a.length; i++) {
			if (f) {
				if (a[i] == ans)
					f = false;
				else
					pw.print(" "+a[i]);
			} else
				pw.print(" " + a[i]);
		}
		pw.flush();

	}

	static class pair implements Comparable<pair> {
		int x;
		int y;

		public pair(int d, int l) {
			x = d;
			y = l;
		}

		@Override
		public int compareTo(pair arg0) {
			// TODO Auto-generated method stub
			if (x == arg0.x) {
				return (int) (y - arg0.y);
			}
			return x - arg0.x;
		}
	}

}


import java.util.*;

import java.io.*;

public class K {

	static int n, k, x;
	static long memo[][];
	static int[] a;

	static long dp(int s, int l) {
		if (s == x) {
			if (n - l - 1 >= k) {
				return (long) (-1 * 1e16);
			}
			return 0;
		}
		if (l == n - 1)
			return (long) (-1 * 1e16);

		if (memo[s][l] != -2)
			return memo[s][l];
		long max = (long) (-1 * 1e16);
		for (int i = l + 1; i <= l + k && i < n; i++) {
			max = Math.max(max, dp(s + 1, i) + a[i]);
		}
		return memo[s][l] = max;
	}

	public static void main(String[] args) throws IOException {
		// TODO Auto-generated method stub
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		PrintWriter pw = new PrintWriter(System.out);
		int n = Integer.parseInt(br.readLine());
		int[] a = new int[n];
		StringTokenizer st = new StringTokenizer(br.readLine());
		int max = 0;
		int ans = -1;
		HashMap<Integer, Integer> hm = new HashMap<>();
		for (int i = 0; i < a.length; i++) {
			a[i] = Integer.parseInt(st.nextToken());
			hm.put(a[i], hm.getOrDefault(a[i], 0) + 1);

		}
		for (int i = 0; i < a.length; i++) {
			if (hm.get(a[i]) == 1) {
				if (Integer.bitCount(a[i]) > max) {
					max = Integer.bitCount(a[i]);
					ans = a[i];
				}
			}
		}
		boolean f = true;
		if (ans != -1)
			pw.print(ans + " ");
		for (int i = 0; i < a.length; i++) {
			if (f) {
				if (a[i] == ans)
					f = false;
				else
					pw.print(a[i] + " ");
			} else
				pw.print(a[i] + " ");
		}
		pw.flush();

	}

	static class pair implements Comparable<pair> {
		int x;
		int y;

		public pair(int d, int l) {
			x = d;
			y = l;
		}

		@Override
		public int compareTo(pair arg0) {
			// TODO Auto-generated method stub
			if (x == arg0.x) {
				return (int) (y - arg0.y);
			}
			return x - arg0.x;
		}
	}

}


import java.util.*;

import java.io.*;

public class K {

	static int n, k, x;
	static long memo[][];
	static int[] a;

	static long dp(int s, int l) {
		if (s == x) {
			if (n - l - 1 >= k) {
				return (long) (-1 * 1e16);
			}
			return 0;
		}
		if (l == n - 1)
			return (long) (-1 * 1e16);

		if (memo[s][l] != -2)
			return memo[s][l];
		long max = (long) (-1 * 1e16);
		for (int i = l + 1; i <= l + k && i < n; i++) {
			max = Math.max(max, dp(s + 1, i) + a[i]);
		}
		return memo[s][l] = max;
	}

	public static void main(String[] args) throws IOException {
		// TODO Auto-generated method stub
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		PrintWriter pw = new PrintWriter(System.out);
		int n = Integer.parseInt(br.readLine());
		int[] a = new int[n];
		int[] b = new int[31];
		StringTokenizer st = new StringTokenizer(br.readLine());
		int max = 0;
		int ans = -1;
		HashMap<Integer, Integer> hm = new HashMap<>();
		for (int i = 0; i < a.length; i++) {
			a[i] = Integer.parseInt(st.nextToken());
			for (int j = 0; j < b.length; j++) {
				if (((a[i] >> j) & 1) == 1)
					b[j]++;
			}

		}
		for (int i = 0; i < a.length; i++) {
			int c = 0;
			for (int j = 0; j < b.length; j++) {
				if (((a[i] >> j) & 1) == 1 && b[j] == 1)
					c |= 1 << j;
			}
			if (c > max) {
				max = c;
				ans = a[i];
			}
		}
		boolean f = true;
		if (ans != -1)
			pw.print(ans + " ");
		for (int i = 0; i < a.length; i++) {
			if (f) {
				if (a[i] == ans)
					f = false;
				else
					pw.print(a[i] + " ");
			} else
				pw.print(a[i] + " ");
		}
		pw.flush();

	}

	static class pair implements Comparable<pair> {
		int x;
		int y;

		public pair(int d, int l) {
			x = d;
			y = l;
		}

		@Override
		public int compareTo(pair arg0) {
			// TODO Auto-generated method stub
			if (x == arg0.x) {
				return (int) (y - arg0.y);
			}
			return x - arg0.x;
		}
	}

}


import java.util.*;

import java.io.*;

public class K {

	static int n, k, x;
	static long memo[][];
	static int[] a;

	static long dp(int s, int l) {
		if (s == x) {
			if (n - l - 1 >= k) {
				return (long) (-1 * 1e16);
			}
			return 0;
		}
		if (l == n - 1)
			return (long) (-1 * 1e16);

		if (memo[s][l] != -2)
			return memo[s][l];
		long max = (long) (-1 * 1e16);
		for (int i = l + 1; i <= l + k && i < n; i++) {
			max = Math.max(max, dp(s + 1, i) + a[i]);
		}
		return memo[s][l] = max;
	}

	public static void main(String[] args) throws IOException {
		// TODO Auto-generated method stub
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		PrintWriter pw = new PrintWriter(System.out);
		int n = Integer.parseInt(br.readLine());
		int[] a = new int[n];
		StringTokenizer st = new StringTokenizer(br.readLine());
		int max = 0;
		int ans = -1;
		HashMap<Integer,Integer> hm=new HashMap<>();
		for (int i = 0; i < a.length; i++) {
			a[i] = Integer.parseInt(st.nextToken());
			hm.put(a[i],hm.getOrDefault(a[i], 0)+1);

		}
		for (int i = 0; i < a.length; i++) {
			if(hm.get(a[i])==1) {
				if (Integer.bitCount(a[i]) > max) {
					max = Integer.bitCount(a[i]);
					ans = a[i];
				}
			}
		}
		boolean f = true;
		pw.print(ans);
		for (int i = 0; i < a.length; i++) {
			if (f) {
				if (a[i] == ans)
					f = false;
				else
					pw.print(" "+a[i]);
			} else
				pw.print(" " + a[i]);
		}
		pw.flush();

	}

	static class pair implements Comparable<pair> {
		int x;
		int y;

		public pair(int d, int l) {
			x = d;
			y = l;
		}

		@Override
		public int compareTo(pair arg0) {
			// TODO Auto-generated method stub
			if (x == arg0.x) {
				return (int) (y - arg0.y);
			}
			return x - arg0.x;
		}
	}

}


import java.util.Arrays;
import java.util.Scanner;

public class C_1300 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner s = new Scanner(System.in);
		int n = s.nextInt();
		int[] a = new int[n];
		int[] b = new int[32];
		int max = 0,ff=0;
		for (int i = 0; i < n; i++) {
           a[i] = s.nextInt();
		}
		for(int  i=0;i<n;i++) {
			for(int j=0;j<b.length;j++) {
				if(((a[i] >> j) & 1) == 1) {
					b[j] ++;
				}
			}
		}
		for(int  i=0;i<n;i++) {
	        int ans = 0;
			for(int j=0;j<b.length;j++) {
				if(((a[i] << j) & 1) == 1 && b[j] ==1) {
					ans = ans | (1 << j);
				}
			}
			if(ans > max ) {
				max = ans;
				ff = a[i];
			}
		}
		int fg = 1;
		if(ff != -1) System.out.print(ff+" ");
		for(int i=0;i<a.length;i++) {
			if(fg == 1) {
				if(a[i] == ff ) {
					fg = 0;
				} else {
					System.out.print(a[i]+" ");
				}
			}else {
				System.out.print(a[i]+" ");
			}
		}

	}

}


import java.util.Arrays;
import java.util.Scanner;

public class C_1300 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner s = new Scanner(System.in);
		int n = s.nextInt();
		int[] a = new int[n];
		int[] b = new int[32];
		int max = 0,ff=0;
		for (int i = 0; i < n; i++) {
           a[i] = s.nextInt();
		}
		for(int  i=0;i<n;i++) {
			for(int j=0;j<b.length;j++) {
				if(((a[i] >> j) & 1) == 1) {
					b[j] ++;
				}
			}
		}
		for(int  i=0;i<n;i++) {
	        int ans = 0;
			for(int j=0;j<b.length;j++) {
				if(((a[i] << j) & 1) == 1 && b[j] ==1) {
					ans = ans | (1 << j);
				}
			}
			if(ans > max ) {
				max = ans;
				ff = a[i];
			}
		}
		if(ff != -1) System.out.print(ff+" ");
		for(int i=0;i<a.length;i++) {
			if(a[i] != ff ) {
				System.out.print(a[i]+" ");
			}
		}

	}

}


import java.util.Arrays;
import java.util.Scanner;

public class C_1300 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner s = new Scanner(System.in);
		int n = s.nextInt();
		int[] a = new int[n];
		int[] b = new int[32];
		int max = 0,ff=-1;
		for (int i = 0; i < n; i++) {
           a[i] = s.nextInt();
		}
		for(int  i=0;i<n;i++) {
			for(int j=0;j<b.length;j++) {
				if(((a[i] >> j) & 1) == 1) {
					b[j] ++;
				}
			}
		}
		for(int  i=0;i<n;i++) {
	        int ans = 0;
			for(int j=0;j<b.length;j++) {
				if(((a[i] >> j) & 1) == 1 && b[j] ==1) {
					ans |= 1 << j;
				}
			}
			if(ans > max ) {
				max = ans;
				ff = a[i];
			}
		}
		int fg = 1;
		if(ff != -1) System.out.print(ff+" ");
		for(int i=0;i<a.length;i++) {
			if(fg == 1) {
				if(a[i] == ff ) {
					fg = 0;
				} else {
					System.out.print(a[i]+" ");
				}
			}else {
				System.out.print(a[i]+" ");
			}
		}

	}

}


'use strict'

const problem = (n, a) => {
    const d = Array(31).fill(0), p = [];
    const b = a.map(i => (+i).toString(2));

    for (let i = 0; i < n; i++) {
        const x = b[i].split('').reverse();
        for (let j = 0; j < x.length; j++) {
            if (x[j] === '1') { d[j]++; p[j] = i; }
        }
    }
    for (let i = d.length - 1; i > -1; i--) {
        if (d[i] === 1) return [ a[p[i]], a.slice(0, p[i]).join(' '), a.slice(p[i]+1).join(' ') ].join(' ');
    }
    return a.join(' ');
}
print(problem(+readline(), readline().split(' ')))


'use strict'

const problem = (n, a) => {
    const d = Array(31).fill(0), p = [];
    const b = a.map(i => (+i).toString(2));
    for (let i = 0; i < n; i++) {
        const x = b[i]
        for (let j = x.length - 1; j > -1; j--) {
            d[j]++;
            p[j] = i;
        }
    }
    for (let i = d.length - 1; i > -1; i--) {
        if (d[i] === 1) return [ a[p[i]], a.slice(0, p[i]).join(' '), a.slice(p[i]+1).join(' ') ].join(' ');
    }
    return a.join(' ');
}
print(problem(+readline(), readline().split(' ')))


#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;
#define NI_DEGI ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>

#define ll long long
#define pb push_back
#define pll pair<ll,ll>
#define ppl pair<pll,ll>
#define plp pair<ll,pll>
#define MAXN 130
#define ff first
#define ss second
#define all(a) a.begin(),a.end()
#define inf INT_MAX
long long mod = 1000000007;
int dx[] = {1,1,0,-1,-1,-1, 0, 1};
int dy[] = {0,1,1, 1, 0,-1,-1,-1};
int bit(int x,int i){
  return ((x>>(i-1))&1);
}
ll power(ll a,ll n){
  ll ans=1;
  while(n>0){
    if(n%2)ans=(ans*a)%mod;
    n=n>>1;
    a=(a*a)%mod;
  }
  return ans;
}
int digit(ll n){
  int d=0;
  while(n>0){
    d+=n%10;n=n/10;
  }
  return d;
}
int root(int par[],int i){
    if(par[i]!=i)par[i]=root(par,par[i]);
    return par[i];
}
void make_union(int par[],int size[],int a,int b){
    int ra=root(par,a);
    int rb=root(par,b);
    if(ra==rb)return;
    /*if(ra>rb){
      par[rb]=ra;
    }
    else par[ra]=rb;
    return;*/

    if(size[ra]<size[rb]){
        par[ra]=par[rb];
        size[rb]+=size[ra];
    }
    else{
        par[rb]=par[ra];
        size[ra]+=size[rb];
    }
}
ll inv(ll n){
  return power(n,mod-2);
}
int count(ll n){
  ll ans=0;
  while(n){
    ans++;
    n=n/2;
  }
  return ans;
}
int bitc(ll x){
  int ans=0;
  while(x){
    ans++;
    x=x&(x-1);
  }
  return ans;
}
bool cmp(pair<int,ll>a ,pair<int,ll>b){
  if(a.first==b.first)return bitc(a.second)<bitc(b.second);
  return a.first>b.first;
}

int main()
 {
  int t=1;//cin>>t;
  while(t--){
    int n;cin>>n;
    vector<int>v;
    ll xr=0;
    for(int i=0;i<n;i++){
      ll x;cin>>x;
      v.pb(x);
      xr^=x;
    }
    sort(v.rbegin(),v.rend());
    vector<pair<ll,ll>>vv;
    for(auto it:v){
      vv.pb({xr^it,it});
    }
    sort(vv.begin(),vv.end());
    for(auto it:v){
      cout<<it<<" ";
    }
    cout<<"\n";
  }
  return 0;
  }


#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;
#define NI_DEGI ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>

#define ll long long
#define pb push_back
#define pll pair<ll,ll>
#define ppl pair<pll,ll>
#define plp pair<ll,pll>
#define MAXN 130
#define ff first
#define ss second
#define all(a) a.begin(),a.end()
#define inf INT_MAX
long long mod = 1000000007;
int dx[] = {1,1,0,-1,-1,-1, 0, 1};
int dy[] = {0,1,1, 1, 0,-1,-1,-1};
int bit(int x,int i){
  return ((x>>(i-1))&1);
}
ll power(ll a,ll n){
  ll ans=1;
  while(n>0){
    if(n%2)ans=(ans*a)%mod;
    n=n>>1;
    a=(a*a)%mod;
  }
  return ans;
}
int digit(ll n){
  int d=0;
  while(n>0){
    d+=n%10;n=n/10;
  }
  return d;
}
int root(int par[],int i){
    if(par[i]!=i)par[i]=root(par,par[i]);
    return par[i];
}
void make_union(int par[],int size[],int a,int b){
    int ra=root(par,a);
    int rb=root(par,b);
    if(ra==rb)return;
    /*if(ra>rb){
      par[rb]=ra;
    }
    else par[ra]=rb;
    return;*/

    if(size[ra]<size[rb]){
        par[ra]=par[rb];
        size[rb]+=size[ra];
    }
    else{
        par[rb]=par[ra];
        size[ra]+=size[rb];
    }
}
ll inv(ll n){
  return power(n,mod-2);
}
int count(ll n){
  ll ans=0;
  while(n){
    ans++;
    n=n/2;
  }
  return ans;
}
int bitc(ll x){
  int ans=0;
  while(x){
    ans++;
    x=x&(x-1);
  }
  return ans;
}
bool cmp(pair<int,ll>a ,pair<int,ll>b){
  if(a.first==b.first)return count(a.second)>count(b.second);
  return a.first>b.first;
}

int main()
 {
  int t=1;//cin>>t;
  while(t--){
    int n;cin>>n;
    vector<pair<int,ll>>v;
    for(int i=0;i<n;i++){
      ll x;cin>>x;
      v.pb({bitc(x),x});
    }
    sort(v.begin(),v.end(),cmp);
    for(auto it:v){
      cout<<it.second<<" ";
    }
    cout<<"\n";
  }
  return 0;
  }


#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;
#define NI_DEGI ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>

#define ll long long
#define pb push_back
#define pll pair<ll,ll>
#define ppl pair<pll,ll>
#define plp pair<ll,pll>
#define MAXN 130
#define ff first
#define ss second
#define all(a) a.begin(),a.end()
#define inf INT_MAX
long long mod = 1000000007;
int dx[] = {1,1,0,-1,-1,-1, 0, 1};
int dy[] = {0,1,1, 1, 0,-1,-1,-1};
int bit(int x,int i){
  return ((x>>(i-1))&1);
}
ll power(ll a,ll n){
  ll ans=1;
  while(n>0){
    if(n%2)ans=(ans*a)%mod;
    n=n>>1;
    a=(a*a)%mod;
  }
  return ans;
}
int digit(ll n){
  int d=0;
  while(n>0){
    d+=n%10;n=n/10;
  }
  return d;
}
int root(int par[],int i){
    if(par[i]!=i)par[i]=root(par,par[i]);
    return par[i];
}
void make_union(int par[],int size[],int a,int b){
    int ra=root(par,a);
    int rb=root(par,b);
    if(ra==rb)return;
    /*if(ra>rb){
      par[rb]=ra;
    }
    else par[ra]=rb;
    return;*/

    if(size[ra]<size[rb]){
        par[ra]=par[rb];
        size[rb]+=size[ra];
    }
    else{
        par[rb]=par[ra];
        size[ra]+=size[rb];
    }
}
ll inv(ll n){
  return power(n,mod-2);
}
int count(ll n){
  ll ans=0;
  while(n){
    ans++;
    n=n&(n-1);
  }
  return ans;
}
bool cmp(pair<int,ll>a ,pair<int,ll>b){
  if(a.first==b.first)return a.second<b.second;
  return a.first>b.first;
}

int main()
 {
  int t=1;//cin>>t;
  while(t--){
    int n;cin>>n;
    vector<pair<int,ll>>v;
    for(int i=0;i<n;i++){
      ll x;cin>>x;
      v.pb({count(x),x});
    }
    sort(v.begin(),v.end(),cmp);
    for(auto it:v){
      cout<<it.second<<" ";
    }
    cout<<"\n";
  }
  return 0;
  }


#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;
#define NI_DEGI ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>

#define ll long long
#define pb push_back
#define pll pair<ll,ll>
#define ppl pair<pll,ll>
#define plp pair<ll,pll>
#define MAXN 130
#define ff first
#define ss second
#define all(a) a.begin(),a.end()
#define inf INT_MAX
long long mod = 1000000007;
int dx[] = {1,1,0,-1,-1,-1, 0, 1};
int dy[] = {0,1,1, 1, 0,-1,-1,-1};
int bit(int x,int i){
  return ((x>>(i-1))&1);
}
ll power(ll a,ll n){
  ll ans=1;
  while(n>0){
    if(n%2)ans=(ans*a)%mod;
    n=n>>1;
    a=(a*a)%mod;
  }
  return ans;
}
int digit(ll n){
  int d=0;
  while(n>0){
    d+=n%10;n=n/10;
  }
  return d;
}
int root(int par[],int i){
    if(par[i]!=i)par[i]=root(par,par[i]);
    return par[i];
}
void make_union(int par[],int size[],int a,int b){
    int ra=root(par,a);
    int rb=root(par,b);
    if(ra==rb)return;
    /*if(ra>rb){
      par[rb]=ra;
    }
    else par[ra]=rb;
    return;*/

    if(size[ra]<size[rb]){
        par[ra]=par[rb];
        size[rb]+=size[ra];
    }
    else{
        par[rb]=par[ra];
        size[ra]+=size[rb];
    }
}
ll inv(ll n){
  return power(n,mod-2);
}
int count(ll n){
  ll ans=0;
  while(n){
    ans++;
    n=n/2;
  }
  return ans;
}
bool cmp(pair<int,ll>a ,pair<int,ll>b){
  if(a.first==b.first)return a.second>b.second;
  return a.first>b.first;
}

int main()
 {
  int t=1;//cin>>t;
  while(t--){
    int n;cin>>n;
    vector<pair<int,ll>>v;
    for(int i=0;i<n;i++){
      ll x;cin>>x;
      v.pb({count(x),x});
    }
    sort(v.begin(),v.end(),cmp);
    for(auto it:v){
      cout<<it.second<<" ";
    }
    cout<<"\n";
  }
  return 0;
  }


#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;
#define NI_DEGI ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>

#define ll long long
#define pb push_back
#define pll pair<ll,ll>
#define ppl pair<pll,ll>
#define plp pair<ll,pll>
#define MAXN 130
#define ff first
#define ss second
#define all(a) a.begin(),a.end()
#define inf INT_MAX
long long mod = 1000000007;
int dx[] = {1,1,0,-1,-1,-1, 0, 1};
int dy[] = {0,1,1, 1, 0,-1,-1,-1};
int bit(int x,int i){
  return ((x>>(i-1))&1);
}
ll power(ll a,ll n){
  ll ans=1;
  while(n>0){
    if(n%2)ans=(ans*a)%mod;
    n=n>>1;
    a=(a*a)%mod;
  }
  return ans;
}
int digit(ll n){
  int d=0;
  while(n>0){
    d+=n%10;n=n/10;
  }
  return d;
}
int root(int par[],int i){
    if(par[i]!=i)par[i]=root(par,par[i]);
    return par[i];
}
void make_union(int par[],int size[],int a,int b){
    int ra=root(par,a);
    int rb=root(par,b);
    if(ra==rb)return;
    /*if(ra>rb){
      par[rb]=ra;
    }
    else par[ra]=rb;
    return;*/

    if(size[ra]<size[rb]){
        par[ra]=par[rb];
        size[rb]+=size[ra];
    }
    else{
        par[rb]=par[ra];
        size[ra]+=size[rb];
    }
}
ll inv(ll n){
  return power(n,mod-2);
}
int count(ll n){
  ll ans=0;
  while(n){
    ans++;
    n=n/2;
  }
  return ans;
}
int bitc(ll x){
  int ans=0;
  while(x){
    ans++;
    x=x&(x-1);
  }
  return ans;
}

int main()
 {
  int t=1;//cin>>t;
  while(t--){
    int n;cin>>n;
    ll arr[n];
    map<ll,vector<ll>>bits;
    for(int i=0;i<n;i++){
      ll x;cin>>x;
      bits[-count(x)].push_back(x);
    }
    int sz=bits.size();
    //cout<<sz<<"\n";
    while(sz>0){
      vector<int>v;
      for(auto it:bits){
        auto vv=it.second;
        cout<<(vv.back())<<" ";
        vv.pop_back();
        bits[it.first]=vv;
        if(vv.size()==0)v.push_back(it.first);
      }
      for(auto i:v)bits.erase(i);
      sz=bits.size();
      //cout<<sz<<"\n";
    }

  }
  return 0;
  }


#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;
#define NI_DEGI ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>

#define ll long long
#define pb push_back
#define pll pair<ll,ll>
#define ppl pair<pll,ll>
#define plp pair<ll,pll>
#define MAXN 130
#define ff first
#define ss second
#define all(a) a.begin(),a.end()
#define inf INT_MAX
long long mod = 1000000007;
int dx[] = {1,1,0,-1,-1,-1, 0, 1};
int dy[] = {0,1,1, 1, 0,-1,-1,-1};
int bit(int x,int i){
  return ((x>>(i-1))&1);
}
ll power(ll a,ll n){
  ll ans=1;
  while(n>0){
    if(n%2)ans=(ans*a)%mod;
    n=n>>1;
    a=(a*a)%mod;
  }
  return ans;
}
int digit(ll n){
  int d=0;
  while(n>0){
    d+=n%10;n=n/10;
  }
  return d;
}
int root(int par[],int i){
    if(par[i]!=i)par[i]=root(par,par[i]);
    return par[i];
}
void make_union(int par[],int size[],int a,int b){
    int ra=root(par,a);
    int rb=root(par,b);
    if(ra==rb)return;
    /*if(ra>rb){
      par[rb]=ra;
    }
    else par[ra]=rb;
    return;*/

    if(size[ra]<size[rb]){
        par[ra]=par[rb];
        size[rb]+=size[ra];
    }
    else{
        par[rb]=par[ra];
        size[ra]+=size[rb];
    }
}
ll inv(ll n){
  return power(n,mod-2);
}
int count(ll n){
  ll ans=0;
  while(n){
    ans++;
    n=n/2;
  }
  return ans;
}
int bitc(ll x){
  int ans=0;
  while(x){
    ans++;
    x=x&(x-1);
  }
  return ans;
}
bool cmp(pair<int,ll>a ,pair<int,ll>b){
  if(a.first==b.first)return bitc(a.second)<bitc(b.second);
  return a.first>b.first;
}

int main()
 {
  int t=1;//cin>>t;
  while(t--){
    int n;cin>>n;
    vector<int>v;
    ll xr=0;
    for(int i=0;i<n;i++){
      ll x;cin>>x;
      v.pb(x);
      xr^=x;
    }
    vector<pair<ll,ll>>vv;
    for(auto it:v){
      vv.pb({xr^it,it});
    }
    sort(vv.begin(),vv.end());
    for(auto it:vv){
      cout<<it.second<<" ";
    }
    cout<<"\n";
  }
  return 0;
  }


#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;
#define NI_DEGI ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>

#define ll long long
#define pb push_back
#define pll pair<ll,ll>
#define ppl pair<pll,ll>
#define plp pair<ll,pll>
#define MAXN 130
#define ff first
#define ss second
#define all(a) a.begin(),a.end()
#define inf INT_MAX
long long mod = 1000000007;
int dx[] = {1,1,0,-1,-1,-1, 0, 1};
int dy[] = {0,1,1, 1, 0,-1,-1,-1};
int bit(int x,int i){
  return ((x>>(i-1))&1);
}
ll power(ll a,ll n){
  ll ans=1;
  while(n>0){
    if(n%2)ans=(ans*a)%mod;
    n=n>>1;
    a=(a*a)%mod;
  }
  return ans;
}
int digit(ll n){
  int d=0;
  while(n>0){
    d+=n%10;n=n/10;
  }
  return d;
}
int root(int par[],int i){
    if(par[i]!=i)par[i]=root(par,par[i]);
    return par[i];
}
void make_union(int par[],int size[],int a,int b){
    int ra=root(par,a);
    int rb=root(par,b);
    if(ra==rb)return;
    /*if(ra>rb){
      par[rb]=ra;
    }
    else par[ra]=rb;
    return;*/

    if(size[ra]<size[rb]){
        par[ra]=par[rb];
        size[rb]+=size[ra];
    }
    else{
        par[rb]=par[ra];
        size[ra]+=size[rb];
    }
}
ll inv(ll n){
  return power(n,mod-2);
}
int count(ll n){
  ll ans=0;
  while(n){
    ans++;
    n=n/2;
  }
  return ans;
}
int bitc(ll x){
  int ans=0;
  while(x){
    ans++;
    x=x&(x-1);
  }
  return ans;
}
bool cmp(pair<int,ll>a ,pair<int,ll>b){
  if(a.first==b.first)return bitc(a.second)<bitc(b.second);
  return a.first>b.first;
}

int main()
 {
  int t=1;//cin>>t;
  while(t--){
    int n;cin>>n;
    vector<int>v;
    ll xr=0;
    for(int i=0;i<n;i++){
      ll x;cin>>x;
      v.pb(x);
      xr^=x;
    }
    vector<pair<ll,ll>>vv;
    for(auto it:v){
      vv.pb({xr^it,it});
    }
    sort(vv.rbegin(),vv.rend());
    for(auto it:vv){
      cout<<it.second<<" ";
    }
    cout<<"\n";
  }
  return 0;
  }


#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;
#define NI_DEGI ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>

#define ll long long
#define pb push_back
#define pll pair<ll,ll>
#define ppl pair<pll,ll>
#define plp pair<ll,pll>
#define MAXN 130
#define ff first
#define ss second
#define all(a) a.begin(),a.end()
#define inf INT_MAX
long long mod = 1000000007;
int dx[] = {1,1,0,-1,-1,-1, 0, 1};
int dy[] = {0,1,1, 1, 0,-1,-1,-1};
int bit(int x,int i){
  return ((x>>(i-1))&1);
}
ll power(ll a,ll n){
  ll ans=1;
  while(n>0){
    if(n%2)ans=(ans*a)%mod;
    n=n>>1;
    a=(a*a)%mod;
  }
  return ans;
}
int digit(ll n){
  int d=0;
  while(n>0){
    d+=n%10;n=n/10;
  }
  return d;
}
int root(int par[],int i){
    if(par[i]!=i)par[i]=root(par,par[i]);
    return par[i];
}
void make_union(int par[],int size[],int a,int b){
    int ra=root(par,a);
    int rb=root(par,b);
    if(ra==rb)return;
    /*if(ra>rb){
      par[rb]=ra;
    }
    else par[ra]=rb;
    return;*/

    if(size[ra]<size[rb]){
        par[ra]=par[rb];
        size[rb]+=size[ra];
    }
    else{
        par[rb]=par[ra];
        size[ra]+=size[rb];
    }
}
ll inv(ll n){
  return power(n,mod-2);
}
int count(ll n){
  ll ans=0;
  while(n){
    ans++;
    n=n&(n-1);
  }
  return ans;
}

int main()
 {
  int t=1;//cin>>t;
  while(t--){
    int n;cin>>n;
    vector<pair<int,ll>>v;
    for(int i=0;i<n;i++){
      ll x;cin>>x;
      v.pb({count(x),x});
    }
    sort(v.rbegin(),v.rend());
    for(auto it:v){
      cout<<it.second<<" ";
    }
    cout<<"\n";
  }
  return 0;
  }


#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;
#define NI_DEGI ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>

#define ll long long
#define pb push_back
#define pll pair<ll,ll>
#define ppl pair<pll,ll>
#define plp pair<ll,pll>
#define MAXN 130
#define ff first
#define ss second
#define all(a) a.begin(),a.end()
#define inf INT_MAX
long long mod = 1000000007;
int dx[] = {1,1,0,-1,-1,-1, 0, 1};
int dy[] = {0,1,1, 1, 0,-1,-1,-1};
int bit(int x,int i){
  return ((x>>(i-1))&1);
}
ll power(ll a,ll n){
  ll ans=1;
  while(n>0){
    if(n%2)ans=(ans*a)%mod;
    n=n>>1;
    a=(a*a)%mod;
  }
  return ans;
}
int digit(ll n){
  int d=0;
  while(n>0){
    d+=n%10;n=n/10;
  }
  return d;
}
int root(int par[],int i){
    if(par[i]!=i)par[i]=root(par,par[i]);
    return par[i];
}
void make_union(int par[],int size[],int a,int b){
    int ra=root(par,a);
    int rb=root(par,b);
    if(ra==rb)return;
    /*if(ra>rb){
      par[rb]=ra;
    }
    else par[ra]=rb;
    return;*/

    if(size[ra]<size[rb]){
        par[ra]=par[rb];
        size[rb]+=size[ra];
    }
    else{
        par[rb]=par[ra];
        size[ra]+=size[rb];
    }
}
ll inv(ll n){
  return power(n,mod-2);
}
int count(ll n){
  ll ans=0;
  while(n){
    ans++;
    n=n/2;
  }
  return ans;
}
int bitc(ll x){
  int ans=0;
  while(x){
    ans++;
    x=x&(x-1);
  }
  return ans;
}
bool cmp(pair<int,ll>a ,pair<int,ll>b){
  if(a.first==b.first)return bitc(a.second)<bitc(b.second);
  return a.first>b.first;
}

int main()
 {
  int t=1;//cin>>t;
  while(t--){
    int n;cin>>n;
    vector<pair<int,ll>>v;
    for(int i=0;i<n;i++){
      ll x;cin>>x;
      v.pb({count(x),x});
    }
    sort(v.begin(),v.end(),cmp);
    for(auto it:v){
      cout<<it.second<<" ";
    }
    cout<<"\n";
  }
  return 0;
  }


#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;
#define NI_DEGI ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>

#define ll long long
#define pb push_back
#define pll pair<ll,ll>
#define ppl pair<pll,ll>
#define plp pair<ll,pll>
#define MAXN 130
#define ff first
#define ss second
#define all(a) a.begin(),a.end()
#define inf INT_MAX
long long mod = 1000000007;
int dx[] = {1,1,0,-1,-1,-1, 0, 1};
int dy[] = {0,1,1, 1, 0,-1,-1,-1};
int bit(int x,int i){
  return ((x>>(i-1))&1);
}
ll power(ll a,ll n){
  ll ans=1;
  while(n>0){
    if(n%2)ans=(ans*a)%mod;
    n=n>>1;
    a=(a*a)%mod;
  }
  return ans;
}
int digit(ll n){
  int d=0;
  while(n>0){
    d+=n%10;n=n/10;
  }
  return d;
}
int root(int par[],int i){
    if(par[i]!=i)par[i]=root(par,par[i]);
    return par[i];
}
void make_union(int par[],int size[],int a,int b){
    int ra=root(par,a);
    int rb=root(par,b);
    if(ra==rb)return;
    /*if(ra>rb){
      par[rb]=ra;
    }
    else par[ra]=rb;
    return;*/

    if(size[ra]<size[rb]){
        par[ra]=par[rb];
        size[rb]+=size[ra];
    }
    else{
        par[rb]=par[ra];
        size[ra]+=size[rb];
    }
}
ll inv(ll n){
  return power(n,mod-2);
}
int count(ll n){
  ll ans=0;
  while(n){
    ans++;
    n=n/2;
  }
  return ans;
}
int bitc(ll x){
  int ans=0;
  while(x){
    ans++;
    x=x&(x-1);
  }
  return ans;
}

int main()
 {
  int t=1;//cin>>t;
  while(t--){
    int n;cin>>n;
    ll arr[n];
    vector<ll>bits[64];
    map<ll,ll>M;
    for(int i=0;i<n;i++){
      cin>>arr[i];
      M[arr[i]]++;
      ll x=arr[i];
      int j=0;
      while(x>0){
        if(x%2)bits[j].pb(arr[i]);
        x=x/2;
        j++;
      }
    }
    vector<ll>ans;
    for(int i=63;i>=0;i--){
      if(bits[i].size()==0)continue;
      ll x=bits[i][0];
      if(M[x]==0)continue;
      ans.pb(x);
      M[x]--;
    }
    for(int i=0;i<n;i++){
      if(M[arr[i]]>0)ans.push_back(arr[i]);
    }
    for(auto i:ans)cout<<i<<" ";
    cout<<"\n";

  }
  return 0;
  }


#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;
#define NI_DEGI ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>

#define ll long long
#define pb push_back
#define pll pair<ll,ll>
#define ppl pair<pll,ll>
#define plp pair<ll,pll>
#define MAXN 130
#define ff first
#define ss second
#define all(a) a.begin(),a.end()
#define inf INT_MAX
long long mod = 1000000007;
int dx[] = {1,1,0,-1,-1,-1, 0, 1};
int dy[] = {0,1,1, 1, 0,-1,-1,-1};
int bit(int x,int i){
  return ((x>>(i-1))&1);
}
ll power(ll a,ll n){
  ll ans=1;
  while(n>0){
    if(n%2)ans=(ans*a)%mod;
    n=n>>1;
    a=(a*a)%mod;
  }
  return ans;
}
int digit(ll n){
  int d=0;
  while(n>0){
    d+=n%10;n=n/10;
  }
  return d;
}
int root(int par[],int i){
    if(par[i]!=i)par[i]=root(par,par[i]);
    return par[i];
}
void make_union(int par[],int size[],int a,int b){
    int ra=root(par,a);
    int rb=root(par,b);
    if(ra==rb)return;
    /*if(ra>rb){
      par[rb]=ra;
    }
    else par[ra]=rb;
    return;*/

    if(size[ra]<size[rb]){
        par[ra]=par[rb];
        size[rb]+=size[ra];
    }
    else{
        par[rb]=par[ra];
        size[ra]+=size[rb];
    }
}
ll inv(ll n){
  return power(n,mod-2);
}
int count(ll n){
  ll ans=0;
  while(n){
    ans++;
    n=n/2;
  }
  return ans;
}
int bitc(ll x){
  int ans=0;
  while(x){
    ans++;
    x=x&(x-1);
  }
  return ans;
}

int main()
 {
  int t=1;//cin>>t;
  while(t--){
    int n;cin>>n;
    ll arr[n];
    vector<ll>bits[64];
    map<ll,ll>M;
    for(int i=0;i<n;i++){
      cin>>arr[i];
      M[arr[i]]++;
      ll x=arr[i];
      int j=0;
      while(x>0){
        if(x%2)bits[j].pb(arr[i]);
        x=x/2;
        j++;
      }
    }
    vector<ll>ans;
    for(int i=63;i>=0;i--){
      if(bits[i].size()!=1)continue;
      ll x=bits[i][0];
      if(M[x]==0)continue;
      ans.pb(x);
      M[x]--;
    }
    for(int i=0;i<n;i++){
      if(M[arr[i]]>0)ans.push_back(arr[i]);
    }
    for(auto i:ans)cout<<i<<" ";
    cout<<"\n";

  }
  return 0;
  }


#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;
#define NI_DEGI ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>

#define ll long long
#define pb push_back
#define pll pair<ll,ll>
#define ppl pair<pll,ll>
#define plp pair<ll,pll>
#define MAXN 130
#define ff first
#define ss second
#define all(a) a.begin(),a.end()
#define inf INT_MAX
long long mod = 1000000007;
int dx[] = {1,1,0,-1,-1,-1, 0, 1};
int dy[] = {0,1,1, 1, 0,-1,-1,-1};
int bit(int x,int i){
  return ((x>>(i-1))&1);
}
ll power(ll a,ll n){
  ll ans=1;
  while(n>0){
    if(n%2)ans=(ans*a)%mod;
    n=n>>1;
    a=(a*a)%mod;
  }
  return ans;
}
int digit(ll n){
  int d=0;
  while(n>0){
    d+=n%10;n=n/10;
  }
  return d;
}
int root(int par[],int i){
    if(par[i]!=i)par[i]=root(par,par[i]);
    return par[i];
}
void make_union(int par[],int size[],int a,int b){
    int ra=root(par,a);
    int rb=root(par,b);
    if(ra==rb)return;
    /*if(ra>rb){
      par[rb]=ra;
    }
    else par[ra]=rb;
    return;*/

    if(size[ra]<size[rb]){
        par[ra]=par[rb];
        size[rb]+=size[ra];
    }
    else{
        par[rb]=par[ra];
        size[ra]+=size[rb];
    }
}
ll inv(ll n){
  return power(n,mod-2);
}
int count(ll n){
  ll ans=0;
  while(n){
    ans++;
    n=n&(n-1);
  }
  return ans;
}
bool cmp(pair<int,ll>a ,pair<int,ll>b){
  if(a.first==b.first)return a.second>b.second;
  return a.first>b.first;
}

int main()
 {
  int t=1;//cin>>t;
  while(t--){
    int n;cin>>n;
    vector<pair<int,ll>>v;
    for(int i=0;i<n;i++){
      ll x;cin>>x;
      v.pb({count(x),x});
    }
    sort(v.begin(),v.end(),cmp);
    for(auto it:v){
      cout<<it.second<<" ";
    }
    cout<<"\n";
  }
  return 0;
  }


#include <bits/stdc++.h>

#define pb push_back
#define mp make_pair
#define xx first
#define yy second
#define ss second
#define ff first

using namespace std;

typedef long long i64;

bool comp(int a, int b) {
	return __builtin_popcount(a) > __builtin_popcount(b);
}

int main(){
	ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
	// freopen("in.txt", "r", stdin);

	vector<int> a;
	int n;

	cin >> n;

	a.resize(n);

	for (int i = 0; i < n; i++){
		cin >> a[i];
	}

	sort(a.begin(), a.end(), comp);

	for (int i = 0; i < n; i++){
		cout << a[i] << ' ';
	}
	return 0;

}


#include <bits/stdc++.h>

#define pb push_back
#define mp make_pair
#define xx first
#define yy second
#define ss second
#define ff first

using namespace std;

typedef long long i64;

// bool comp(int a, int b) {
// 	return __builtin_popcount(a) > __builtin_popcount(b);
// }

int main(){
	ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
	// freopen("in.txt", "r", stdin);

	vector<int> a;
	int n;

	cin >> n;

	a.resize(n);
	int overall = 0;

	for (int i = 0; i < n; i++){
		cin >> a[i];
		overall |= a[i];
	}

	vector<int> orb(n), ora(n);

	int tmp = 0;

	for (int i = 0; i < n; i++){
		tmp |= a[i];
		orb[i] = tmp;
	}
	tmp = 0;
	for (int i = n - 1; i >= 0; i--){
		tmp |= a[i];
		ora[i] = tmp;
	}

	int maxi = 0;
	int maxval = -1;

	for (int i = 1; i < n - 1; i++){
		if ((a[i] | (orb[i - 1] | ora[i + 1])) - (orb[i - 1] | ora[i + 1]) > maxval){
			maxi = i;
			maxval = (a[i] | (orb[i - 1] | ora[i + 1])) - (orb[i - 1] | ora[i + 1]);
		}
	}

	if ((ora[1] | a[0]) - ora[1] > maxval){
		maxi = 0;
		maxval = (ora[1] | a[0]) - ora[1];
	}

	if ((a[n - 2] | ora[n - 2]) - ora[n - 2] > maxval){
		maxi = n - 1;
		maxval = (a[n - 2] | ora[n - 2]) - ora[n - 2];
	}

	cout << a[maxi] << ' ';

	for (int i = 0; i < n; i++){
		if (i != maxi) cout << a[i] << ' ';
	}

	return 0;

}


#include <bits/stdc++.h>

#define pb push_back
#define mp make_pair
#define xx first
#define yy second
#define ss second
#define ff first

using namespace std;

typedef long long i64;

bool comp(int a, int b) {
	return __builtin_popcount(a) < __builtin_popcount(b);
}

int main(){
	ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
	freopen("in.txt", "r", stdin);

	vector<int> a;
	int n;

	cin >> n;

	a.resize(n);

	for (int i = 0; i < n; i++){
		cin >> a[i];
	}

	sort(a.begin(), a.end());

	cout << a[n - 1] << ' ';

	a.pop_back();

	reverse(a.begin(), a.end());

	sort(a.begin(), a.end(), comp);

	for (int i = 0; i < n - 1; i++){
		cout << a[i] << ' ';
	}
	return 0;

}


#include <bits/stdc++.h>

#define pb push_back
#define mp make_pair
#define xx first
#define yy second
#define ss second
#define ff first

using namespace std;

typedef long long i64;

bool comp(int a, int b) {
	if (__builtin_popcount(a) == __builtin_popcount(b) ) return a > b;
	return __builtin_popcount(a) > __builtin_popcount(b);
}

int main(){
	ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
	// freopen("in.txt", "r", stdin);

	vector<int> a;
	int n;

	cin >> n;

	a.resize(n);

	for (int i = 0; i < n; i++){
		cin >> a[i];
	}

	sort(a.begin(), a.end(), comp);

	for (int i = 0; i < n; i++){
		cout << a[i] << ' ';
	}
	return 0;

}


#include <bits/stdc++.h>

#define pb push_back
#define mp make_pair
#define xx first
#define yy second
#define ss second
#define ff first

using namespace std;

typedef long long i64;

// bool comp(int a, int b) {
// 	return __builtin_popcount(a) > __builtin_popcount(b);
// }

int main(){
	ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
	// freopen("in.txt", "r", stdin);

	vector<int> a;
	int n;

	cin >> n;

	a.resize(n);
	int overall = 0;

	for (int i = 0; i < n; i++){
		cin >> a[i];
		overall |= a[i];
	}

	vector<int> orb(n), ora(n);

	int tmp = 0;

	for (int i = 0; i < n; i++){
		tmp |= a[i];
		orb[i] = tmp;
	}
	tmp = 0;
	for (int i = n - 1; i >= 0; i--){
		tmp |= a[i];
		ora[i] = tmp;
	}

	int maxi = 0;
	int maxval = -1;

	for (int i = 1; i < n - 1; i++){
		if ((overall - (orb[i - 1] | ora[i + 1])) > maxval){
			maxi = i;
			maxval = overall - (orb[i - 1] | ora[i + 1]);
		}
	}

	if (overall - ora[1] > maxval){
		maxi = 0;
		maxval = overall - ora[1];
	}

	if (overall - ora[n - 2] > maxval){
		maxi = n - 1;
		maxval = overall - ora[n - 2];
	}

	cout << a[maxi] << ' ';

	for (int i = 0; i < n; i++){
		if (i != maxi) cout << a[i] << ' ';
	}

	return 0;

}


#include <bits/stdc++.h>

#define pb push_back
#define mp make_pair
#define xx first
#define yy second
#define ss second
#define ff first

using namespace std;

typedef long long i64;

bool comp(int a, int b) {
	return __builtin_popcount(a) < __builtin_popcount(b);
}

int main(){
	ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
	// freopen("in.txt", "r", stdin);

	vector<int> a;
	int n;

	cin >> n;

	a.resize(n);

	for (int i = 0; i < n; i++){
		cin >> a[i];
	}

	sort(a.begin(), a.end());

	cout << a[n - 1] << ' ';

	a.pop_back();

	reverse(a.begin(), a.end());

	sort(a.begin(), a.end(), comp);

	for (int i = 0; i < n - 1; i++){
		cout << a[i] << ' ';
	}
	return 0;

}


#include <bits/stdc++.h>

#define pb push_back
#define mp make_pair
#define xx first
#define yy second
#define ss second
#define ff first

using namespace std;

typedef long long i64;

bool comp(int a, int b) {
	if (__builtin_popcount(a) == __builtin_popcount(b) ) return a < b;
	return __builtin_popcount(a) > __builtin_popcount(b);
}

int main(){
	ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
	// freopen("in.txt", "r", stdin);

	vector<int> a;
	int n;

	cin >> n;

	a.resize(n);

	for (int i = 0; i < n; i++){
		cin >> a[i];
	}

	sort(a.begin(), a.end(), comp);

	for (int i = 0; i < n; i++){
		cout << a[i] << ' ';
	}
	return 0;

}


#include <bits/stdc++.h>

#define pb push_back
#define mp make_pair
#define xx first
#define yy second
#define ss second
#define ff first

using namespace std;

typedef long long i64;

// bool comp(int a, int b) {
// 	return __builtin_popcount(a) > __builtin_popcount(b);
// }

int main(){
	ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
	// freopen("in.txt", "r", stdin);

	vector<int> a;
	int n;

	cin >> n;

	a.resize(n);
	int overall = 0;

	for (int i = 0; i < n; i++){
		cin >> a[i];
		overall |= a[i];
	}

	vector<int> orb(n), ora(n);

	int tmp = 0;

	for (int i = 0; i < n; i++){
		tmp |= a[i];
		orb[i] = tmp;
	}
	tmp = 0;
	for (int i = n - 1; i >= 0; i--){
		tmp |= a[i];
		ora[i] = tmp;
	}

	int maxi = 0;
	int maxval = -1;

	for (int i = 1; i < n - 1; i++){
		if ((a[i] | (orb[i - 1] | ora[i + 1])) - (orb[i - 1] | ora[i + 1]) > maxval){
			maxi = i;
			maxval = (a[i] | (orb[i - 1] | ora[i + 1])) - (orb[i - 1] | ora[i + 1]);
		}
	}

	if ((ora[1] | a[0]) - ora[1] > maxval){
		maxi = 0;
		maxval = (ora[1] | a[0]) - ora[1];
	}

	if ((a[n - 1] | orb[n - 2]) - orb[n - 2] > maxval){
		maxi = n - 1;
		maxval = (a[n - 1] | orb[n - 2]) - orb[n - 2];
	}

	cout << a[maxi] << ' ';

	for (int i = 0; i < n; i++){
		if (i != maxi) cout << a[i] << ' ';
	}

	return 0;

}


#include <bits/stdc++.h>

#define pb push_back
#define mp make_pair
#define xx first
#define yy second
#define ss second
#define ff first

using namespace std;

typedef long long i64;

// bool comp(int a, int b) {
// 	return __builtin_popcount(a) > __builtin_popcount(b);
// }

int main(){
	ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
	// freopen("in.txt", "r", stdin);

	vector<int> a;
	int n;

	cin >> n;

	a.resize(n);

	for (int i = 0; i < n; i++){
		cin >> a[i];
	}

	sort(a.begin(), a.end());
	reverse(a.begin(), a.end());

	for (int i = 0; i < n; i++){
		cout << a[i] << ' ';
	}
	return 0;

}


#include <bits/stdc++.h>
#define pb push_back
#define rep(i, a, b) for (int i = a; i < b; i++)
#define ll long long int

using namespace std;

int main()
{
    ll n;
    cin>>n;

    ll a[n],a1[n];

    rep(i,0,n)
    {
        cin>>a[i];
        a1[i] = ~a[i];
    }

    ll pre[n+1],suf[n+1];

    pre[0] = 1;
    suf[n] = 1;

    rep(i,1,n+1)
    {
        pre[i] = a1[i-1]&pre[i-1];
    }

    for(ll j = n-1;j>=0;j--)
    {
        suf[j] = suf[j+1]&a1[j];
    }

    ll ans = INT_MIN,ind;

    rep(i,0,n)
    {
        if(i==0)
        {
            if(a[0]&suf[i+1]>ans)
            {
                ans = a[0]&suf[i+1];
                ind = i;
            }
        }
        else if(i==n-1)
        {
            if(pre[i]&a[n-1]>ans)
            {
                ans = pre[i]&a[n-1];
                ind = i;
            }
        }
        else
        {
            if(ans<pre[i-1]&a[i]&suf[i+1])
            {
                ans = pre[i]&a[i]&suf[i+1];
                ind = i;
            }
        }
    }

    cout<<a[ind]<<" ";

    rep(i,0,n)
    {
        if(i!=ind)
        cout<<a[i]<<" ";
    }
    return 0;
}


#include <bits/stdc++.h>
#define pb push_back
#define rep(i, a, b) for (int i = a; i < b; i++)
#define ll long long int

using namespace std;

int main()
{
    ll n;
    cin>>n;

    ll a[n],a1[n];

    rep(i,0,n)
    {
        cin>>a[i];
        a1[i] = ~a[i];
    }

    ll pre[n],suf[n];

    pre[0] = a1[0];
    suf[n-1] = a1[n-1];

    rep(i,1,n)
    {
        pre[i] = a1[i]&pre[i-1];
    }

    for(ll j = n-2;j>=0;j--)
    {
        suf[j] = suf[j+1]&a1[j];
    }

    ll ans = INT_MIN,ind;

    rep(i,0,n)
    {
        if(i==0)
        {
            if(a[0]&suf[i+1]>ans)
            {
                ans = a[0]&suf[i+1];
                ind = i;
            }
        }
        else if(i==n-1)
        {
            if(pre[i-1]&a[n-1]>ans)
            {
                ans = pre[i-1]&a[n-1];
                ind = i;
            }
        }
        else
        {
            if(ans<pre[i-1]&a[i]&suf[i+1])
            {
                ans = pre[i-1]&a[i]&suf[i+1];
                ind = i;
            }
        }
    }

    cout<<a[ind]<<" ";

    rep(i,0,n)
    {
        if(i!=ind)
        cout<<a[i]<<" ";
    }
    return 0;
}


import java.io.*;
import java.math.BigInteger;
import java.util.*;

public class C {

    static BufferedReader in;
    static PrintWriter out;
    static StringTokenizer tok;

    public static void main(String[] args) throws IOException {
        try {
            in = new BufferedReader(new FileReader("rsq.in"));
            out = new PrintWriter(new File("rsq.out"));
        } catch (Exception e) {
            in = new BufferedReader(new InputStreamReader(System.in));
            out = new PrintWriter(new OutputStreamWriter(System.out));
        }
        tok = new StringTokenizer("");
        new C().solve();
        out.flush();
        out.close();
    }

    String nextTok() throws IOException {
        if (!tok.hasMoreTokens()) {
            tok = new StringTokenizer(in.readLine());
        }
        return tok.nextToken();
    }

    int nextInt() throws IOException {
        return Integer.parseInt(nextTok());
    }

    long nextLong() throws IOException {
        return Long.parseLong(nextTok());
    }

    double nextDouble() throws IOException {
        return Double.parseDouble(nextTok());
    }
    int n;
    int m;
    int fi;
    int fj;
    boolean used[][];
    int ar[][];
    int cnt = 0;

    void tt(int ii, int jj, int st) {
        used[ii][jj] = true;
        if (ii == fi && jj == fj) {
            used[ii][jj] = false;
            if (ar[ii][jj] == -1 && st != 1) {
                System.out.println("YES");
                System.exit(0);
            }
        }
        if (ar[ii][jj] == -1 && st > 1) return;
        ar[ii][jj] = -1;
        if (ii-1 >= 0 && !used[ii-1][jj]) tt(ii-1,jj,st+1);
        if (ii+1 < n && !used[ii+1][jj]) tt(ii+1,jj,st+1);
        if (jj+1 < m && !used[ii][jj+1]) tt(ii,jj+1,st+1);
        if (jj-1 >= 0 && !used[ii][jj-1]) tt(ii,jj-1,st+1);
        //ar[ii][jj] = 0;

    }

    void solve() throws IOException {
        n = nextInt();
        m = nextInt();
        ar = new int[n][m];
        used = new boolean[n][m];
        for (int i = 0; i < n; i++) {
            String tmp1 = nextTok();
            for (int j = 0; j < m; j++) {
                if (tmp1.charAt(j) == 'X') {
                    ar[i][j] = -1;
                }
            }
        }
        int si = nextInt();
        int sj = nextInt();
        fi = nextInt()-1;
        fj = nextInt()-1;
        //ar[si-1][sj-1] = 0;
        tt(si - 1, sj - 1,1);
        System.out.println("NO");
        //System.out.println(cnt);

    }

}


import java.io.*;
import java.math.BigInteger;
import java.util.*;

public class C {

    static BufferedReader in;
    static PrintWriter out;
    static StringTokenizer tok;

    public static void main(String[] args) throws IOException {
        try {
            in = new BufferedReader(new FileReader("rsq.in"));
            out = new PrintWriter(new File("rsq.out"));
        } catch (Exception e) {
            in = new BufferedReader(new InputStreamReader(System.in));
            out = new PrintWriter(new OutputStreamWriter(System.out));
        }
        tok = new StringTokenizer("");
        new C().solve();
        out.flush();
        out.close();
    }

    String nextTok() throws IOException {
        if (!tok.hasMoreTokens()) {
            tok = new StringTokenizer(in.readLine());
        }
        return tok.nextToken();
    }

    int nextInt() throws IOException {
        return Integer.parseInt(nextTok());
    }

    long nextLong() throws IOException {
        return Long.parseLong(nextTok());
    }

    double nextDouble() throws IOException {
        return Double.parseDouble(nextTok());
    }
    int n;
    int m;
    int fi;
    int fj;
    boolean used[][];
    int ar[][];
    int cnt = 0;

    void tt(int ii, int jj) {
        if (ii == fi && jj == fj) {
            if (ar[ii][jj] == -1) {
                System.out.println("YES");
                System.exit(0);
            }
        }
        if (ar[ii][jj] == -1) return;
        ar[ii][jj] = -1;
        if (ii-1 >= 0) tt(ii-1,jj);
        if (ii+1 < n) tt(ii+1,jj);
        if (jj+1 < m) tt(ii,jj+1);
        if (jj-1 >= 0) tt(ii,jj-1);
        ar[ii][jj] = 0;

    }

    void solve() throws IOException {
        n = nextInt();
        m = nextInt();
        ar = new int[n][m];
        used = new boolean[n][m];
        for (int i = 0; i < n; i++) {
            String tmp = nextTok();
            for (int j = 0; j < m; j++) {
                if (tmp.charAt(j) == 'X') {
                    ar[i][j] = -1;
                } else ar[i][j] = 0;
            }
        }
        int si = nextInt();
        int sj = nextInt();
        fi = nextInt()-1;
        fj = nextInt()-1;
        ar[si-1][sj-1] = 0;
        tt(si - 1, sj - 1);
        System.out.println("NO");
        //System.out.println(cnt);

    }

}


import java.io.*;
import java.math.BigInteger;
import java.util.*;

public class C {

    static BufferedReader in;
    static PrintWriter out;
    static StringTokenizer tok;

    public static void main(String[] args) throws IOException {
        try {
            in = new BufferedReader(new FileReader("rsq.in"));
            out = new PrintWriter(new File("rsq.out"));
        } catch (Exception e) {
            in = new BufferedReader(new InputStreamReader(System.in));
            out = new PrintWriter(new OutputStreamWriter(System.out));
        }
        tok = new StringTokenizer("");
        new C().solve();
        out.flush();
        out.close();
    }

    String nextTok() throws IOException {
        if (!tok.hasMoreTokens()) {
            tok = new StringTokenizer(in.readLine());
        }
        return tok.nextToken();
    }

    int nextInt() throws IOException {
        return Integer.parseInt(nextTok());
    }

    long nextLong() throws IOException {
        return Long.parseLong(nextTok());
    }

    double nextDouble() throws IOException {
        return Double.parseDouble(nextTok());
    }
    int n;
    int m;
    int fi;
    int fj;
    boolean used[][];
    int ar[][];
    int cnt = 0;

    void tt(int ii, int jj, int st) {
        used[ii][jj] = true;
        if (ii == fi && jj == fj) {
            used[ii][jj] = false;
            if (ar[ii][jj] == -1 && st != 1) {
                System.out.println("YES");
                System.exit(0);
            }
        }
        if (ar[ii][jj] == -1 && st != 1) return;
        ar[ii][jj] = -1;
        if (ii-1 >= 0 && !used[ii-1][jj]) tt(ii-1,jj,st+1);
        if (ii+1 < n && !used[ii+1][jj]) tt(ii+1,jj,st+1);
        if (jj+1 < m && !used[ii][jj+1]) tt(ii,jj+1,st+1);
        if (jj-1 >= 0 && !used[ii][jj-1]) tt(ii,jj-1,st+1);
        ar[ii][jj] = 0;

    }

    void solve() throws IOException {
        n = nextInt();
        m = nextInt();
        ar = new int[n][m];
        used = new boolean[n][m];
        for (int i = 0; i < n; i++) {
            String tmp = nextTok();
            for (int j = 0; j < m; j++) {
                if (tmp.charAt(j) == 'X') {
                    ar[i][j] = -1;
                } else ar[i][j] = 0;
            }
        }
        int si = nextInt();
        int sj = nextInt();
        fi = nextInt()-1;
        fj = nextInt()-1;
        //ar[si-1][sj-1] = 0;
        tt(si - 1, sj - 1,1);
        System.out.println("NO");
        //System.out.println(cnt);

    }

}


import java.io.*;
import java.math.BigInteger;
import java.util.*;

public class C {

    static BufferedReader in;
    static PrintWriter out;
    static StringTokenizer tok;

    public static void main(String[] args) throws IOException {
        try {
            in = new BufferedReader(new FileReader("rsq.in"));
            out = new PrintWriter(new File("rsq.out"));
        } catch (Exception e) {
            in = new BufferedReader(new InputStreamReader(System.in));
            out = new PrintWriter(new OutputStreamWriter(System.out));
        }
        tok = new StringTokenizer("");
        new C().solve();
        out.flush();
        out.close();
    }

    String nextTok() throws IOException {
        if (!tok.hasMoreTokens()) {
            tok = new StringTokenizer(in.readLine());
        }
        return tok.nextToken();
    }

    int nextInt() throws IOException {
        return Integer.parseInt(nextTok());
    }

    long nextLong() throws IOException {
        return Long.parseLong(nextTok());
    }

    double nextDouble() throws IOException {
        return Double.parseDouble(nextTok());
    }
    int n;
    int m;
    int fi;
    int fj;
    boolean used[][];
    int ar[][];
    int cnt = 0;

    void tt(int ii, int jj) {
        used[ii][jj] = true;

        if (ii == fi && jj == fj) {
            cnt++;
            if (cnt <= 1) used[ii][jj] = false;
        }
        if (ar[ii][jj] == -1) ii += 1;
        if (ii < n) ar[ii][jj] = -1;

        if (ii+1 < n && !used[ii+1][jj]) tt(ii+1,jj);
        if (ii-1 >= 0 && ii < n && !used[ii-1][jj]) tt(ii-1,jj);
        if (jj+1 < m && ii < n &&  !used[ii][jj+1]) tt(ii,jj+1);
        if (jj-1 >= 0 && ii < n && !used[ii][jj-1]) tt(ii,jj-1);
        if (ii < n) ar[ii][jj] = 0;

    }

    void solve() throws IOException {
        n = nextInt();
        m = nextInt();
        ar = new int[n][m];
        used = new boolean[n][m];
        for (int i = 0; i < n; i++) {
            String tmp = nextTok();
            for (int j = 0; j < m; j++) {
                if (tmp.charAt(j) == 'X') {
                    ar[i][j] = -1;
                } else ar[i][j] = 0;
            }
        }
        int si = nextInt();
        int sj = nextInt();
        fi = nextInt()-1;
        fj = nextInt()-1;
        tt(si - 1, sj - 1);
        //System.out.println(fi+" "+fj);
        //System.out.println(cnt);
        if (cnt >= 2 || (cnt == 1 && ar[fi][fj] == -1)) {
            System.out.println("YES");
            return;
        }
        System.out.println("NO");
    }

}


import java.io.*;
import java.math.BigInteger;
import java.util.*;

public class C {

    static BufferedReader in;
    static PrintWriter out;
    static StringTokenizer tok;

    public static void main(String[] args) throws IOException {
        try {
            in = new BufferedReader(new FileReader("rsq.in"));
            out = new PrintWriter(new File("rsq.out"));
        } catch (Exception e) {
            in = new BufferedReader(new InputStreamReader(System.in));
            out = new PrintWriter(new OutputStreamWriter(System.out));
        }
        tok = new StringTokenizer("");
        new C().solve();
        out.flush();
        out.close();
    }

    String nextTok() throws IOException {
        if (!tok.hasMoreTokens()) {
            tok = new StringTokenizer(in.readLine());
        }
        return tok.nextToken();
    }

    int nextInt() throws IOException {
        return Integer.parseInt(nextTok());
    }

    long nextLong() throws IOException {
        return Long.parseLong(nextTok());
    }

    double nextDouble() throws IOException {
        return Double.parseDouble(nextTok());
    }
    int n;
    int m;
    int fi;
    int fj;
    boolean used[][];
    int ar[][];
    int cnt = 0;

    void tt(int ii, int jj, int st) {
        used[ii][jj] = true;
        if (ii == fi && jj == fj) {
            used[ii][jj] = false;
            if (ar[ii][jj] == -1) {
                System.out.println("YES");
                System.exit(0);
            }
        }
        if (ar[ii][jj] == -1 && st != 1) return;
        ar[ii][jj] = -1;
        if (ii-1 >= 0 && !used[ii-1][jj]) tt(ii-1,jj,st+1);
        if (ii+1 < n && !used[ii+1][jj]) tt(ii+1,jj,st+1);
        if (jj+1 < m && !used[ii][jj+1]) tt(ii,jj+1,st+1);
        if (jj-1 >= 0 && !used[ii][jj-1]) tt(ii,jj-1,st+1);
        ar[ii][jj] = 0;

    }

    void solve() throws IOException {
        n = nextInt();
        m = nextInt();
        ar = new int[n][m];
        used = new boolean[n][m];
        for (int i = 0; i < n; i++) {
            String tmp = nextTok();
            for (int j = 0; j < m; j++) {
                if (tmp.charAt(j) == 'X') {
                    ar[i][j] = -1;
                } else ar[i][j] = 0;
            }
        }
        int si = nextInt();
        int sj = nextInt();
        fi = nextInt()-1;
        fj = nextInt()-1;
        //ar[si-1][sj-1] = 0;
        tt(si - 1, sj - 1,1);
        System.out.println("NO");
        //System.out.println(cnt);

    }

}


import java.io.*;
import java.math.BigInteger;
import java.util.*;

public class C {

    static BufferedReader in;
    static PrintWriter out;
    static StringTokenizer tok;

    public static void main(String[] args) throws IOException {
        try {
            in = new BufferedReader(new FileReader("rsq.in"));
            out = new PrintWriter(new File("rsq.out"));
        } catch (Exception e) {
            in = new BufferedReader(new InputStreamReader(System.in));
            out = new PrintWriter(new OutputStreamWriter(System.out));
        }
        tok = new StringTokenizer("");
        new C().solve();
        out.flush();
        out.close();
    }

    String nextTok() throws IOException {
        if (!tok.hasMoreTokens()) {
            tok = new StringTokenizer(in.readLine());
        }
        return tok.nextToken();
    }

    int nextInt() throws IOException {
        return Integer.parseInt(nextTok());
    }

    long nextLong() throws IOException {
        return Long.parseLong(nextTok());
    }

    double nextDouble() throws IOException {
        return Double.parseDouble(nextTok());
    }
    int n;
    int m;
    int fi;
    int fj;
    boolean used[][];
    int ar[][];
    int cnt = 0;

    void tt(int ii, int jj) {
        used[ii][jj] = true;

        if (ii == fi && jj == fj) {
            cnt++;
            if (cnt <= 1) used[ii][jj] = false;
        }
        if (ar[ii][jj] == -1) ii += 1;
        if (ii < n) ar[ii][jj] = -1;

        if (ii+1 < n && !used[ii+1][jj]) tt(ii+1,jj);
        if (ii-1 >= 0 && ii < n && !used[ii-1][jj]) tt(ii-1,jj);
        if (jj+1 < m && ii < n &&  !used[ii][jj+1]) tt(ii,jj+1);
        if (jj-1 >= 0 && ii < n && !used[ii][jj-1]) tt(ii,jj-1);
        if (ii < n) ar[ii][jj] = 0;

    }

    void solve() throws IOException {
        n = nextInt();
        m = nextInt();
        ar = new int[n][m];
        used = new boolean[n][m];
        for (int i = 0; i < n; i++) {
            String tmp = nextTok();
            for (int j = 0; j < m; j++) {
                if (tmp.charAt(j) == 'X') {
                    ar[i][j] = -1;
                } else ar[i][j] = 0;
            }
        }
        int si = nextInt();
        int sj = nextInt();
        fi = nextInt()-1;
        fj = nextInt()-1;
        tt(si - 1, sj - 1);
        //System.out.println(fi+" "+fj);
        //System.out.println(cnt);
        if (cnt >= 2 || (cnt == 1 && ar[fi][fj] == -1)) {
            if (n == 1 && m == 1) {
                System.out.println("NO");
                return;
            }
            System.out.println("YES");
            return;
        }
        System.out.println("NO");
    }

}