#include <bits/stdc++.h>
using namespace std;
const int Maxn=200005;
int hb,a[Maxn],b[Maxn],tot,val[30],now[30],n;
void work(int x,int res)
{
    if(!x) return ;
    hb=log2(x);
    if(!val[hb]) val[hb]=x,now[++tot]=res;
    else
        work(val[hb]^x,res);
}
void cal(int l,int r,int xx)
{
	int mid=(l+r)/2;
	b[mid]=now[xx];
	if(l==r) return ;
	cal(l,mid-1,xx-1),cal(mid+1,r,xx-1);
}
int main()
{
	scanf("%d",&n);
	for(int i=1;i<=n;i++)
		scanf("%d",&a[i]);
	sort(a+1,a+1+n);
	for(int i=1;i<=n;i++)
		work(a[i],a[i]);
	for(int i=0;i<tot;i++)
		if(!val[i])
		{
			tot=max(0,i-1);
			break;
		}
	if(!tot)
	{
		printf("0\n0");
		return 0;
	}
	int maxi=pow(2,tot);
	cal(1,maxi-1,tot);
	printf("%d\n",tot);
	for(int i=1;i<maxi;i++)
		b[i]=b[i-1]^b[i];
	for(int i=0;i<maxi;i++)
		printf("%d ",b[i]);
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
const int Maxn=400005;
int hb,a[2*Maxn],tot,val[30],now[30],n,b[2*Maxn],l2[Maxn];
bool V[2*Maxn];
void work(int x,int res)
{
    if(!x) return ;
    hb=l2[x];
    if(!val[hb]) val[hb]=res;
    else
        work(val[hb]^x,res);
}
void cal(int l,int r,int xx)
{
	int mid=(l+r)/2;
	a[mid]=now[xx];
	if(l==r) return ;
	cal(l,mid-1,xx-1),cal(mid+1,r,xx-1);
}
long long fast_pow(long long x,long long y)
{
	long long ans=1,now=x;
	while(y)
	{
		if(y&1) ans*=now;
		now*=now;
		y>>=1;
	}
	return ans;
}
int main()
{
	for(int i=1;i<=200000;i++)
		l2[i]=(int)log2(i);
	scanf("%d",&n);
	for(int i=1;i<=n;i++)
	{
		int x;
		scanf("%d",&x);
		work(x,x);
	}
	for(int i=0;i<=20;i++)
		if(val[i]) now[++tot]=val[i];
	cal(1,fast_pow(2,tot)-1,tot);
	int maxi=fast_pow(2,tot);
	for(int i=0;i<maxi;i++)
		b[i]=b[i-1]^a[i];
	for(int i=0;i<maxi;i++)
		V[b[i]]=true;
	for(int i=0;i<maxi;i++)
		if(!V[i])
		{
			printf("0\n0");
			return 0;
		}
	printf("%d\n",tot);
	for(int i=0;i<maxi;i++)
		printf("%d ",b[i]);
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
const int Maxn=200005;
int hb,a[Maxn],b[Maxn],tot,val[30],now[30],n;
void work(int x,int res)
{
    if(!x) return ;
    hb=log2(x);
    if(!val[hb]) val[hb]=x,now[++tot]=res;
    else
        work(val[hb]^x,res);
}
void cal(int l,int r,int xx)
{
	int mid=(l+r)/2;
	b[mid]=now[xx];
	if(l==r) return ;
	cal(l,mid-1,xx-1),cal(mid+1,r,xx-1);
}
int main()
{
	scanf("%d",&n);
	for(int i=1;i<=n;i++)
		scanf("%d",&a[i]);
	sort(a+1,a+1+n);
	for(int i=1;i<=n;i++)
		work(a[i],a[i]);
	for(int i=0;i<=tot;i++)
		if(!val[i])
		{
			tot=i;
			break;
		}
	if(!tot)
	{
		printf("0\n0");
		return 0;
	}
	int maxi=pow(2,tot);
	cal(1,maxi-1,tot);
	printf("%d\n",tot);
	for(int i=1;i<maxi;i++)
		b[i]=b[i-1]^b[i];
	for(int i=0;i<maxi;i++)
		printf("%d ",b[i]);
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
const int Maxn=200005;
int hb,a[2*Maxn],tot,val[30],now[30],x[Maxn],n,b[2*Maxn],l2[Maxn];
bool V[2*Maxn];
void work(int x,int res)
{
    if(!x) return ;
    hb=l2[x];
    if(!val[hb]) val[hb]=x,now[++tot]=res;
    else
        work(val[hb]^x,res);
}
void cal(int l,int r,int xx)
{
	int mid=(l+r)/2;
	a[mid]=now[xx];
	if(l==r) return ;
	cal(l,mid-1,xx-1),cal(mid+1,r,xx-1);
}
long long fast_pow(long long x,long long y)
{
	long long ans=1,now=x;
	while(y)
	{
		if(y&1) ans*=now;
		now*=now;
		y>>=1;
	}
	return ans;
}
int main()
{
//	freopen("E.in","r",stdin);
	for(int i=1;i<=200000;i++)
		l2[i]=(int)log2(i);
	scanf("%d",&n);
	for(int i=1;i<=n;i++)
		scanf("%d",&x[i]);
	sort(x+1,x+1+n);
	for(int i=1;i<=n;i++)
		work(x[i],x[i]);
	cal(1,fast_pow(2,tot)-1,tot);
	int maxi=fast_pow(2,tot),tmp=0;
	for(int i=0;i<maxi;i++)
	{
		tmp^=a[i];
		b[i]=tmp;
		V[tmp]=true;
	}
	for(int i=0;i<maxi;i++)
		if(!V[i])
		{
			printf("0\n0");
			return 0;
		}
	printf("%d\n",tot);
	for(int i=0;i<maxi;i++)
		printf("%d ",b[i]);
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
const int Maxn=200005;
int a[2*Maxn],tot,val[30],now[30],n,b[2*Maxn];
bool V[2*Maxn];
void work(int x,int res)
{
    if(!x) return ;
    int hb=log2(x);
    if(!val[hb]) val[hb]=res;
    else
        work(val[hb]^x,res);
}
void cal(int l,int r,int xx)
{
	int mid=(l+r)/2;
	a[mid]=now[xx];
	if(l>=r) return ;
	cal(l,mid-1,xx-1),cal(mid+1,r,xx-1);
}
int main()
{
	scanf("%d",&n);
	for(int i=1;i<=n;i++)
	{
		int x;
		scanf("%d",&x),work(x,x);
	}
	for(int i=0;i<=29;i++)
		if(val[i]) now[++tot]=val[i];
	cal(1,pow(2,tot)-1,tot);
	int maxi=pow(2,tot);
	for(int i=0;i<maxi;i++)
		b[i]=b[i-1]^a[i];
	for(int i=0;i<maxi;i++)
		a[i]=b[i];
	for(int i=0;i<=maxi;i++)
		V[a[i]]=true;
	for(int i=0;i<maxi;i++)
		if(!V[i])
		{
			printf("0\n0");
			return 0;
		}
	printf("%d\n",tot);
	for(int i=1;i<=maxi;i++)
		printf("%d ",b[i-1]);
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
const int Maxn=400005;
int a[2*Maxn],tot,val[30],now[30],n,b[2*Maxn],l2[Maxn];
bool V[2*Maxn];
void work(int x,int res)
{
    if(!x) return ;
    int hb=l2[x];
    if(!val[hb]) val[hb]=res;
    else
        work(val[hb]^x,res);
}
void cal(int l,int r,int xx)
{
	int mid=(l+r)/2;
	a[mid]=now[xx];
	if(l==r) return ;
	cal(l,mid-1,xx-1),cal(mid+1,r,xx-1);
}
long long fast_pow(long long x,long long y)
{
	long long ans=1,now=x;
	while(y)
	{
		if(y&1) ans*=now;
		now*=now;
		y>>=1;
	}
	return ans;
}
int main()
{
	for(int i=1;i<=200000;i++)
		l2[i]=(int)log2(i);
	scanf("%d",&n);
	for(int i=1;i<=n;i++)
	{
		int x;
		scanf("%d",&x);
		work(x,x);
	}
	for(int i=0;i<=20;i++)
		if(val[i]) now[++tot]=val[i];
	cal(1,fast_pow(2,tot)-1,tot);
	int maxi=fast_pow(2,tot);
	for(int i=0;i<maxi;i++)
		b[i]=b[i-1]^a[i];
	for(int i=0;i<maxi;i++)
		V[b[i]]=true;
	for(int i=0;i<maxi;i++)
		if(!V[i])
		{
			printf("0\n0");
			return 0;
		}
	printf("%d\n",tot);
	for(int i=0;i<maxi;i++)
		printf("%d ",b[i]);
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
const int Maxn=200005;
int a[2*Maxn],tot,val[30],now[30],n,b[2*Maxn];
bool V[2*Maxn];
void work(int x,int res)
{
    if(!x) return ;
    int hb=log2(x);
    if(!val[hb]) val[hb]=res;
    else
        work(val[hb]^x,res);
}
void cal(int l,int r,int xx)
{
	int mid=(l+r)/2;
	a[mid]=now[xx];
	if(l>=r) return ;
	cal(l,mid-1,xx-1),cal(mid+1,r,xx-1);
}
int main()
{
	scanf("%d",&n);
	for(int i=1;i<=n;i++)
	{
		int x;
		scanf("%d",&x),work(x,x);
	}
	for(int i=0;i<=29;i++)
		if(val[i]) now[++tot]=val[i];
	cal(1,pow(2,tot)-1,tot);
	int maxi=pow(2,tot);
	for(int i=0;i<maxi;i++)
		b[i]=b[i-1]^a[i];
	for(int i=0;i<maxi;i++)
		a[i]=b[i];
	for(int i=0;i<=maxi;i++)
		V[a[i]]=true;
	for(int i=0;i<maxi;i++)
		if(!V[i])
		{
			printf("0\n0");
			return 0;
		}
	printf("%d\n",tot);
	for(int i=1;i<=pow(2,tot);i++)
		printf("%d ",b[i-1]);
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
const int Maxn=200005;
int hb,a[Maxn],b[Maxn],tot,val[30],now[30],n;
void work(int x,int res)
{
    if(!x) return ;
    hb=log2(x);
    if(!val[hb]) val[hb]=x,now[++tot]=res;
    else
        work(val[hb]^x,res);
}
void cal(int l,int r,int xx)
{
	int mid=(l+r)/2;
	b[mid]=now[xx];
	if(l==r) return ;
	cal(l,mid-1,xx-1),cal(mid+1,r,xx-1);
}
int main()
{
	scanf("%d",&n);
	for(int i=1;i<=n;i++)
		scanf("%d",&a[i]);
	sort(a+1,a+1+n);
	for(int i=1;i<=n;i++)
		work(a[i],a[i]);
	int maxi=pow(2,tot);
	cal(1,maxi-1,tot);
	printf("%d\n",tot);
	for(int i=1;i<maxi;i++)
		b[i]=b[i-1]^b[i];
	for(int i=0;i<maxi;i++)
		printf("%d ",b[i]);
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

const int MAXX = 18;
const int MAXN = 2E5;
int a[MAXN];
bool valid[1 << MAXX];
vector<int> base;
vector<int> ans;

bool check(int k, int n)
{
    int big = 1 << k;
    fill(valid, valid + big, true);
    base.clear();
    valid[0] = false;
    for (int i = 0; i < n; i++)
    {
        int x = a[i];
        if (x >= big)
            continue;
        if (!valid[x])
            continue;
        base.push_back(x);
        vector<int> v;
        for (int i = 0; i < big; i++)
        {
            if (!valid[i])
                v.push_back(i ^ x);
        }
        for (int y : v)
            valid[y] = false;
    }
    return (int) base.size() == k;
}

void solve(int ind)
{
    if (ind < 0)
        return;
    if (ind == 0)
    {
        ans.push_back(ans.back() ^ base[ind]);
        return;
    }
    solve(ind - 1);
    ans.push_back(ans.back() ^ base[ind]);
    solve(ind - 1);
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int n;
    cin >> n;
    for (int i = 0; i < n; i++)
    {
        cin >> a[i];
    }
    int x = MAXX;
    while (!check(x, n))
        x--;
    ans.push_back(0);
    solve(x - 1);
    cout << x << "\n";
    for (int i = 0; i < (int) ans.size(); i++)
    {
        cout << ans[i] << " \n"[i == (int) ans.size() - 1];
    }
    return 0;
}


#include <cstdio>
#include <cstdlib>
#include <cassert>
#include <cstring>
#include <bitset>
#include <cmath>
#include <cctype>
#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <queue>
#include <map>
#include <set>
#include <sstream>
#include <iomanip>
using namespace std;
typedef long long ll;
typedef vector<long long> VI;
typedef unsigned long long ull;
const ll inff = 0x3f3f3f3f3f3f3f3f;
#define FOR(i,a,b) for(int i(a);i<=(b);++i)
#define FOL(i,a,b) for(int i(a);i>=(b);--i)
#define SZ(x) ((long long)(x).size())
#define REW(a,b) memset(a,b,sizeof(a))
#define inf int(0x3f3f3f3f)
#define si(a) scanf("%d",&a)
#define sl(a) scanf("%lld",&a)
#define sd(a) scanf("%lf",&a)
#define ss(a) scanf("%s",a)
#define mod ll(998244353)
#define pb push_back
#define eps 1e-6
#define lc d<<1
#define rc d<<1|1
#define Pll pair<ll,ll>
#define P pair<int,int>
#define pi acos(-1)
int n,a[200088],qw,ans[300008],b[300008],k,x,fd;
vector<int>g;
int main()
{
    cin.tie(0);
    cout.tie(0);
    cin>>n;
    FOR(i,1,n) si(a[i]);
    sort(a+1,a+n+1);
    FOL(i,18,0)
    {
        if(i==0) {cout<<0<<endl<<0<<endl;break;}
        qw=(1<<i)-1;k=0;x=1;g.clear();REW(b,0);
        ans[++k]=0;b[0]=1;
        while(k<(qw+1))
        {
            int df=0;
            for(int j=0;j<g.size();j++)
             if(!b[g[j]^ans[k]]&&(g[j]^ans[k])<=qw) {b[g[j]^ans[k]]=1,ans[++k]=g[j]^ans[k-1],df=1;break;}
            if(df) continue;
            for(int j=x;j<=n;j++)
             if(!b[a[j]^ans[k]]&&(a[j]^ans[k])<=qw) {b[a[j]^ans[k]]=1,ans[++k]=a[j]^ans[k-1],g.pb(a[j]);x=j;df=1;break;}
            if(!df) break;
        }
        if(k==qw+1)
        {
            cout<<i<<endl;
            FOR(j,1,qw+1) printf("%d ",ans[j]);
            puts("");break;
        }
    }
    return 0;
}


#include <cstdio>
#include <cstdlib>
#include <cassert>
#include <cstring>
#include <bitset>
#include <cmath>
#include <cctype>
#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <queue>
#include <map>
#include <set>
#include <sstream>
#include <iomanip>
using namespace std;
typedef long long ll;
typedef vector<long long> VI;
typedef unsigned long long ull;
const ll inff = 0x3f3f3f3f3f3f3f3f;
#define FOR(i,a,b) for(int i(a);i<=(b);++i)
#define FOL(i,a,b) for(int i(a);i>=(b);--i)
#define SZ(x) ((long long)(x).size())
#define REW(a,b) memset(a,b,sizeof(a))
#define inf int(0x3f3f3f3f)
#define si(a) scanf("%d",&a)
#define sl(a) scanf("%lld",&a)
#define sd(a) scanf("%lf",&a)
#define ss(a) scanf("%s",a)
#define mod ll(998244353)
#define pb push_back
#define eps 1e-6
#define lc d<<1
#define rc d<<1|1
#define Pll pair<ll,ll>
#define P pair<int,int>
#define pi acos(-1)
int n,a[200088],qw,ans[300008],b[300008],k,x,fd;
vector<int>g;
int main()
{
    cin.tie(0);
    cout.tie(0);
    cin>>n;
    FOR(i,1,n) si(a[i]);
    sort(a+1,a+n+1);
    FOL(i,18,0)
    {
        if(i==0) {cout<<0<<endl<<0<<endl;break;}
        qw=(1<<i)-1;k=0;x=1;g.clear();REW(b,0);
        ans[++k]=0;b[0]=1;
        while(k<(qw+1))
        {
            int df=0;
            for(int j=0;j<g.size();j++)
             if(!b[g[j]^ans[k]]&&(g[j]^ans[k])<=qw) {b[g[j]^ans[k]]=1,ans[++k]=g[j]^ans[k-1],df=1;break;}
            if(df) continue;
            for(int j=x;j<=n;j++)
             if(!b[a[j]^ans[k]]&&(a[j]^ans[k])<=qw) {b[a[j]^ans[k]]=1,ans[++k]=a[j]^ans[k-1],g.pb(a[j]);x=j;df=1;break;}
            if(!df) break;
        }
        if(k==qw+1)
        {
            FOR(j,1,qw+1) printf("%d ",ans[j]);
            puts("");break;
        }
    }
    return 0;
}


#include <cstdio>
#include <cstdlib>
#include <cassert>
#include <cstring>
#include <bitset>
#include <cmath>
#include <cctype>
#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <queue>
#include <map>
#include <set>
#include <sstream>
#include <iomanip>
using namespace std;
typedef long long ll;
typedef vector<long long> VI;
typedef unsigned long long ull;
const ll inff = 0x3f3f3f3f3f3f3f3f;
#define FOR(i,a,b) for(int i(a);i<=(b);++i)
#define FOL(i,a,b) for(int i(a);i>=(b);--i)
#define SZ(x) ((long long)(x).size())
#define REW(a,b) memset(a,b,sizeof(a))
#define inf int(0x3f3f3f3f)
#define si(a) scanf("%d",&a)
#define sl(a) scanf("%lld",&a)
#define sd(a) scanf("%lf",&a)
#define ss(a) scanf("%s",a)
#define mod ll(998244353)
#define pb push_back
#define eps 1e-6
#define lc d<<1
#define rc d<<1|1
#define Pll pair<ll,ll>
#define P pair<int,int>
#define pi acos(-1)
int n,a[200088],qw,ans[300008],b[300008],k,x,fd;
vector<int>g;
int main()
{
    cin.tie(0);
    cout.tie(0);
    cin>>n;
    FOR(i,1,n) si(a[i]);
    //sort(a+1,a+n+1);
    FOL(i,18,0)
    {
        if(i==0) {cout<<0<<endl<<0<<endl;break;}
        qw=(1<<i)-1;k=0;x=1;g.clear();REW(b,0);
        ans[++k]=0;b[0]=1;
        while(k<qw)
        {
            int df=0;
            for(int j=0;j<g.size();j++)
             if(!b[g[j]^ans[k]]&&(g[j]^ans[k])<=qw) {b[g[j]^ans[k]]=1,ans[k+1]=g[j]^ans[k],++k,df=1;break;}
            if(df) continue;
            for(int j=x;j<=n;j++)
             if(!b[a[j]^ans[k]]&&(a[j]^ans[k])<=qw) {b[a[j]^ans[k]]=1,ans[k+1]=a[j]^ans[k],++k, g.pb(a[j]);x=j;df=1;break;}
            if(!df) break;
        }
        //cout<<i<<" "<<qw<<" "<<k<<endl;
        if(k==qw)
        {
            cout<<i<<endl;
            FOR(j,1,qw) printf("%d ",ans[j]);
            FOR(i,0,qw) if(!b[i]) cout<<i<<endl;
            break;
        }
    }
    return 0;
}


#include <cstdio>
#include <cstdlib>
#include <cassert>
#include <cstring>
#include <bitset>
#include <cmath>
#include <cctype>
#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <queue>
#include <map>
#include <set>
#include <sstream>
#include <iomanip>
using namespace std;
typedef long long ll;
typedef vector<long long> VI;
typedef unsigned long long ull;
const ll inff = 0x3f3f3f3f3f3f3f3f;
#define FOR(i,a,b) for(int i(a);i<=(b);++i)
#define FOL(i,a,b) for(int i(a);i>=(b);--i)
#define SZ(x) ((long long)(x).size())
#define REW(a,b) memset(a,b,sizeof(a))
#define inf int(0x3f3f3f3f)
#define si(a) scanf("%d",&a)
#define sl(a) scanf("%lld",&a)
#define sd(a) scanf("%lf",&a)
#define ss(a) scanf("%s",a)
#define mod ll(998244353)
#define pb push_back
#define eps 1e-6
#define lc d<<1
#define rc d<<1|1
#define Pll pair<ll,ll>
#define P pair<int,int>
#define pi acos(-1)
int n,a[200088],qw,ans[300008],b[300008],k,x,fd;
vector<int>g;
int main()
{
    cin.tie(0);
    cout.tie(0);
    cin>>n;
    FOR(i,1,n) si(a[i]);
    //sort(a+1,a+n+1);
    FOL(i,18,0)
    {
        if(i==0) {cout<<0<<endl<<0<<endl;break;}
        qw=(1<<i)-1;k=0;x=1;g.clear();REW(b,0);
        ans[++k]=0;b[0]=1;
        while(k<(qw+1))
        {
            int df=0;
            for(int j=0;j<g.size();j++)
             if(!b[g[j]^ans[k]]&&(g[j]^ans[k])<=qw) {b[g[j]^ans[k]]=1,ans[k+1]=g[j]^ans[k],++k,df=1;break;}
            if(df) continue;
            for(int j=x;j<=n;j++)
             if(!b[a[j]^ans[k]]&&(a[j]^ans[k])<=qw) {b[a[j]^ans[k]]=1,ans[k+1]=a[j]^ans[k],++k, g.pb(a[j]);x=j;df=1;break;}
            if(!df) break;
        }
        //cout<<i<<" "<<qw<<" "<<k<<endl;
        if(k==qw+1)
        {
            cout<<i<<endl;
            FOR(j,1,qw+1) printf("%d ",ans[j]);
            cout<<endl;break;
        }
    }
    return 0;
}


#include <cstdio>
#include <cstdlib>
#include <cassert>
#include <cstring>
#include <bitset>
#include <cmath>
#include <cctype>
#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <queue>
#include <map>
#include <set>
#include <sstream>
#include <iomanip>
using namespace std;
typedef long long ll;
typedef vector<long long> VI;
typedef unsigned long long ull;
const ll inff = 0x3f3f3f3f3f3f3f3f;
#define FOR(i,a,b) for(int i(a);i<=(b);++i)
#define FOL(i,a,b) for(int i(a);i>=(b);--i)
#define SZ(x) ((long long)(x).size())
#define REW(a,b) memset(a,b,sizeof(a))
#define inf int(0x3f3f3f3f)
#define si(a) scanf("%d",&a)
#define sl(a) scanf("%lld",&a)
#define sd(a) scanf("%lf",&a)
#define ss(a) scanf("%s",a)
#define mod ll(998244353)
#define pb push_back
#define eps 1e-6
#define lc d<<1
#define rc d<<1|1
#define Pll pair<ll,ll>
#define P pair<int,int>
#define pi acos(-1)
int n,a[200088],qw,ans[300008],b[300008],k,x,fd;
vector<int>g;
int main()
{
    cin.tie(0);
    cout.tie(0);
    cin>>n;
    FOR(i,1,n) si(a[i]);
    sort(a+1,a+n+1);
    FOL(i,18,0)
    {
        if(i==0) {cout<<0<<endl<<0<<endl;break;}
        qw=(1<<i)-1;k=0;x=1;g.clear();REW(b,0);
        ans[++k]=0;b[0]=1;
        while(k<(qw+1))
        {
            int df=0;
            for(int j=0;j<g.size();j++)
             if(!b[g[j]^ans[k]]&&(g[j]^ans[k])<=qw) {b[g[j]^ans[k]]=1,ans[k+1]=g[j]^ans[k],++k,df=1;break;}
            if(df) continue;
            for(int j=x;j<=n;j++)
             if(!b[a[j]^ans[k]]&&(a[j]^ans[k])<=qw) {b[a[j]^ans[k]]=1,ans[k+1]=a[j]^ans[k],++k, g.pb(a[j]);x=j;df=1;break;}
            if(!df) break;
        }
        //cout<<i<<" "<<qw<<" "<<k<<endl;
        if(k==qw+1)
        {
            cout<<i<<endl;
            FOR(j,1,qw+1) printf("%d ",ans[j]);
            cout<<endl;break;
        }
    }
    return 0;
}


#include <cstdio>
#include <cstdlib>
#include <cassert>
#include <cstring>
#include <bitset>
#include <cmath>
#include <cctype>
#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <queue>
#include <map>
#include <set>
#include <sstream>
#include <iomanip>
using namespace std;
typedef long long ll;
typedef vector<long long> VI;
typedef unsigned long long ull;
const ll inff = 0x3f3f3f3f3f3f3f3f;
#define FOR(i,a,b) for(int i(a);i<=(b);++i)
#define FOL(i,a,b) for(int i(a);i>=(b);--i)
#define SZ(x) ((long long)(x).size())
#define REW(a,b) memset(a,b,sizeof(a))
#define inf int(0x3f3f3f3f)
#define si(a) scanf("%d",&a)
#define sl(a) scanf("%lld",&a)
#define sd(a) scanf("%lf",&a)
#define ss(a) scanf("%s",a)
#define mod ll(998244353)
#define pb push_back
#define eps 1e-6
#define lc d<<1
#define rc d<<1|1
#define Pll pair<ll,ll>
#define P pair<int,int>
#define pi acos(-1)
int n,a[200088],qw,ans[300008],b[300008],k,x,fd;
vector<int>g;
int main()
{
    cin.tie(0);
    cout.tie(0);
    cin>>n;
    FOR(i,1,n) si(a[i]);
    sort(a+1,a+n+1);
    FOL(i,18,0)
    {
        if(i==0) {cout<<0<<endl<<0<<endl;break;}
        qw=(1<<i)-1;k=0;x=1;g.clear();REW(b,0);
        ans[++k]=0;b[0]=1;
        while(k<(qw+1))
        {
            int df=0;
            for(int j=0;j<g.size();j++)
             if(!b[g[j]^ans[k]]&&(g[j]^ans[k])<=qw) {b[g[j]^ans[k]]=1,ans[++k]=g[j]^ans[k-1],df=1;break;}
            if(df) continue;
            for(int j=x;j<=n;j++)
             if(!b[a[j]^ans[k]]&&(a[j]^ans[k])<=qw) {b[a[j]^ans[k]]=1,ans[++k]=a[j]^ans[k-1],g.pb(a[j]);x=j;df=1;break;}
            if(!df) break;
        }
        //cout<<i<<" "<<qw<<" "<<k<<endl;
        if(k==qw+1)
        {
            cout<<i<<endl;
            FOR(j,1,qw+1) printf("%d ",ans[j]);
            cout<<endl;break;
        }
    }
    return 0;
}


//#pragma GCC optimize("trapv")
//#pragma GCC optimize("Ofast")
//#pragma GCC target("avx,avx2,fma")
//#pragma GCC optimize("unroll-loops")
#include<bits/stdc++.h>
#include<bits/extc++.h>
using namespace __gnu_pbds;
//using namespace __gnu_cxx;
#define pb push_back
#define all(x) x.begin(), x.end()
using namespace std;
using ll = long long;
using vi = vector<int>;
using pi = pair<ll, ll>;
using vpi = vector<pi>;
const int maxn = 3e5 + 33, mlg = 18, mod = 1e9 + 7;
int n;
vi a;

vi basis;
void insert(int x) {
	for(auto i : basis)
		x = min(x, x^i);
	if(x)
	basis.pb(x);
}
bool check(int x) {
	for(auto i : basis)
		x = min(x, x^i);
	return x==0;
}
int main() {
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	cin >> n;
	a.resize(n);
	for(auto &i : a) cin >> i;
	sort(all(a));
	vi ans = {0};
	int pos = 0;
	for(int i = 0; i <= 18 && pos < a.size(); i++) {
		for(int k = 1<<i; k--;)
			ans.pb(ans[k]);
		while(check(a[pos])) pos++;
//		cout << pos << " " << a[pos] << "pos\n";
		for(int j = 1<<i; j < 1<<(i+1); j++) {
			ans[j]^=a[pos];
		}
		insert(a[pos++]);
	}//resubmit
	set<int> x;
	int rr = 0;
	for(int i = 0; i < ans.size(); i++) {
		if(!x.insert(ans[i]).second) break;
		if(*x.rbegin()+1 == x.size()&& (i&(i+1))==0) {
//			cout << (*x.rbegin()) << " " << x.size() << "\n";
			rr = i+1;
		}
	}
	cout << __lg(rr) << "\n";
	for(int i = 0; i < rr; i++)
		cout << ans[i] << " ";
}


//#pragma GCC optimize("trapv")
//#pragma GCC optimize("Ofast")
//#pragma GCC target("avx,avx2,fma")
//#pragma GCC optimize("unroll-loops")
#include<bits/stdc++.h>
#include<bits/extc++.h>
using namespace __gnu_pbds;
//using namespace __gnu_cxx;
#define pb push_back
#define all(x) x.begin(), x.end()
using namespace std;
using ll = long long;
using vi = vector<int>;
using pi = pair<ll, ll>;
using vpi = vector<pi>;
const int maxn = 3e5 + 33, mlg = 18, mod = 1e9 + 7;
int n;
vi a;

vi basis;
void insert(int x) {
	for(auto i : basis)
		x = min(x, x^i);
	if(x)
	basis.pb(x);
}
bool check(int x) {
	for(auto i : basis)
		x = min(x, x^i);
	return x==0;
}
int main() {
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	cin >> n;
	a.resize(n);
	for(auto &i : a) cin >> i;
	sort(all(a));
	vi ans = {0};
	int pos = 0;
	for(int i = 0; i <= 20 && pos < a.size(); i++) {
		for(int k = 1<<i; k--;)
			ans.pb(ans[k]);
		while(pos<a.size()&&check(a[pos])) pos++;
		if(pos==a.size()) break;
//		cout << pos << " " << a[pos] << "pos\n";
		for(int j = 1<<i; j < 1<<(i+1); j++) {
			ans[j]^=a[pos];
		}
		insert(a[pos++]);
	}
	set<int> x;
	int rr = 0;
	for(int i = 0; i < ans.size(); i++) {
		if(!x.insert(ans[i]).second) break;
		if(*x.rbegin()+1 == x.size()&& (i&(i+1))==0) {
//			cout << (*x.rbegin()) << " " << x.size() << "\n";
			rr = i+1;
		}
	}
	cout << __lg(rr) << "\n";
	for(int i = 0; i < rr; i++)
		cout << ans[i] << " ";
}


//#pragma GCC optimize("trapv")
//#pragma GCC optimize("Ofast")
//#pragma GCC target("avx,avx2,fma")
//#pragma GCC optimize("unroll-loops")
#include<bits/stdc++.h>
#include<bits/extc++.h>
using namespace __gnu_pbds;
//using namespace __gnu_cxx;
#define pb push_back
#define all(x) x.begin(), x.end()
using namespace std;
using ll = long long;
using vi = vector<int>;
using pi = pair<ll, ll>;
using vpi = vector<pi>;
const int maxn = 3e5 + 33, mlg = 18, mod = 1e9 + 7;
int n;
vi a;
int check(vi& v) {
	vi t;
	for(auto i : v) {
		for(auto j : t) i = min(i, j^i);
		if(i) t.pb(i);
//		cout << i << "\n";
	}
	int x = -1, k = 1;
	while(k) {
		k = 0;
		for(auto i : t) {
//			cout << i << " " << (1<<(x+1)) << "\n";
			if((i&(1<<(x+1)))&&i<(1<<(x+2))) {
				k=1;
			}
		}
		x += k;
	}
	return x;
}
vi basis;
void insert(int x) {
	for(auto i : basis)
		x = min(x, x^i);
	if(x)
	basis.pb(x);
}
bool check(int x) {
	for(auto i : basis)
		x = min(x, x^i);
	return x==0;
}
int main() {
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	cin >> n;
	a.resize(n);
	for(auto &i : a) cin >> i;
	int t = check(a);
	cout << t+1 <<'\n';
	sort(all(a));
	vi ans = {0};
	int pos = 0;
	for(int i = 0; i <= t; i++) {
		for(int k = 1<<i; k--;)
			ans.pb(ans[k]);
		while(check(a[pos])) pos++;
//		cout << pos << " " << a[pos] << "pos\n";
		for(int j = 1<<i; j < 1<<(i+1); j++) {
			ans[j]^=a[pos];
		}
		insert(a[pos++]);
	}
	for(auto i : ans) cout << i << " ";
}


//#pragma GCC optimize("trapv")
//#pragma GCC optimize("Ofast")
//#pragma GCC target("avx,avx2,fma")
//#pragma GCC optimize("unroll-loops")
#include<bits/stdc++.h>
#include<bits/extc++.h>
using namespace __gnu_pbds;
//using namespace __gnu_cxx;
#define pb push_back
#define all(x) x.begin(), x.end()
using namespace std;
using ll = long long;
using vi = vector<int>;
using pi = pair<ll, ll>;
using vpi = vector<pi>;
const int maxn = 3e5 + 33, mlg = 18, mod = 1e9 + 7;
int n;
vi a;

vi basis;
void insert(int x) {
	for(auto i : basis)
		x = min(x, x^i);
	if(x)
	basis.pb(x);
}
bool check(int x) {
	for(auto i : basis)
		x = min(x, x^i);
	return x==0;
}
int main() {
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	cin >> n;
	a.resize(n);
	for(auto &i : a) cin >> i;
	sort(all(a));
	vi ans = {0};
	int pos = 0;
	for(int i = 0; i <= 18 && pos < a.size(); i++) {
		for(int k = 1<<i; k--;)
			ans.pb(ans[k]);
		while(check(a[pos])) pos++;
//		cout << pos << " " << a[pos] << "pos\n";
		for(int j = 1<<i; j < 1<<(i+1); j++) {
			ans[j]^=a[pos];
		}
		insert(a[pos++]);
	}
	set<int> x;
	int rr = 0;
	for(int i = 0; i < ans.size(); i++) {
		if(!x.insert(ans[i]).second) break;
		if(*x.rbegin()+1 == x.size()&& (i&(i+1))==0) {
//			cout << (*x.rbegin()) << " " << x.size() << "\n";
			rr = i+1;
		}
	}
	cout << __lg(rr) << "\n";
	for(int i = 0; i < rr; i++)
		cout << ans[i] << " ";
}


//#pragma GCC optimize("trapv")
//#pragma GCC optimize("Ofast")
//#pragma GCC target("avx,avx2,fma")
//#pragma GCC optimize("unroll-loops")
#include<bits/stdc++.h>
#include<bits/extc++.h>
using namespace __gnu_pbds;
//using namespace __gnu_cxx;
#define pb push_back
#define all(x) x.begin(), x.end()
using namespace std;
using ll = long long;
using vi = vector<int>;
using pi = pair<ll, ll>;
using vpi = vector<pi>;
const int maxn = 3e5 + 33, mlg = 18, mod = 1e9 + 7;
int n;
vi a;

vi basis;
void insert(int x) {
	for(auto i : basis)
		x = min(x, x^i);
	if(x)
	basis.pb(x);
}
bool check(int x) {
	for(auto i : basis)
		x = min(x, x^i);
	return x==0;
}
int main() {
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	cin >> n;
	a.resize(n);
	for(auto &i : a) cin >> i;
	sort(all(a));
	vi ans = {0};
	int pos = 0;
	for(int i = 0; i <= 20 && pos < a.size(); i++) {
		for(int k = 1<<i; k--;)
			ans.pb(ans[k]);
		while(i<a.size()&&check(a[pos])) pos++;
		if(i==a.size()) break;
//		cout << pos << " " << a[pos] << "pos\n";
		for(int j = 1<<i; j < 1<<(i+1); j++) {
			ans[j]^=a[pos];
		}
		insert(a[pos++]);
	}
	set<int> x;
	int rr = 0;
	for(int i = 0; i < ans.size(); i++) {
		if(!x.insert(ans[i]).second) break;
		if(*x.rbegin()+1 == x.size()&& (i&(i+1))==0) {
//			cout << (*x.rbegin()) << " " << x.size() << "\n";
			rr = i+1;
		}
	}
	cout << __lg(rr) << "\n";
	for(int i = 0; i < rr; i++)
		cout << ans[i] << " ";
}


#include <bits/stdc++.h>
using namespace std;

const int N = 220000;

int a[N], b[N], id[N];

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    int n; cin >> n;
    for (int i = 1; i <= n; i++) cin >> b[i];
    sort(b + 1, b + n + 1);
    for (int x = 17; x >= 0; x--) {
        int m = lower_bound(b + 1, b + n + 1, 1 << x) - b - 1;
        for (int i = 1; i <= m; i++) a[i] = b[i], id[i] = i;
        int r = 1;
        for (int i = 0; r <= m && i < 18; i++) {
            for (int j = r; j <= m; j++) if (a[j] >> i & 1) {
                swap(a[j], a[r]);
                swap(id[j], id[r]);
                break;
            }
            if (~a[r] >> i & 1) continue;
            for (int j = 1; j <= m; j++) {
                if (j == r) continue;
                if (a[j] >> i & 1) a[j] ^= a[r];
            }
            r++;
        }
        if (r - 1 == x) {
            cout << x << endl;
            for (int i = 0; i < 1 << x; i++) {
                int j = i ^ (i >> 1);
                int ans = 0;
                for (int k = 0; k < x; k++) if (j >> k & 1) ans ^= b[id[k + 1]];
                cout << ans << ' ';
            }
            cout << endl;
            return 0;
        }
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;

const int N = 220000;

int a[N], b[N], id[N];

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    int n; cin >> n;
    for (int i = 1; i <= n; i++) cin >> b[i];
    sort(b + 1, b + n + 1);
    for (int x = 18; x >= 0; x--) {
        int m = lower_bound(b + 1, b + n + 1, 1 << x) - b - 1;
        for (int i = 1; i <= m; i++) a[i] = b[i], id[i] = i;
        int r = 1;
        for (int i = 0; r <= m && i < x; i++) {
            for (int j = r; j <= m; j++) if (a[j] >> i & 1) {
                swap(a[j], a[r]);
                swap(id[j], id[r]);
                break;
            }
            if (~a[r] >> i & 1) continue;
            for (int j = 1; j <= m; j++) {
                if (j == r) continue;
                if (a[j] >> i & 1) a[j] ^= a[r];
            }
            r++;
        }
        if (r - 1 == x) {
            cout << x << endl;
            for (int i = 0; i < 1 << x; i++) {
                int j = i ^ (i >> 1);
                int ans = 0;
                for (int k = 0; k < x; k++) if (j >> k & 1) ans ^= b[id[k + 1]];
                cout << ans << ' ';
            }
            cout << endl;
            return 0;
        }
    }
    return 0;
}


#include <bits/stdc++.h>

#ifdef NON_SUBMIT
#define TEST(n) (n)
#define tout cerr
#else
#define TEST(n) ((void)0)
#define tout cin
#endif

using namespace std;

vector<int> ans(1), A, B;
int idx[18];

int main()
{
	ios::sync_with_stdio(false);
	cin.tie(NULL); cout.tie(NULL);
	TEST(freopen("input.txt","r",stdin));
	TEST(freopen("output.txt","w",stdout));
	TEST(freopen("debug.txt","w",stderr));
	int N, res=0, p=0;
	for(int i=0;i<18;i++) idx[i]=-1;
	cin>>N;
	A.resize(N);
	for(int i=0;i<N;i++) cin>>A[i];
	sort(A.begin(),A.end());
	B=A;
	for(int j=0;j<18;j++) {
		for(int i=0;i<N;i++) {
			if(B[i]&(1<<j)) {
				if(idx[j]==-1) idx[j]=i;
				else B[j]^=B[idx[j]];
			}
		}
	}
	for(;res<18 && idx[res]!=-1;res++);
	cout<<res<<'\n';
	for(int i=0;i<res;i++) for(int j=(1<<i)-1;j>=0;j--) ans.push_back(A[idx[i]]^ans[j]);
	for(auto a: ans) cout<<a<<' ';
	return 0;
}


#include <bits/stdc++.h>

#ifdef NON_SUBMIT
#define TEST(n) (n)
#define tout cerr
#else
#define TEST(n) ((void)0)
#define tout cin
#endif

using namespace std;

vector<int> ans(1), A, B;
int idx[18];

int main()
{
	ios::sync_with_stdio(false);
	cin.tie(NULL); cout.tie(NULL);
	TEST(freopen("input.txt","r",stdin));
	TEST(freopen("output.txt","w",stdout));
	TEST(freopen("debug.txt","w",stderr));
	int N, res=0, p=0;
	for(int i=0;i<18;i++) idx[i]=-1;
	cin>>N;
	A.resize(N);
	for(int i=0;i<N;i++) cin>>A[i];
	sort(A.begin(),A.end());
	B=A;
	for(int j=0;j<18;j++) {
		for(int i=0;i<N;i++) {
			if(B[i]&(1<<j)) {
				if(idx[j]==-1) idx[j]=i;
				else B[j]^=B[idx[j]];
			}
		}
	}
	for(;res<18 && idx[res]!=-1;res++);
	for(int i=0;i<18;i++) idx[i]=-1;
	for(int j=0;j<18;j++) {
		for(int i=0;i<N && A[i]<(1<<res);i++) {
			if(B[i]&(1<<j)) {
				if(idx[j]==-1) idx[j]=i;
				else B[j]^=B[idx[j]];
			}
		}
	}
	cout<<res<<'\n';
	for(int i=0;i<res;i++) for(int j=(1<<i)-1;j>=0;j--) ans.push_back(A[idx[i]]^ans[j]);
	for(auto a: ans) cout<<a<<' ';
	return 0;
}


#include <bits/stdc++.h>

#ifdef NON_SUBMIT
#define TEST(n) (n)
#define tout cerr
#else
#define TEST(n) ((void)0)
#define tout cin
#endif

using namespace std;

vector<int> ori(1), A, B;
int idx[19];

int main()
{
	ios::sync_with_stdio(false);
	cin.tie(NULL); cout.tie(NULL);
	TEST(freopen("input.txt","r",stdin));
	TEST(freopen("output.txt","w",stdout));
	TEST(freopen("debug.txt","w",stderr));
	int N, ans=0, p=0;
	for(int i=1;i<18;i++) {
		vector<int> temp(1,i); idx[i]=-1;
		for(auto a: ori) temp.push_back(a);
		for(auto a: temp) ori.push_back(a);
	}
	idx[0]=idx[18]=-1;
	cin>>N;
	A.resize(N); B.resize(N);
	for(int i=0;i<N;i++) {
		cin>>A[i]; B[i]=A[i];
	}
	for(int j=0;j<18;j++) {
		for(int i=0;i<N;i++) {
			if(B[i]&(1<<j)) {
				if(idx[j]==-1) idx[j]=i;
				else B[j]^=B[idx[j]];
			}
		}
	}
	for(;idx[ans]!=-1;ans++);
	cout<<ans<<"\n0";
	for(int i=1;i<(1<<ans);i++) cout<<' '<<(p^=A[idx[ori[i-1]]]);
	cout<<'\n';
	return 0;
}


#include <bits/stdc++.h>

#ifdef NON_SUBMIT
#define TEST(n) (n)
#define tout cerr
#else
#define TEST(n) ((void)0)
#define tout cin
#endif

using namespace std;

vector<int> ans(1), A, B;
int idx[18], V[18];

int main()
{
	ios::sync_with_stdio(false);
	cin.tie(NULL); cout.tie(NULL);
	TEST(freopen("input.txt","r",stdin));
	TEST(freopen("output.txt","w",stdout));
	TEST(freopen("debug.txt","w",stderr));
	int N, res=0, p=0;
	for(int i=0;i<18;i++) idx[i]=-1;
	cin>>N;
	A.resize(N);
	for(int i=0;i<N;i++) cin>>A[i];
	sort(A.begin(),A.end());
	B=A;
	for(int j=0;j<18;j++) {
		for(int i=0;i<N;i++) {
			if(B[i]&(1<<j)) {
				if(idx[j]==-1) {
					idx[j]=i;
					swap(B[i],V[j]);
				}
				else B[i]^=V[j];
			}
		}
	}
	for(;res<18 && idx[res]!=-1;res++);
	cout<<res<<'\n';
	for(int i=0;i<res;i++) for(int j=(1<<i)-1;j>=0;j--) ans.push_back(A[idx[i]]^ans[j]);
	for(auto a: ans) cout<<a<<' ';
	return 0;
}


#include <bits/stdc++.h>

#ifdef NON_SUBMIT
#define TEST(n) (n)
#define tout cerr
#else
#define TEST(n) ((void)0)
#define tout cin
#endif

using namespace std;

vector<int> ans(1), A, B;
int idx[19];

int main()
{
	ios::sync_with_stdio(false);
	cin.tie(NULL); cout.tie(NULL);
	TEST(freopen("input.txt","r",stdin));
	TEST(freopen("output.txt","w",stdout));
	TEST(freopen("debug.txt","w",stderr));
	int N, res=0, p=0;
	idx[0]=idx[18]=-1;
	cin>>N;
	A.resize(N);
	for(int i=0;i<N;i++) cin>>A[i];
	B=A;
	for(int j=0;j<18;j++) {
		for(int i=0;i<N;i++) {
			if(B[i]&(1<<j)) {
				if(idx[j]==-1) idx[j]=i;
				else B[j]^=B[idx[j]];
			}
		}
	}
	for(;idx[res]!=-1;res++);
	cout<<res<<'\n';
	for(int i=1;i<res;i++) for(int j=(1<<i)-1;i>=0;j--) ans.push_back(A[idx[i]]^ans[j]);
	for(auto a: ans) cout<<a<<' ';
	return 0;
}


#include <bits/stdc++.h>

#ifdef NON_SUBMIT
#define TEST(n) (n)
#define tout cerr
#else
#define TEST(n) ((void)0)
#define tout cin
#endif

using namespace std;

vector<int> ans(1), A, B;
int idx[19];

int main()
{
	ios::sync_with_stdio(false);
	cin.tie(NULL); cout.tie(NULL);
	TEST(freopen("input.txt","r",stdin));
	TEST(freopen("output.txt","w",stdout));
	TEST(freopen("debug.txt","w",stderr));
	int N, res=0, p=0;
	idx[0]=idx[18]=-1;
	cin>>N;
	A.resize(N);
	for(int i=0;i<N;i++) cin>>A[i];
	B=A;
	for(int j=0;j<18;j++) {
		for(int i=0;i<N;i++) {
			if(B[i]&(1<<j)) {
				if(idx[j]==-1) idx[j]=i;
				else B[j]^=B[idx[j]];
			}
		}
	}
	for(;idx[res]!=-1;res++);
	cout<<res<<'\n';
	for(int i=1;i<res;i++) for(int j=(1<<i)-1;i>=0;j--) ans.push_back(A[idx[i]]^ans[j])
	for(auto a: ans) cout<<a<<' ';
	return 0;
}


#include <bits/stdc++.h>

#ifdef NON_SUBMIT
#define TEST(n) (n)
#define tout cerr
#else
#define TEST(n) ((void)0)
#define tout cin
#endif

using namespace std;

vector<int> ans(1), A, B;
int idx[18], V[18];

int main()
{
	ios::sync_with_stdio(false);
	cin.tie(NULL); cout.tie(NULL);
	TEST(freopen("input.txt","r",stdin));
	TEST(freopen("output.txt","w",stdout));
	TEST(freopen("debug.txt","w",stderr));
	int N, res=0, p=0;
	for(int i=0;i<18;i++) idx[i]=-1;
	cin>>N;
	A.resize(N);
	for(int i=0;i<N;i++) cin>>A[i];
	sort(A.begin(),A.end());
	B=A;
	for(int j=17;j>=0;j--) {
		for(int i=0;i<N;i++) {
			if(B[i]&(1<<j)) {
				if(idx[j]==-1) {
					idx[j]=i;
					swap(B[i],V[j]);
				}
				else B[i]^=V[j];
			}
		}
	}
	for(;res<18 && idx[res]!=-1;res++);
	cout<<res<<'\n';
	for(int i=0;i<res;i++) for(int j=(1<<i)-1;j>=0;j--) ans.push_back(A[idx[i]]^ans[j]);
	for(auto a: ans) cout<<a<<' ';
	return 0;
}


#include <bits/stdc++.h>

#ifdef NON_SUBMIT
#define TEST(n) (n)
#define tout cerr
#else
#define TEST(n) ((void)0)
#define tout cin
#endif

using namespace std;

vector<int> ans(1), A, B;
int idx[18];

int main()
{
	ios::sync_with_stdio(false);
	cin.tie(NULL); cout.tie(NULL);
	TEST(freopen("input.txt","r",stdin));
	TEST(freopen("output.txt","w",stdout));
	TEST(freopen("debug.txt","w",stderr));
	int N, res=0, p=0;
	for(int i=0;i<18;i++) idx[i]=-1;
	cin>>N;
	A.resize(N);
	for(int i=0;i<N;i++) cin>>A[i];
	B=A;
	for(int j=0;j<18;j++) {
		for(int i=0;i<N;i++) {
			if(B[i]&(1<<j)) {
				if(idx[j]==-1) idx[j]=i;
				else B[j]^=B[idx[j]];
			}
		}
	}
	for(;res<18 && idx[res]!=-1;res++);
	cout<<res<<'\n';
	for(int i=0;i<res;i++) for(int j=(1<<i)-1;j>=0;j--) ans.push_back(A[idx[i]]^ans[j]);
	for(auto a: ans) cout<<a<<' ';
	return 0;
}


#include <bits/stdc++.h>

#ifdef NON_SUBMIT
#define TEST(n) (n)
#define tout cerr
#else
#define TEST(n) ((void)0)
#define tout cin
#endif

using namespace std;

vector<int> ori(1), A, B;
int idx[19];

int main()
{
	ios::sync_with_stdio(false);
	cin.tie(NULL); cout.tie(NULL);
	TEST(freopen("input.txt","r",stdin));
	TEST(freopen("output.txt","w",stdout));
	TEST(freopen("debug.txt","w",stderr));
	int N, ans=0, p=0;
	for(int i=1;i<18;i++) {
		vector<int> temp(1,i); idx[i]=-1;
		for(auto a: ori) temp.push_back(a);
		for(auto a: temp) ori.push_back(a);
	}
	idx[0]=idx[18]=-1;
	cin>>N;
	A.resize(N);
	for(int i=0;i<N;i++) cin>>A[i];
	sort(A.begin(),A.end()); B=A;
	for(int j=0;j<18;j++) {
		for(int i=0;i<N;i++) {
			if(B[i]&(1<<j)) {
				if(idx[j]==-1) idx[j]=i;
				else B[j]^=B[idx[j]];
			}
		}
	}
	for(;idx[ans]!=-1;ans++);
	cout<<ans<<"\n0";
	for(int i=1;i<(1<<ans);i++) cout<<' '<<(p^=A[idx[ori[i-1]]]);
	cout<<'\n';
	return 0;
}


#include <bits/stdc++.h>

#ifdef NON_SUBMIT
#define TEST(n) (n)
#define tout cerr
#else
#define TEST(n) ((void)0)
#define tout cin
#endif

using namespace std;

vector<int> ans(1), A, B;
int idx[19];

int main()
{
	ios::sync_with_stdio(false);
	cin.tie(NULL); cout.tie(NULL);
	TEST(freopen("input.txt","r",stdin));
	TEST(freopen("output.txt","w",stdout));
	TEST(freopen("debug.txt","w",stderr));
	int N, res=0, p=0;
	idx[0]=idx[18]=-1;
	cin>>N;
	A.resize(N);
	for(int i=0;i<N;i++) cin>>A[i];
	B=A;
	for(int j=0;j<18;j++) {
		for(int i=0;i<N;i++) {
			if(B[i]&(1<<j)) {
				if(idx[j]==-1) idx[j]=i;
				else B[j]^=B[idx[j]];
			}
		}
	}
	for(;idx[res]!=-1;res++);
	cout<<res<<'\n';
	for(int i=0;i<res;i++) for(int j=(1<<i)-1;j>=0;j--) ans.push_back(A[idx[i]]^ans[j]);
	for(auto a: ans) cout<<a<<' ';
	return 0;
}


#include <bits/stdc++.h>

#ifdef NON_SUBMIT
#define TEST(n) (n)
#define tout cerr
#else
#define TEST(n) ((void)0)
#define tout cin
#endif

using namespace std;

vector<int> ans(1), A, B;
int idx[19];

int main()
{
	ios::sync_with_stdio(false);
	cin.tie(NULL); cout.tie(NULL);
	TEST(freopen("input.txt","r",stdin));
	TEST(freopen("output.txt","w",stdout));
	TEST(freopen("debug.txt","w",stderr));
	int N, res=0, p=0;
	idx[0]=idx[18]=-1;
	cin>>N;
	A.resize(N);
	for(int i=0;i<N;i++) cin>>A[i];
	B=A;
	for(int j=0;j<18;j++) {
		for(int i=0;i<N;i++) {
			if(B[i]&(1<<j)) {
				if(idx[j]==-1) idx[j]=i;
				else B[j]^=B[idx[j]];
			}
		}
	}
	for(;idx[res]!=-1;res++);
	cout<<res<<''\n';
	for(int i=1;i<res;i++) for(int j=(1<<i)-1;i>=0;j--) ans.push_back(A[idx[i]]^ans[j])
	for(auto a: ans) cout<<a<<' ';
	return 0;
}


#include <bits/stdc++.h>

#ifdef NON_SUBMIT
#define TEST(n) (n)
#define tout cerr
#else
#define TEST(n) ((void)0)
#define tout cin
#endif

using namespace std;

vector<int> ans(1), A, B;
int idx[19];

int main()
{
	ios::sync_with_stdio(false);
	cin.tie(NULL); cout.tie(NULL);
	TEST(freopen("input.txt","r",stdin));
	TEST(freopen("output.txt","w",stdout));
	TEST(freopen("debug.txt","w",stderr));
	int N, res=0, p=0;
	for(int i=0;i<19;i++) idx[i]==-1;
	cin>>N;
	A.resize(N);
	for(int i=0;i<N;i++) cin>>A[i];
	B=A;
	for(int j=0;j<18;j++) {
		for(int i=0;i<N;i++) {
			if(B[i]&(1<<j)) {
				if(idx[j]==-1) idx[j]=i;
				else B[j]^=B[idx[j]];
			}
		}
	}
	for(;idx[res]!=-1;res++);
	cout<<res<<'\n';
	for(int i=0;i<res;i++) for(int j=(1<<i)-1;j>=0;j--) ans.push_back(A[idx[i]]^ans[j]);
	for(auto a: ans) cout<<a<<' ';
	return 0;
}


#include <bits/stdc++.h>

#ifdef NON_SUBMIT
#define TEST(n) (n)
#define tout cerr
#else
#define TEST(n) ((void)0)
#define tout cin
#endif

using namespace std;

vector<int> ans(1), A, B;
int idx[18];

int main()
{
	ios::sync_with_stdio(false);
	cin.tie(NULL); cout.tie(NULL);
	TEST(freopen("input.txt","r",stdin));
	TEST(freopen("output.txt","w",stdout));
	TEST(freopen("debug.txt","w",stderr));
	int N, res=0, p=0;
	for(int i=0;i<18;i++) idx[i]=-1;
	cin>>N;
	A.resize(N);
	for(int i=0;i<N;i++) cin>>A[i];
	sort(A.begin(),A.end());
	B=A;
	for(int j=0;j<18;j++) {
		for(int i=0;i<N;i++) {
			if(B[i]&(1<<j)) {
				if(idx[j]==-1) idx[j]=i;
				else B[i]^=B[idx[j]];
			}
		}
	}
	for(;res<18 && idx[res]!=-1;res++);
	for(int i=0;i<18;i++) idx[i]=-1;
	B=A;
	for(int j=0;j<18;j++) {
		for(int i=0;i<N && A[i]<(1<<res);i++) {
			if(B[i]&(1<<j)) {
				if(idx[j]==-1) idx[j]=i;
				else B[i]^=B[idx[j]];
			}
		}
	}
	cout<<res<<'\n';
	for(int i=0;i<res;i++) for(int j=(1<<i)-1;j>=0;j--) ans.push_back(A[idx[i]]^ans[j]);
	for(auto a: ans) cout<<a<<' ';
	return 0;
}


#include <bits/stdc++.h>

#ifdef NON_SUBMIT
#define TEST(n) (n)
#define tout cerr
#else
#define TEST(n) ((void)0)
#define tout cin
#endif

using namespace std;

vector<int> ans(1), A, B;
int idx[18];

int main()
{
	ios::sync_with_stdio(false);
	cin.tie(NULL); cout.tie(NULL);
	TEST(freopen("input.txt","r",stdin));
	TEST(freopen("output.txt","w",stdout));
	TEST(freopen("debug.txt","w",stderr));
	int N, res=0, p=0;
	for(int i=0;i<18;i++) idx[i]=-1;
	cin>>N;
	A.resize(N);
	for(int i=0;i<N;i++) cin>>A[i];
	sort(A.begin(),A.end());
	B=A;
	for(int j=0;j<18;j++) {
		for(int i=0;i<N;i++) {
			if(B[i]&(1<<j)) {
				if(idx[j]==-1) idx[j]=i;
				else B[j]^=B[idx[j]];
			}
		}
	}
	for(;res<18 && idx[res]!=-1;res++);
	for(int i=0;i<18;i++) idx[i]=-1;
	B=A;
	for(int j=0;j<18;j++) {
		for(int i=0;i<N && A[i]<(1<<res);i++) {
			if(B[i]&(1<<j)) {
				if(idx[j]==-1) idx[j]=i;
				else B[j]^=B[idx[j]];
			}
		}
	}
	cout<<res<<'\n';
	for(int i=0;i<res;i++) for(int j=(1<<i)-1;j>=0;j--) ans.push_back(A[idx[i]]^ans[j]);
	for(auto a: ans) cout<<a<<' ';
	return 0;
}


#include <bits/stdc++.h>

#ifdef NON_SUBMIT
#define TEST(n) (n)
#define tout cerr
#else
#define TEST(n) ((void)0)
#define tout cin
#endif

using namespace std;

vector<int> ori(1), A, B;
int idx[19], V[19];

int main()
{
	ios::sync_with_stdio(false);
	cin.tie(NULL); cout.tie(NULL);
	TEST(freopen("input.txt","r",stdin));
	TEST(freopen("output.txt","w",stdout));
	TEST(freopen("debug.txt","w",stderr));
	int N, ans=0, p=0;
	for(int i=1;i<18;i++) {
		vector<int> temp(1,i); idx[i]=-1;
		for(auto a: ori) temp.push_back(a);
		for(auto a: temp) ori.push_back(a);
	}
	idx[0]=idx[18]=-1;
	cin>>N;
	A.resize(N);
	for(int i=0;i<N;i++) cin>>A[i];
	sort(A.begin(),A.end()); B=A;
	for(int j=17;j>=0;j--) {
		for(int i=0;i<N;i++) {
			if(B[i]&(1<<j)) {
				if(idx[j]==-1) idx[j]=i, swap(V[j],B[i]);
				else B[i]^=V[j];
			}
		}
	}
	for(;idx[ans]!=-1;ans++);
	cout<<ans<<"\n0";
	for(int i=1;i<(1<<ans);i++) cout<<' '<<(p^=A[idx[ori[i-1]]]);
	cout<<'\n';
	return 0;
}


#include <bits/stdc++.h>

#ifdef NON_SUBMIT
#define TEST(n) (n)
#define tout cerr
#else
#define TEST(n) ((void)0)
#define tout cin
#endif

using namespace std;

vector<int> ans(1), A, B;
int idx[19];

int main()
{
	ios::sync_with_stdio(false);
	cin.tie(NULL); cout.tie(NULL);
	TEST(freopen("input.txt","r",stdin));
	TEST(freopen("output.txt","w",stdout));
	TEST(freopen("debug.txt","w",stderr));
	int N, res=0, p=0;
	idx[0]=idx[18]=-1;
	cin>>N;
	A.resize(N);
	for(int i=0;i<N;i++) cin>>A[i];
	B=A;
	for(int j=0;j<18;j++) {
		for(int i=0;i<N;i++) {
			if(B[i]&(1<<j)) {
				if(idx[j]==-1) idx[j]=i;
				else B[j]^=B[idx[j]];
			}
		}
	}
	for(;idx[res]!=-1;res++);
	cout<<res<<'\n';
	for(int i=0;i<res;i++) for(int j=(1<<i)-1;i>=0;j--) ans.push_back(A[idx[i]]^ans[j]);
	for(auto a: ans) cout<<a<<' ';
	return 0;
}


#include <bits/stdc++.h>

#ifdef NON_SUBMIT
#define TEST(n) (n)
#define tout cerr
#else
#define TEST(n) ((void)0)
#define tout cin
#endif

using namespace std;

vector<int> ans(1), A, B;
int idx[19];

int main()
{
	ios::sync_with_stdio(false);
	cin.tie(NULL); cout.tie(NULL);
	TEST(freopen("input.txt","r",stdin));
	TEST(freopen("output.txt","w",stdout));
	TEST(freopen("debug.txt","w",stderr));
	int N, res=0, p=0;
	for(int i=0;i<19;i++) idx[i]==-1;
	cin>>N;
	A.resize(N);
	for(int i=0;i<N;i++) cin>>A[i];
	B=A;
	for(int j=0;j<18;j++) {
		for(int i=0;i<N;i++) {
			if(B[i]&(1<<j)) {
				if(idx[j]==-1) idx[j]=i;
				else B[j]^=B[idx[j]];
			}
		}
	}
	for(;idx[res]!=-1;res++);
	cout<<res<<'\n';
	for(int i=0;i<res;i++) {
	    ans.push_back(A[idx[i]]);
	    for(int j=(1<<i)-1;j>=0;j--) ans.push_back(A[idx[i]]^ans[j]);
	}
	for(auto a: ans) cout<<a<<' ';
	return 0;
}


#include <bits/stdc++.h>

#ifdef NON_SUBMIT
#define TEST(n) (n)
#define tout cerr
#else
#define TEST(n) ((void)0)
#define tout cin
#endif

using namespace std;

vector<int> ori(1), A, B;
int idx[19], V[19];

int main()
{
	ios::sync_with_stdio(false);
	cin.tie(NULL); cout.tie(NULL);
	TEST(freopen("input.txt","r",stdin));
	TEST(freopen("output.txt","w",stdout));
	TEST(freopen("debug.txt","w",stderr));
	int N, ans=0, p=0;
	for(int i=1;i<18;i++) {
		vector<int> temp(1,i); idx[i]=-1;
		for(auto a: ori) temp.push_back(a);
		for(auto a: temp) ori.push_back(a);
	}
	idx[0]=idx[18]=-1;
	cin>>N;
	A.resize(N); B.resize(N);
	for(int i=0;i<N;i++) {
		cin>>A[i]; B[i]=A[i];
	}
	for(int j=17;j>=0;j--) {
		for(int i=0;i<N;i++) {
			if(B[i]&(1<<j)) {
				if(idx[j]==-1) idx[j]=i, swap(V[j],B[i]);
				else B[i]^=V[j];
			}
		}
	}
	for(;idx[ans]!=-1;ans++);
	cout<<ans<<"\n0";
	for(int i=1;i<(1<<ans);i++) cout<<' '<<(p^=A[idx[ori[i-1]]]);
	cout<<'\n';
	return 0;
}


#include <bits/stdc++.h>
using namespace std;

#define N 200020

int n, s[N];

vector<int> ans;

void input(int l, int r, vector<int> &todae) {
	int m = l + r >> 1;
	ans[m] = todae.back();
	todae.pop_back();
	if (l == r) return;
	input(l, m - 1, todae);
	for (int i = l; i < m; i ++) ans[i+m+1-l] = ans[i];
}

bool check(int x) {

	vector<int> now;
	for (int i = 1; i <= n; i ++) if (!(s[i]>>x)) now.push_back(s[i]);
	if (now.size() < x) return 0;
	vector<bool> vis(1<<x);
	vector<int> a;
	vector<int> todae;
	vis[0] = 1;
	a.push_back(0);
	for (int f:now) {
		if (vis[f]) continue;
		else {
			todae.push_back(f);
			int sz = a.size();
			for (int k = 0; k < sz; k ++) {
				vis[a[k]^f] = 1;
				a.push_back(a[k]^f);
			}
		}
	}
	if (todae.size() < x) return 0;
	ans.resize(1<<x);
	ans[0] = 0;
	if (x) input(1, (1<<x) - 1, todae);
	for (int i = 1; i < (1<<x); i ++) ans[i] ^= ans[i-1];
}

int main() {
	scanf("%d", &n);
	for (int i = 1; i <= n; i ++) scanf("%d", &s[i]);
	int x = 19;
	while (1) {
		if (check(x)) {
			printf("%d\n", x);
			for (int i = 0; i < (1<<x); i ++) printf("%d ", ans[i]);
			break;
		}
		x --;
	}
}


#include <bits/stdc++.h>
using namespace std;

#define N 200020

int n, s[N];

vector<int> ans;

void input(int l, int r, vector<int> &todae) {
	int m = l + r >> 1;
	ans[m] = todae.back();
	todae.pop_back();
	if (l == r) return;
	input(l, m - 1, todae);
	for (int i = l; i < m; i ++) ans[i+m+1-l] = ans[i];
}

bool check(int x) {

	vector<int> now;
	for (int i = 1; i <= n; i ++) if (!(s[i]>>x)) now.push_back(s[i]);
	if (now.size() < x) return 0;
	vector<bool> vis(1<<x);
	vector<int> a;
	vector<int> todae;
	vis[0] = 1;
	a.push_back(0);
	for (int f:now) {
		if (vis[f]) continue;
		else {
			todae.push_back(f);
			int sz = a.size();
			for (int k = 0; k < sz; k ++) {
				vis[a[k]^f] = 1;
				a.push_back(a[k]^f);
			}
		}
	}
	if (todae.size() < x) return 0;
	ans.resize(1<<x);
	ans[0] = 0;
	if (x) input(1, (1<<x) - 1, todae);
	for (int i = 1; i < (1<<x); i ++) ans[i] ^= ans[i-1];
        return 1;
}

int main() {
	scanf("%d", &n);
	for (int i = 1; i <= n; i ++) scanf("%d", &s[i]);
	int x = 19;
	while (1) {
		if (check(x)) {
			printf("%d\n", x);
			for (int i = 0; i < (1<<x); i ++) printf("%d ", ans[i]);
			break;
		}
		x --;
	}
}


#include <bits/stdc++.h>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/assoc_container.hpp>

using namespace std;
using namespace __gnu_pbds;

typedef long long ll;
typedef long double ld;
typedef complex<ld> cd;

typedef pair<int, int> pi;
typedef pair<ll,ll> pl;
typedef pair<double,double> pd;

typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<pi> vpi;

template <class T> using Tree = tree<T, null_type, less<T>, rb_tree_tag,tree_order_statistics_node_update>;

#define dbg(x) cout<<#x<<" = "<<(x)<<endl;
#define ones(x) __builtin_popcount(x)
#define clz(x) __builtin_clz(x)
#define ctz(x) __builtin_ctz(x)
#define lbit(x) ((x)&(-(x)))
#define hbit(x) 1<<(31-__builtin_clz(x))

#define FOR(i, a, b) for (int i=a; i<(b); i++)
#define F0R(i, a) for (int i=0; i<(a); i++)
#define FORd(i,a,b) for (int i = (b)-1; i >= a; i--)
#define F0Rd(i,a) for (int i = (a)-1; i >= 0; i--)

#define sz(x) (int)(x).size()
#define mp make_pair
#define pb push_back
#define f first
#define s second
#define lb lower_bound
#define ub upper_bound
#define all(x) x.begin(), x.end()

const int MOD = 1000000007;
const ll INF = 1e18;
const int MX = 100001;
const int CF = 300005;

void solve() {
    int n; cin >> n;
    vi s(n);
    for (int &x:s)
        cin >> x;
    sort(all(s));
    vi basis, vec;
    auto add = [&](int u){
        int tmp = u;
        for (int& v: basis)
            u = min(u, u^v);
        if (u > 0){
            basis.pb(u);
            vec.pb(tmp);
        }
    };
    auto gray = [&](int dim){
        assert(sz(vec) == dim);
        vi res(1<<dim); res[0] = 0;
        for (int d = 0; d < dim; d++){
            int crt = 1 << d;//crt size
            for (int j = 0; j < crt; j++)
                res[j+crt] = res[j] ^ vec[d];
        }
        cout << dim << '\n';
        for (int& y: res)
            cout << y << ' ';
    };
    int j = 0;
    int dim = 0;
    for (int x = 0; x < 20; x++){
        for (;j < n && s[j] < (1<<x); j++)
            add(s[j]);
        if (sz(basis) == x)
            dim = x;
    }
    basis.clear(); vec.clear();
    for (j=0; j < n && s[j] < (1<<dim); j++)
        add(s[j]);
    gray(dim);
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    solve();
    cout << endl;
}


#include <bits/stdc++.h>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/assoc_container.hpp>

using namespace std;
using namespace __gnu_pbds;

typedef long long ll;
typedef long double ld;
typedef complex<ld> cd;

typedef pair<int, int> pi;
typedef pair<ll,ll> pl;
typedef pair<double,double> pd;

typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<pi> vpi;

template <class T> using Tree = tree<T, null_type, less<T>, rb_tree_tag,tree_order_statistics_node_update>;

#define dbg(x) cout<<#x<<" = "<<(x)<<endl;
#define ones(x) __builtin_popcount(x)
#define clz(x) __builtin_clz(x)
#define ctz(x) __builtin_ctz(x)
#define lbit(x) ((x)&(-(x)))
#define hbit(x) 1<<(31-__builtin_clz(x))

#define FOR(i, a, b) for (int i=a; i<(b); i++)
#define F0R(i, a) for (int i=0; i<(a); i++)
#define FORd(i,a,b) for (int i = (b)-1; i >= a; i--)
#define F0Rd(i,a) for (int i = (a)-1; i >= 0; i--)

#define sz(x) (int)(x).size()
#define mp make_pair
#define pb push_back
#define f first
#define s second
#define lb lower_bound
#define ub upper_bound
#define all(x) x.begin(), x.end()

const int MOD = 1000000007;
const ll INF = 1e18;
const int MX = 100001;
const int CF = 300005;

void solve() {
    int n; cin >> n;
    vi s(n);
    for (int &x:s)
        cin >> x;
    sort(all(s));
    vi basis, vec;
    auto add = [&](int u){
        int tmp = u;
        for (int& v: basis)
            u = min(u, u^v);
        if (u > 0){
            basis.pb(u);
            vec.pb(tmp);
        }
    };
    auto gray = [&](int dim){
        assert(sz(vec) == dim);
        vi res(1<<dim); res[0] = 0;
        for (int d = 0; d < dim; d++){
            int crt = 1 << d;//crt size
            //dbg(vec[d]);
            for (int j = 0; j < crt; j++)
                res[j+crt] = res[crt-j-1] ^ vec[d];
        }
        cout << dim << '\n';
        for (int& y: res)
            cout << y << ' ';
    };
    int j = 0;
    int dim = 0;
    for (int x = 0; x < 20; x++){
        for (;j < n && s[j] < (1<<x); j++)
            add(s[j]);
        if (sz(basis) == x)
            dim = x;
    }
    basis.clear(); vec.clear();
    for (j=0; j < n && s[j] < (1<<dim); j++)
        add(s[j]);
    gray(dim);
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    solve();
    cout << endl;
}


#include <bits/stdc++.h>
using namespace std;
#define ft              first
#define nd              second
#define ii              pair<int, vector<int> >
#define forn(i, a, b)   for(int i=a; i<=b; ++i)
#define repn(i, a, b)   for(int i=a; i <b; ++i)
const int N=2e5 +100;
int n;
int p[N], idx[N], flag[N];
vector<int> set_bit;
vector<int> s;
ii gauss[N];
void add(vector<int> &a, vector<int> b)
{
    for(auto &x: b) a.push_back(x);
}
int get_bit(int mask, int i) { return mask>>i&1; }
bool cmp(ii a, ii b) { return a.ft<b.ft; }
int main()
{
    cin>>n;
    s.resize(n);
    repn(i, 0, n)
    {
        cin>>s[i];
        p[i]=s[i];
        idx[i]=i;
        gauss[i].ft=s[i];
        gauss[i].nd.push_back(i);
    }
    int column=0;
    forn(row, 0, 17)
    {
        repn(i, row, n) if (get_bit(gauss[i].ft, column)) swap(gauss[i], gauss[row]);
        if (!get_bit(gauss[row].ft, column)) break;
        repn(i, 0, n) if (i!=row && get_bit(gauss[i].ft, column))
        {
            gauss[i].ft^=gauss[row].ft;
            add(gauss[i].nd, gauss[row].nd);
        }
        column ++;
    }
    sort(gauss, gauss+n, cmp);
    // repn(i, 0, n) cout<<gauss[i].ft<<' ';
    // cout<<'\n';
    int x=0;
    repn(i, 0, n)
    {
        if (gauss[i].ft==0) continue;
        if (gauss[i].ft==1<<x)
        {
            x++;
            for(auto &value: gauss[i].nd) flag[value]=true;
        } else break;
    }
    repn(i, 0, n) if (flag[i]) set_bit.push_back(p[i]);
    cout<<x<<'\n';
    forn(i, 0, (1<<x) -1)
    {
        int mask= i^(i>>1);
        int value=0;
        repn(bit, 0, set_bit.size()) if (get_bit(mask, bit)) value^=set_bit[bit];
        cout<<value<<' ';
    }
}


#include <bits/stdc++.h>
using namespace std;
#define ft              first
#define nd              second
#define ii              pair<int, vector<int> >
#define forn(i, a, b)   for(int i=a; i<=b; ++i)
#define repn(i, a, b)   for(int i=a; i <b; ++i)
const int N=2e5 +10;
int n;
int p[N], idx[N], flag[N];
bool mark[N];
vector<int> set_bit;
vector<int> s;
ii gauss[N];
int get_bit(int mask, int i) { return mask>>i&1; }
bool cmp(ii a, ii b) { return a.ft<b.ft; }
int main()
{
    cin>>n;
    s.resize(n);
    repn(i, 0, n)
    {
        cin>>s[i];
        idx[i]=i;
        gauss[i].ft=s[i];
        gauss[i].nd.push_back(i);
    }
    int column=0;
    forn(row, 0, 17)
    {
        repn(i, row, n) if (get_bit(gauss[i].ft, column))
        {
            swap(gauss[i], gauss[row]);
            swap(idx[row], idx[i]);
            break;
        }
        if (!get_bit(gauss[row].ft, column)) break;
        repn(i, 0, n) if (i!=row && get_bit(gauss[i].ft, column))
        {
            gauss[i].ft^=gauss[row].ft;
            gauss[i].nd.push_back(idx[row]);
        }
        column ++;
    }
    sort(gauss, gauss+n, cmp);
    // repn(i, 0, n) cout<<gauss[i].ft<<' ';
    // cout<<'\n';
    int x=0;
    repn(i, 0, n)
    {
        if (gauss[i].ft==0) continue;
        if (gauss[i].ft==1<<x)
        {
            x++;
            for(auto &value: gauss[i].nd) flag[value]=true;
        } else break;
    }
    repn(i, 0, n) if (flag[i]) set_bit.push_back(s[i]);
    sort(set_bit.begin(), set_bit.end());
    set_bit.resize(unique(set_bit.begin(), set_bit.end())-set_bit.begin());
    while (set_bit.size()>x) set_bit.pop_back();
    cout<<x<<'\n';
    forn(i, 0, (1<<x) -1)
    {
        int mask= i^(i>>1);
        int value=0;
        repn(bit, 0, set_bit.size()) if (get_bit(mask, bit)) value^=set_bit[bit];
        cout<<value<<' ';
    }
}


#include <bits/stdc++.h>
using namespace std;
#define ft              first
#define nd              second
#define ii              pair<int, vector<int> >
#define forn(i, a, b)   for(int i=a; i<=b; ++i)
#define repn(i, a, b)   for(int i=a; i <b; ++i)
const int N=2e5 +10;
int n;
int p[N], idx[N], flag[N];
bool mark[N];
vector<int> set_bit;
vector<int> s;
ii gauss[N];
int get_bit(int mask, int i) { return mask>>i&1; }
bool cmp(ii a, ii b) { return a.ft<b.ft; }
int main()
{
    cin>>n;
    s.resize(n);
    repn(i, 0, n)
    {
        cin>>s[i];
        idx[i]=i;
        gauss[i].ft=s[i];
        gauss[i].nd.push_back(i);
    }
    int column=0;
    forn(row, 0, 17)
    {
        repn(i, row, n) if (get_bit(gauss[i].ft, column))
        {
            swap(gauss[i], gauss[row]);
            swap(idx[row], idx[i]);
            break;
        }
        if (!get_bit(gauss[row].ft, column)) break;
        repn(i, 0, n) if (i!=row && get_bit(gauss[i].ft, column))
        {
            gauss[i].ft^=gauss[row].ft;
            gauss[i].nd.push_back(idx[row]);
        }
        column ++;
    }
    sort(gauss, gauss+n, cmp);
    // repn(i, 0, n) cout<<gauss[i].ft<<' ';
    // cout<<'\n';
    int x=0;
    repn(i, 0, n)
    {
        if (gauss[i].ft==0) continue;
        if (gauss[i].ft==1<<x)
        {
            x++;
            for(auto &value: gauss[i].nd) flag[value]=true;
        } else break;
    }
    repn(i, 0, n) if (flag[i]) set_bit.push_back(s[i]);
    cout<<x<<'\n';
    forn(i, 0, (1<<x) -1)
    {
        int mask= i^(i>>1);
        int value=0;
        repn(bit, 0, set_bit.size()) if (get_bit(mask, bit)) value^=set_bit[bit];
        cout<<value<<' ';
    }
}


#include <bits/stdc++.h>
using namespace std;
#define ft              first
#define nd              second
#define ii              pair<int, vector<int> >
#define forn(i, a, b)   for(int i=a; i<=b; ++i)
#define repn(i, a, b)   for(int i=a; i <b; ++i)
const int N=2e5 +10;
int n;
int p[N], idx[N], flag[N];
bool mark[N];
vector<int> set_bit;
vector<int> s;
ii gauss[N];
int get_bit(int mask, int i) { return mask>>i&1; }
bool cmp(ii a, ii b) { return a.ft<b.ft; }
int main()
{
    cin>>n;
    s.resize(n);
    repn(i, 0, n)
    {
        cin>>s[i];
        idx[i]=i;
        gauss[i].ft=s[i];
        gauss[i].nd.push_back(i);
    }
    int column=0;
    forn(row, 0, 17)
    {
        repn(i, row, n) if (get_bit(gauss[i].ft, column))
        {
            swap(gauss[i], gauss[row]);
            swap(idx[row], idx[i]);
            break;
        }
        if (!get_bit(gauss[row].ft, column)) break;
        repn(i, 0, n) if (i!=row && get_bit(gauss[i].ft, column))
        {
            gauss[i].ft^=gauss[row].ft;
            gauss[i].nd.push_back(idx[row]);
        }
        column ++;
    }
    sort(gauss, gauss+n, cmp);
    // repn(i, 0, n) cout<<gauss[i].ft<<' ';
    // cout<<'\n';
    int x=0;
    repn(i, 0, n)
    {
        if (gauss[i].ft==0) continue;
        if (gauss[i].ft==1<<x)
        {
            x++;
            for(auto &value: gauss[i].nd) flag[value]=true;
        } else break;
    }
    repn(i, 0, n) if (flag[i]) set_bit.push_back(s[i]);
    assert(set_bit.size() == x);
    cout<<x<<'\n';
    forn(i, 0, (1<<x) -1)
    {
        int mask= i^(i>>1);
        int value=0;
        repn(bit, 0, set_bit.size()) if (get_bit(mask, bit)) value^=set_bit[bit];
        cout<<value<<' ';
    }
}


#include <bits/stdc++.h>
using namespace std;
#define ft              first
#define nd              second
#define ii              pair<int, vector<int> >
#define forn(i, a, b)   for(int i=a; i<=b; ++i)
#define ford(i, a, b)   for(int i=a; i>=b; --i)
#define repn(i, a, b)   for(int i=a; i <b; ++i)
const int N=2e5 +10;
int n;
int p[N], idx[N], flag[N];
bool mark[N];
vector<int> set_bit;
vector<int> s;
ii gauss[N];
int get_bit(int mask, int i) { return mask>>i&1; }
bool cmp(ii a, ii b) { return a.ft<b.ft; }
void check(int x)
{
    int m=0;
    repn(i, 0, n) if (s[i]<(1<<x))
    {
        gauss[m]={s[i], {i}};
        idx[m]=i;
        m++;
    }
    if (m==0) return;
    int column=0;
    repn(row, 0, x)
    {
        repn(i, row, m) if (get_bit(gauss[i].ft, column))
        {
            swap(gauss[i], gauss[row]);
            swap(idx[row], idx[i]);
            break;
        }
        if (!get_bit(gauss[row].ft, column)) break;
        repn(i, 0, m) if (i!=row && get_bit(gauss[i].ft, column))
        {
            gauss[i].ft^=gauss[row].ft;
            gauss[i].nd.push_back(idx[row]);
        }
        column ++;
    }
    //if (x==2) cout<<column<<'\n';
    if (column!=x) return;
    repn(i, 0, x)  for(auto &value: gauss[i].nd) flag[value]=true;
    repn(i, 0, n) if (flag[i]) set_bit.push_back(s[i]);
    sort(set_bit.begin(), set_bit.end());
    set_bit.resize(unique(set_bit.begin(), set_bit.end())-set_bit.begin());
    cout<<x<<'\n';
    forn(i, 0, (1<<x) -1)
    {
        int mask= i^(i>>1);
        int value=0;
        repn(bit, 0, set_bit.size()) if (get_bit(mask, bit)) value^=set_bit[bit];
        cout<<value<<' ';
    }
    exit(0);

}
int main()
{
    ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    cin>>n;
    s.resize(n);
    repn(i, 0, n)
    {
        cin>>s[i];
    }
    ford(x, 18, 0) check(x);
    cout<<0<<'\n'<<0;
}


#include <bits/stdc++.h>
using namespace std;
#define forn(i, a, b)   for(int i=a; i<=b; ++i)
#define repn(i, a, b)   for(int i=a; i <b; ++i)
int n;
vector<int> s;
int get_bit(int mask, int i) { return mask>>i&1; }
int main()
{
    cin>>n;
    s.resize(n);
    repn(i, 0, n) cin>>s[i];
    int column=0;
    forn(row, 0, 17)
    {
        if (column>17) break;
        repn(i, row, n) if (get_bit(s[i], column)) swap(s[i], s[row]);
        if (!get_bit(s[row], column))
        {
            column ++;
            row --;
            continue;
        }
        repn(i, 0, n) if (i!=row && get_bit(s[i], column)) s[i]^=s[row];
        column ++;
    }
    sort(s.begin(), s.end());
    if (!s.front()) s.erase(s.begin());
    int x=0;
    repn(i, 0, n) if (s[i]!=1<<i) break; else x=i+1;
    cout<<x<<'\n';
    forn(i, 0, (1<<x) -1) cout<< ( i^(i>>1) ) <<' ';
}


#include <bits/stdc++.h>
using namespace std;
#define ft              first
#define nd              second
#define ii              pair<int, vector<int> >
#define forn(i, a, b)   for(int i=a; i<=b; ++i)
#define repn(i, a, b)   for(int i=a; i <b; ++i)
const int N=2e5 +10;
int n;
int p[N], idx[N], flag[N];
bool mark[N];
vector<int> set_bit;
vector<int> s;
ii gauss[N];
void add(vector<int> &a, vector<int> b)
{
    vector<int> v;
    for(auto &x: a) { mark[x]=true; v.push_back(x); }
    for(auto &x: b) if (!mark[x]) a.push_back(x);
    for(auto &x: v) mark[x]=false;
    v.clear();
}
int get_bit(int mask, int i) { return mask>>i&1; }
bool cmp(ii a, ii b) { return a.ft<b.ft; }
int main()
{
    cin>>n;
    s.resize(n);
    repn(i, 0, n)
    {
        cin>>s[i];
        idx[i]=i;
        gauss[i].ft=s[i];
        gauss[i].nd.push_back(i);
    }
    int column=0;
    forn(row, 0, 17)
    {
        repn(i, row, n) if (get_bit(gauss[i].ft, column)) swap(gauss[i], gauss[row]);
        if (!get_bit(gauss[row].ft, column)) break;
        repn(i, 0, n) if (i!=row && get_bit(gauss[i].ft, column))
        {
            gauss[i].ft^=gauss[row].ft;
            add(gauss[i].nd, gauss[row].nd);
        }
        column ++;
    }
    sort(gauss, gauss+n, cmp);
    // repn(i, 0, n) cout<<gauss[i].ft<<' ';
    // cout<<'\n';
    int x=0;
    repn(i, 0, n)
    {
        if (gauss[i].ft==0) continue;
        if (gauss[i].ft==1<<x)
        {
            x++;
            for(auto &value: gauss[i].nd) flag[value]=true;
        } else break;
    }
    repn(i, 0, n) if (flag[i]) set_bit.push_back(s[i]);
    cout<<x<<'\n';
    forn(i, 0, (1<<x) -1)
    {
        int mask= i^(i>>1);
        int value=0;
        repn(bit, 0, set_bit.size()) if (get_bit(mask, bit)) value^=set_bit[bit];
        cout<<value<<' ';
    }
}


#include <bits/stdc++.h>
using namespace std;
#define ft              first
#define nd              second
#define ii              pair<int, vector<int> >
#define forn(i, a, b)   for(int i=a; i<=b; ++i)
#define ford(i, a, b)   for(int i=a; i>=b; --i)
#define repn(i, a, b)   for(int i=a; i <b; ++i)
const int N=2e5 +10;
int n;
int p[N], idx[N], flag[N];
bool mark[N];
vector<int> set_bit;
vector<int> s;
ii gauss[N];
int get_bit(int mask, int i) { return mask>>i&1; }
bool cmp(ii a, ii b) { return a.ft<b.ft; }
void check(int x)
{
    int m=0;
    repn(i, 0, n) if (s[i]<(1<<x))
    {
        gauss[m]={s[i], {i}};
        idx[m]=i;
        m++;
    }
    int column=0;
    repn(row, 0, x)
    {
        repn(i, row, m) if (get_bit(gauss[i].ft, column))
        {
            swap(gauss[i], gauss[row]);
            swap(idx[row], idx[i]);
            break;
        }
        if (!get_bit(gauss[row].ft, column)) break;
        repn(i, 0, m) if (i!=row && get_bit(gauss[i].ft, column))
        {
            gauss[i].ft^=gauss[row].ft;
            gauss[i].nd.push_back(idx[row]);
        }
        column ++;
    }
    //if (x==2) cout<<column<<'\n';
    if (column!=x) return;
    repn(i, 0, x)  for(auto &value: gauss[i].nd) flag[value]=true;
    repn(i, 0, n) if (flag[i]) set_bit.push_back(s[i]);
    sort(set_bit.begin(), set_bit.end());
    set_bit.resize(unique(set_bit.begin(), set_bit.end())-set_bit.begin());
    cout<<x<<'\n';
    forn(i, 0, (1<<x) -1)
    {
        int mask= i^(i>>1);
        int value=0;
        repn(bit, 0, set_bit.size()) if (get_bit(mask, bit)) value^=set_bit[bit];
        cout<<value<<' ';
    }
    exit(0);

}
int main()
{
    cin>>n;
    s.resize(n);
    repn(i, 0, n)
    {
        cin>>s[i];
    }
    ford(x, 17, 0) check(x);
    int column=0;
    forn(row, 0, 17)
    {
        repn(i, row, n) if (get_bit(gauss[i].ft, column))
        {
            swap(gauss[i], gauss[row]);
            swap(idx[row], idx[i]);
            break;
        }
        if (!get_bit(gauss[row].ft, column)) break;
        repn(i, 0, n) if (i!=row && get_bit(gauss[i].ft, column))
        {
            gauss[i].ft^=gauss[row].ft;
            gauss[i].nd.push_back(idx[row]);
        }
        column ++;
    }
    sort(gauss, gauss+n, cmp);
    // repn(i, 0, n) cout<<gauss[i].ft<<' ';
    // cout<<'\n';
    int x=0;

}


#include <bits/stdc++.h>
using namespace std;
#define ft              first
#define nd              second
#define ii              pair<int, vector<int> >
#define forn(i, a, b)   for(int i=a; i<=b; ++i)
#define ford(i, a, b)   for(int i=a; i>=b; --i)
#define repn(i, a, b)   for(int i=a; i <b; ++i)
const int N=2e5 +10;
int n;
int p[N], idx[N], flag[N];
bool mark[N];
vector<int> set_bit;
vector<int> s;
ii gauss[N];
int get_bit(int mask, int i) { return mask>>i&1; }
bool cmp(ii a, ii b) { return a.ft<b.ft; }
void check(int x)
{
    int m=0;
    repn(i, 0, n) if (s[i]<(1<<x))
    {
        gauss[m]={s[i], {i}};
        idx[m]=i;
        m++;
    }
    int column=0;
    repn(row, 0, x)
    {
        repn(i, row, m) if (get_bit(gauss[i].ft, column))
        {
            swap(gauss[i], gauss[row]);
            swap(idx[row], idx[i]);
            break;
        }
        if (!get_bit(gauss[row].ft, column)) break;
        repn(i, 0, m) if (i!=row && get_bit(gauss[i].ft, column))
        {
            gauss[i].ft^=gauss[row].ft;
            gauss[i].nd.push_back(idx[row]);
        }
        column ++;
    }
    if (x==2) cout<<column<<'\n';
    if (column!=x) return;
    repn(i, 0, x)  for(auto &value: gauss[i].nd) flag[value]=true;
    repn(i, 0, n) if (flag[i]) set_bit.push_back(s[i]);
    sort(set_bit.begin(), set_bit.end());
    set_bit.resize(unique(set_bit.begin(), set_bit.end())-set_bit.begin());
    cout<<x<<'\n';
    forn(i, 0, (1<<x) -1)
    {
        int mask= i^(i>>1);
        int value=0;
        repn(bit, 0, set_bit.size()) if (get_bit(mask, bit)) value^=set_bit[bit];
        cout<<value<<' ';
    }
    exit(0);

}
int main()
{
    cin>>n;
    s.resize(n);
    repn(i, 0, n)
    {
        cin>>s[i];
    }
    ford(x, 17, 0) check(x);
    int column=0;
    forn(row, 0, 17)
    {
        repn(i, row, n) if (get_bit(gauss[i].ft, column))
        {
            swap(gauss[i], gauss[row]);
            swap(idx[row], idx[i]);
            break;
        }
        if (!get_bit(gauss[row].ft, column)) break;
        repn(i, 0, n) if (i!=row && get_bit(gauss[i].ft, column))
        {
            gauss[i].ft^=gauss[row].ft;
            gauss[i].nd.push_back(idx[row]);
        }
        column ++;
    }
    sort(gauss, gauss+n, cmp);
    // repn(i, 0, n) cout<<gauss[i].ft<<' ';
    // cout<<'\n';
    int x=0;

}


#include <bits/stdc++.h>
using namespace std;
#define ft              first
#define nd              second
#define ii              pair<int, vector<int> >
#define forn(i, a, b)   for(int i=a; i<=b; ++i)
#define ford(i, a, b)   for(int i=a; i>=b; --i)
#define repn(i, a, b)   for(int i=a; i <b; ++i)
const int N=2e5 +10;
int n;
int p[N], idx[N], flag[N];
bool mark[N];
vector<int> set_bit;
vector<int> s;
ii gauss[N];
int get_bit(int mask, int i) { return mask>>i&1; }
bool cmp(ii a, ii b) { return a.ft<b.ft; }
void check(int x)
{
    int m=0;
    repn(i, 0, n) if (s[i]<(1<<x))
    {
        gauss[m]={s[i], {i}};
        idx[m]=i;
        m++;
    }
    int column=0;
    repn(row, 0, x)
    {
        repn(i, row, m) if (get_bit(gauss[i].ft, column))
        {
            swap(gauss[i], gauss[row]);
            swap(idx[row], idx[i]);
            break;
        }
        if (!get_bit(gauss[row].ft, column)) break;
        repn(i, 0, m) if (i!=row && get_bit(gauss[i].ft, column))
        {
            gauss[i].ft^=gauss[row].ft;
            gauss[i].nd.push_back(idx[row]);
        }
        column ++;
    }
    //if (x==2) cout<<column<<'\n';
    if (column!=x) return;
    repn(i, 0, x)  for(auto &value: gauss[i].nd) flag[value]=true;
    repn(i, 0, n) if (flag[i]) set_bit.push_back(s[i]);
    sort(set_bit.begin(), set_bit.end());
    set_bit.resize(unique(set_bit.begin(), set_bit.end())-set_bit.begin());
    cout<<x<<'\n';
    forn(i, 0, (1<<x) -1)
    {
        int mask= i^(i>>1);
        int value=0;
        repn(bit, 0, set_bit.size()) if (get_bit(mask, bit)) value^=set_bit[bit];
        cout<<value<<' ';
    }
    exit(0);

}
int main()
{
    ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    cin>>n;
    s.resize(n);
    repn(i, 0, n)
    {
        cin>>s[i];
    }
    ford(x, 18, 0) check(x);
}


#include <bits/stdc++.h>
using namespace std;
#define forn(i, a, b)   for(int i=a; i<=b; ++i)
#define repn(i, a, b)   for(int i=a; i <b; ++i)
const int N=2e5 +100;
int n;
int p[N], idx[N], flag[N];
vector<int> set_bit;
vector<int> s;
int get_bit(int mask, int i) { return mask>>i&1; }
int main()
{
    cin>>n;
    s.resize(n);
    repn(i, 0, n) { cin>>s[i]; p[i]=s[i]; idx[i]=i; }
    int column=0;
    forn(row, 0, 17)
    {
        repn(i, row, n) if (get_bit(s[i], column))
        {
            swap(s[i], s[row]);
            swap(idx[i], idx[row]);
        }
        if (!get_bit(s[row], column)) break;
        flag[idx[row]]=true;
        repn(i, 0, n) if (i!=row && get_bit(s[i], column)) s[i]^=s[row];
        column ++;
    }
    repn(i, 0, n) if (flag[i]) set_bit.push_back(p[i]);
    cout<<set_bit.size()<<'\n';
    forn(i, 0, (1<<set_bit.size()) -1)
    {
        int mask= i^(i>>1);
        int value=0;
        repn(bit, 0, set_bit.size()) if (get_bit(mask, bit)) value^=set_bit[bit];
        cout<<value<<' ';
    }
}


#include <bits/stdc++.h>

using namespace std;

const int maxn = 200010;
int n,a[maxn],b[18],limit,ans[maxn],top,shu[maxn],cnt,vis[maxn],anss;

void build()
{
    for (int i = 1; i <= n; i++)
    {
        int t = a[i];
        for (int j = 17; j >= 0; j--)
            if (t >> j & 1)
            {
                if (!b[j])
                {
                    b[j] = t;
                    break;
                }
                t ^= b[j];
            }
    }
}

void Insert(int x)
{
    for (int i = 1; i <= n; i++)
    {
        int t = x;
        for (int j = 17; j >= 0; j--)
            if (t >> j & 1)
            {
                if (!b[j])
                {
                    b[j] = t;
                    break;
                }
                else
                    t ^= b[j];
            }
        if (t > 0)
            shu[++cnt] = x;
    }
}

void dfs(int u)
{
    ans[++top] = u;
    vis[u] = 1;
    for (int i = 1; i <= cnt; i++)
        if (!vis[u ^ shu[i]])
            dfs(u ^ shu[i]);
}

int main()
{
    scanf("%d",&n);
    for (int i = 1; i <= n; i++)
        scanf("%d",&a[i]);
    build();
    limit = 0;
    for (int i = 0; i <= 17; i++)
    {
        if (b[i])
        {
            limit = (1 << (i + 1)) - 1;
            anss = i + 1;
        }
        else
            break;
    }
    memset(b,0,sizeof(b));
    for (int i = 1; i <= n; i++)
        if (a[i] <= limit)
            Insert(a[i]);
    vis[0] = 1;
    dfs(0);
    printf("%d\n",anss);
    for (int i = 1; i <= limit + 1; i++)
        printf("%d ",ans[i]);

    return 0;
}


#include <bits/stdc++.h>

using namespace std;

const int maxn = 500010;
int n,a[maxn],b[18],limit,ans[maxn],top,shu[maxn],cnt,vis[maxn],anss;

void build()
{
    for (int i = 1; i <= n; i++)
    {
        int t = a[i];
        for (int j = 17; j >= 0; j--)
            if (t >> j & 1)
            {
                if (!b[j])
                {
                    b[j] = t;
                    break;
                }
                t ^= b[j];
            }
    }
}

void Insert(int x)
{
    int t = x;
    for (int j = 17; j >= 0; j--)
        if (t >> j & 1)
        {
            if (!b[j])
            {
                b[j] = t;
                break;
            }
            else
                t ^= b[j];
        }
    if (t > 0)
        shu[++cnt] = x;
}

void dfs(int u)
{
    ans[++top] = u;
    vis[u] = 1;
    for (int i = 1; i <= cnt; i++)
        if (!vis[u ^ shu[i]])
            dfs(u ^ shu[i]);
}

int main()
{
    scanf("%d",&n);
    for (int i = 1; i <= n; i++)
        scanf("%d",&a[i]);
    build();
    limit = 0;
    for (int i = 0; i <= 17; i++)
    {
        if (b[i])
        {
            limit = (1 << (i + 1)) - 1;
            anss = i + 1;
        }
        else
            break;
    }
    memset(b,0,sizeof(b));
    for (int i = 1; i <= n; i++)
        if (a[i] <= limit)
            Insert(a[i]);
    vis[0] = 1;
    dfs(0);
    printf("%d\n",anss);
    for (int i = 1; i <= limit + 1; i++)
        printf("%d ",ans[i]);

    return 0;
}


#include <bits/stdc++.h>

using namespace std;

const int maxn = 500010;
int n,a[maxn],b[18],limit,ans[maxn],top,shu[maxn],cnt,vis[maxn],anss;

void build()
{
    for (int i = 1; i <= n; i++)
    {
        int t = a[i];
        for (int j = 17; j >= 0; j--)
            if (t >> j & 1)
            {
                if (!b[j])
                {
                    b[j] = t;
                    break;
                }
                t ^= b[j];
            }
    }
}

void Insert(int x)
{
    for (int i = 1; i <= n; i++)
    {
        int t = x;
        for (int j = 17; j >= 0; j--)
            if (t >> j & 1)
            {
                if (!b[j])
                {
                    b[j] = t;
                    break;
                }
                else
                    t ^= b[j];
            }
        if (t > 0)
            shu[++cnt] = x;
    }
}

void dfs(int u)
{
    ans[++top] = u;
    vis[u] = 1;
    for (int i = 1; i <= cnt; i++)
        if (!vis[u ^ shu[i]])
            dfs(u ^ shu[i]);
}

int main()
{
    scanf("%d",&n);
    for (int i = 1; i <= n; i++)
        scanf("%d",&a[i]);
    build();
    limit = 0;
    for (int i = 0; i <= 17; i++)
    {
        if (b[i])
        {
            limit = (1 << (i + 1)) - 1;
            anss = i + 1;
        }
        else
            break;
    }
    memset(b,0,sizeof(b));
    for (int i = 1; i <= n; i++)
        if (a[i] <= limit)
            Insert(a[i]);
    vis[0] = 1;
    dfs(0);
    printf("%d\n",anss);
    for (int i = 1; i <= limit + 1; i++)
        printf("%d ",ans[i]);

    return 0;
}


#include <bits/stdc++.h>
using namespace std;

template<typename T>
void out(T x) { cout << x << endl; exit(0); }
#define watch(x) cout << (#x) << " is " << (x) << endl

// source: https://codeforces.com/contest/1163/problem/E
// r558 E Magical Permutation

using ll = long long;

const int maxn = 1e6 + 5;

int n;
int a[maxn];

vector<int> basis,vec;

void add(int x) {
    int save = x;
    for (int b: basis) {
	x = min(x, x^b);
    }
    if (x > 0) {
	basis.push_back(x);
	vec.push_back(save);
	sort(basis.rbegin(),basis.rend());
    }
}

vector<int> res;
bool viz[maxn];

int dim;

void dfs(int x) {
    if (viz[x]) {
	return;
    }
    viz[x] = true;
    res.push_back(x);
    if ((int)res.size() == (1<<dim)) {
	return;
    }

    for (int v: vec) {
	dfs(v^x);
    }
}

vector<int> gray_code(int x) {
    dim = x;
    dfs(0);
    return res;
}

int main() {
    ios_base::sync_with_stdio(false); cin.tie(0);  cout.tie(0);

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

    int x=0;
    int p=0;
    for (int i=1; i<20; i++) {
	for (; p<n && a[p]<1<<i; p++) {
	    add(a[p]);
	}
	if ((int)basis.size()==i) {
	    x=i;
	}
    }

    basis.clear();
    vec.clear();

    for (int i=0; i<n; i++) {
	if (a[i] < (1<<x)) {
	    add(a[i]);
	}
    }

    vector<int> res = gray_code(x);

    cout<<x<<"\n";
    for (int i: res) cout<<i<<" ";
    cout<<endl;
    return 0;
}


#include <bits/stdc++.h>
using namespace std;

template<typename T>
void out(T x) { cout << x << endl; exit(0); }
#define watch(x) cout << (#x) << " is " << (x) << endl

using ll = long long;

const int maxn = 1e6 + 5;

int n;
int a[maxn];

vector<int> basis,vec;

void add(int x) {
    int save = x;
    for (int b: basis) {
	x = min(x, x^b);
    }
    if (x > 0) {
	basis.push_back(x);
	vec.push_back(save);
	sort(basis.rbegin(),basis.rend());
    }
}

vector<int> gray_code(int x) {
    vector<int> res = {0};
    for (int i=0; i<x; i++) {
	for (int j=(1<<i)-1; j>=0; j--) {
	    res.push_back(res[j]^vec[i]);
	}
    }

    return res;
}

int main() {
    ios_base::sync_with_stdio(false); cin.tie(0);  cout.tie(0);

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

    int x=0;
    int p=0;
    for (int i=1; i<20; i++) {
	for (; p<n && a[p]<1<<i; p++) {
	    add(a[p]);
	}
	if ((int)basis.size()==i) {
	    x=i;
	}
    }

    basis.clear();
    vec.clear();

    for (int i=0; i<n; i++) {
	if (a[i] < (1<<x)) {
	    add(a[i]);
	}
    }

    vector<int> res = gray_code(x);

    cout<<x<<"\n";
    for (int i: res) cout<<i<<" ";
    cout<<endl;
    return 0;
}


#include <bits/stdc++.h>
using namespace std;

template<typename T>
void out(T x) { cout << x << endl; exit(0); }
#define watch(x) cout << (#x) << " is " << (x) << endl

using ll = long long;

const int maxn = 1e6 + 5;
const int inf = 1e9;

int getbit(int x) {
    return 31 - __builtin_clz(x);
}

int n;

vector<int> s;

int basis[maxn];
int match[maxn];
vector<int> g[maxn];
int viz[maxn];
int iter = 1;

bool dfs(int b) {
    if (viz[b]==iter) {
	return false;
    }
    viz[b]=iter;

    for (int v: g[b]) {
	if (match[v]==-1) {
	    basis[b]=v;
	    match[v]=b;
	    return true;
	}
    }

    for (int v: g[b]) {
	if (dfs(match[v])) {
	    match[v]=b;
	    basis[b]=v;
	    return true;
	}
    }

    return false;
}

int main() {
    ios_base::sync_with_stdio(false); cin.tie(0);  cout.tie(0);

    assert(getbit(1)==0);
    assert(getbit(4)==2);

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

    for (int v: s) {
	for (int i=0; i<32; i++) {
	    if (v>>i&1) {
		g[i].push_back(v);
	    }
	}
    }

    memset(match,-1,sizeof(match));

    for (int i=0; i<32; i++) {
	if (dfs(i)) {
	    ++iter;
	} else {
	    break;
	}
    }

    ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    int x = 0;
    while (basis[x]) {
	++x;
    }

    cout<<x<<"\n";

    cout<<0<<" ";
    int last = 0;

    for (int i=1; i<(1<<x); i++) {
	int f = getbit((i-1)^i);
	last = last^basis[f];
	cout<<last<<" ";
    }
    cout<<endl;
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define _ ios_base::sync_with_stdio(0);cin.tie(0);
#define endl '\n'

mt19937 rng((int) chrono::steady_clock::now().time_since_epoch().count());

typedef long long ll;
typedef pair<int, int> ii;

const int INF = 0x3f3f3f3f;
const ll LINF = 0x3f3f3f3f3f3f3f3f;

vector<int> gray_code(int n) {
	vector<int> ret(1<<n);
	for (int i = 0; i < (1<<n); i++) ret[i] = i^(i>>1);
	return ret;
}

int main(){ _
	int n;
	cin >> n;
	vector<int> v(n);
	for (int &i : v)
		cin >> i;

	vector<int> b, t;
	auto insert = [&](int m){
		int M = m;
		for (int j = 0; j < 20; j++){
			if ((m&(1 << j)) == 0) continue;
			if (b[j] != -1){
				m ^= b[j];
			}
			else{
				b[j] = m;
				t[j] = M;
				//cout << bitset<5>(M) << '\t' << M << endl;
				return j;
			}
		}
		return -1;
	};

	for (int x = 19; x >= 0; x--){
		b = t = vector(20, -1);
		for (int i : v)
			if (i < (1 << x))
				insert(i);

		bool ok = true;
		for (int i = x-1; i >= 0; i--)
			ok &= (b[i] != -1);
		if (!ok) {
			//cout << "failed" << x << endl;
			continue;
		}

		cout << x << endl;
		for (int i : gray_code(x)){
			int m = 0;
			for (int j = 0; j < x; j++)
				if (i&(1 << j))
					m ^= t[j];
			cout << m << " ";
		}
		cout << endl;
		exit(0);

	}

	cout << 0 << endl;
	exit(0);
}


#include <bits/stdc++.h>

using namespace std;

#define _ ios_base::sync_with_stdio(0);cin.tie(0);
#define endl '\n'

mt19937 rng((int) chrono::steady_clock::now().time_since_epoch().count());

typedef long long ll;
typedef pair<int, int> ii;

const int INF = 0x3f3f3f3f;
const ll LINF = 0x3f3f3f3f3f3f3f3f;

vector<int> gray_code(int n) {
	vector<int> ret(1<<n);
	for (int i = 0; i < (1<<n); i++) ret[i] = i^(i>>1);
	return ret;
}

int main(){ _
	int n;
	cin >> n;
	vector<int> v(n);
	for (int &i : v)
		cin >> i;

	vector<int> b, t;
	auto insert = [&](int m){
		int M = m;
		for (int j = 0; j < 20; j++){
			if ((m&(1 << j)) == 0) continue;
			if (b[j] != -1){
				m ^= b[j];
			}
			else{
				b[j] = m;
				t[j] = M;
				//cout << bitset<5>(M) << '\t' << M << endl;
				return j;
			}
		}
		return -1;
	};

	for (int x = 19; x >= 0; x--){
		b = t = vector(20, -1);
		for (int i : v)
			if (i < (1 << x))
				insert(i);

		bool ok = true;
		for (int i = x-1; i >= 0; i--)
			ok &= (b[i] != -1);
		if (!ok) {
			//cout << "failed" << x << endl;
			continue;
		}

		cout << x << endl;
		for (int i : gray_code(x)){
			int m = 0;
			for (int j = 0; j < x; j++)
				if (i&(1 << j))
					m ^= b[j];
			cout << m << " ";
		}
		cout << endl;
		exit(0);

	}

	cout << 0 << endl;
	exit(0);
}


#include <bits/stdc++.h>

using namespace std;

#define _ ios_base::sync_with_stdio(0);cin.tie(0);
#define endl '\n'

mt19937 rng((int) chrono::steady_clock::now().time_since_epoch().count());

typedef long long ll;
typedef pair<int, int> ii;

const int INF = 0x3f3f3f3f;
const ll LINF = 0x3f3f3f3f3f3f3f3f;

vector<int> gray_code(int n, int init) {
	vector<int> ret(1<<n);
	for (int i = 0; i < (1<<n); i++) ret[i] = i^(i>>1);
	for (int i = 0; i < (1<<n); i++) ret[i] ^= init;
	return ret;
}

int main(){ _
	int n;
	cin >> n;
	vector<int> v(n);
	for (int &i : v)
		cin >> i;

	vector<int> b, t;
	auto insert = [&](int m){
		int M = m;
		for (int j = 0; j < 20; j++){
			if ((m&(1 << j)) == 0) continue;
			if (b[j] != -1){
				m ^= b[j];
			}
			else{
				b[j] = m;
				t[j] = M;
				//cout << bitset<5>(M) << '\t' << M << endl;
				return j;
			}
		}
		return -1;
	};

	for (int x = 19; x >= 0; x--){
		b = t = vector(20, -1);
		for (int i : v)
			if (i < (1 << x))
				insert(i);

		bool ok = true;
		for (int i = x-1; i >= 0; i--)
			ok &= (b[i] != -1);
		if (!ok) {
			//cout << "failed" << x << endl;
			continue;
		}

		cout << x << endl;
		for (int i : gray_code(x, (1 << x) - 1)){
			int m = 0;
			for (int j = 0; j < x; j++)
				if (i&(1 << j))
					m ^= b[j];
			cout << m << " ";
		}
		cout << endl;
		exit(0);

	}

	cout << 0 << endl;
	exit(0);
}


#include <bits/stdc++.h>

using namespace std;

const int maxn=3e5+100;

int a[maxn];
int tmp[20];
int ans[maxn];

bool check(int num){
	int t=1<<num;
	for(int i=0;i<num;++i){
		if(tmp[i]>=t) return false;
	}
	return 1;
}

int main(){

	//cout<<(1<<18)-1<<endl;
	ios::sync_with_stdio(false);
	for(int i=0;i<20;++i){
		tmp[i]=maxn;
	}
	int n;
	cin>>n;
	for(int i=0;i<n;++i){
		cin>>a[i];
	}
	sort(a,a+n);
	for(int i=0;i<n;++i){
		for(int j=0;j<18;++j){
			if(a[i]&(1<<j)){
				if(tmp[j]==maxn){
					tmp[j]=a[i];
				}
				break;
			}
		}
	}
	for(int i=18;i;--i){
		if(check(i)){
			cout<<i<<endl;
			// for(int j=0;j<i;++j){
			// 	cout<<tmp[j]<<" ";
			// }
			// cout<<endl;
			int cnt=0;
			ans[cnt]=0;
			for(int j=0;j<i;++j){
				for(int k=cnt;k>=0;--k){
					ans[++cnt]=ans[k]^tmp[j];
				}
			}
			for(int j=0;j<=cnt;++j){
				cout<<ans[j]<<" ";
			}
			cout<<endl;
			return 0;
		}
	}
	cout<<0<<endl<<0<<endl;
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

const int maxn=2e5+100;

int a[maxn];
int tmp[20];
int ans[maxn];

bool check(int num){
	int t=1<<num;
	for(int i=0;i<num;++i){
		if(tmp[i]>=t) return false;
	}
	return 1;
}

int main(){

	//cout<<(1<<17)<<endl;
	ios::sync_with_stdio(false);
	for(int i=0;i<20;++i){
		tmp[i]=maxn;
	}
	int n;
	cin>>n;
	for(int i=0;i<n;++i){
		cin>>a[i];
	}
	sort(a,a+n);
	for(int i=0;i<n;++i){
		for(int j=0;j<18;++j){
			if(a[i]&(1<<j)){
				if(tmp[j]==maxn){
					tmp[j]=a[i];
				}
				break;
			}
		}
	}
	for(int i=17;i;--i){
		if(check(i)){
			cout<<i<<endl;
			// for(int j=0;j<i;++j){
			// 	cout<<tmp[j]<<" ";
			// }
			// cout<<endl;
			int cnt=0;
			ans[cnt]=0;
			for(int j=0;j<i;++j){
				for(int k=cnt;k>=0;--k){
					ans[++cnt]=ans[k]^tmp[j];
				}
			}
			for(int j=0;j<=cnt;++j){
				cout<<ans[j]<<" ";
			}
			cout<<endl;
			return 0;
		}
	}
	cout<<0<<endl<<0<<endl;
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

const int maxn=3e5+100;
const int Max_Base=18;

int a[maxn];
int tmp[20];
int ans[maxn];
int b[20]={0};

bool check(int num){
	int t=1<<num;
	for(int i=0;i<num;++i){
		if(tmp[i]>=t) return false;
	}
	return 1;
}

void cal(int t){
	int tt=t;
	for(int i=Max_Base-1;i>=0;--i){
		if(!(tt&(1<<i))) continue;
		if(!b[i]){
			b[i]=tt;
			tmp[i]=t;
			for(int k=i+1;k<Max_Base;++k) if(b[k]&(1<<i)) b[k]^=b[i];
			for(int k=i-1;k>=0;--k) if(b[i]&(1<<k)) b[i]^=b[k];
			break;
		}else{
			tt^=b[i];
		}
	}

}

int main(){

	ios::sync_with_stdio(false);
	for(int i=0;i<Max_Base;++i)
		tmp[i]=maxn;
	int n;
	cin>>n;
	for(int i=0;i<n;++i){
		cin>>a[i];
	}

	sort(a,a+n);

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

	// for(int j=0;j<=17;++j){
	// 	cout<<tmp[j]<<" ";
	// }
	// cout<<endl;

	for(int i=18;i;--i){
		if(check(i)){
			cout<<i<<endl;
			int cnt=0;
			ans[cnt]=0;
			for(int j=0;j<i;++j){
				for(int k=cnt;k>=0;--k){
					ans[++cnt]=ans[k]^tmp[j];
				}
			}
			for(int j=0;j<=cnt;++j){
				cout<<ans[j]<<" ";
			}
			cout<<endl;
			return 0;
		}
	}
	cout<<0<<endl<<0<<endl;
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

const int maxn=2e5+100;

int a[maxn];
int tmp[20];
int ans[maxn];

bool check(int num){
	int t=1<<num;
	for(int i=0;i<num;++i){
		if(tmp[i]>=t) return false;
	}
	return 1;
}

int main(){

	//cout<<(1<<18)-1<<endl;
	ios::sync_with_stdio(false);
	for(int i=0;i<20;++i){
		tmp[i]=maxn;
	}
	int n;
	cin>>n;
	for(int i=0;i<n;++i){
		cin>>a[i];
	}
	sort(a,a+n);
	for(int i=0;i<n;++i){
		for(int j=0;j<18;++j){
			if(a[i]&(1<<j)){
				if(tmp[j]==maxn){
					tmp[j]=a[i];
				}
				break;
			}
		}
	}
	for(int i=18;i;--i){
		if(check(i)){
			cout<<i<<endl;
			// for(int j=0;j<i;++j){
			// 	cout<<tmp[j]<<" ";
			// }
			// cout<<endl;
			int cnt=0;
			ans[cnt]=0;
			for(int j=0;j<i;++j){
				for(int k=cnt;k>=0;--k){
					ans[++cnt]=ans[k]^tmp[j];
				}
			}
			for(int j=0;j<=cnt;++j){
				cout<<ans[j]<<" ";
			}
			cout<<endl;
			return 0;
		}
	}
	cout<<0<<endl<<0<<endl;
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

const int maxn=2e5+100;

int a[maxn];
int tmp[20];

bool check(int num){
	int t=1<<num;
	for(int i=0;i<num;++i){
		if(tmp[i]>=t) return false;
	}
	return 1;
}

int main(){

	//cout<<(1<<17)<<endl;
	ios::sync_with_stdio(false);
	for(int i=0;i<20;++i){
		tmp[i]=maxn;
	}
	int n;
	cin>>n;
	for(int i=0;i<n;++i){
		cin>>a[i];
	}
	sort(a,a+n);
	for(int i=0;i<n;++i){
		for(int j=0;j<18;++j){
			if(tmp[j]==maxn&&a[i]&(1<<j)){
				tmp[j]=a[i];
				break;
			}
		}
	}

	for(int i=17;i;--i){
		if(check(i)){
			cout<<i<<endl;
			// for(int j=0;j<i;++j){
			// 	cout<<tmp[j]<<" ";
			// }
			// cout<<endl;
			int num=0;
			int t=0;
			int fg=1;
			int ans=0;
			while(num<(1<<i)){
				cout<<ans<<" ";
				++num;
				ans^=tmp[t];
				if(t+fg>=i||t+fg<0){
					fg*=-1;
				}
				t+=fg;
			}
			return 0;
		}
	}
	cout<<0<<endl<<0<<endl;
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

const int maxn=3e5+100;

int a[maxn];
int tmp[20];
int ans[maxn];

bool check(int num){
	int t=1<<num;
	for(int i=0;i<num;++i){
		if(tmp[i]>=t) return false;
	}
	return 1;
}

int main(){

	//cout<<(1<<18)-1<<endl;
	ios::sync_with_stdio(false);
	for(int i=0;i<20;++i){
		tmp[i]=maxn;
	}
	int n;
	cin>>n;
	for(int i=0;i<n;++i){
		cin>>a[i];
	}
	sort(a,a+n);
	for(int i=0;i<n;++i){
		for(int j=0;j<18;++j){
			if(tmp[j]==maxn&&(1<<j)&a[i]){
				tmp[j]=a[i];
				break;
			}
		}
	}
	// for(int j=0;j<=17;++j){
	// 	cout<<tmp[j]<<" ";
	// }
	// cout<<endl;
	for(int i=18;i;--i){
		if(check(i)){
			cout<<i<<endl;

			int cnt=0;
			ans[cnt]=0;
			for(int j=0;j<i;++j){
				for(int k=cnt;k>=0;--k){
					ans[++cnt]=ans[k]^tmp[j];
				}
			}
			for(int j=0;j<=cnt;++j){
				cout<<ans[j]<<" ";
			}
			cout<<endl;
			return 0;
		}
	}
	cout<<0<<endl<<0<<endl;
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

const int maxn=3e5+100;

int a[maxn];
int tmp[20];
int ans[maxn];

bool check(int num){
	int t=1<<num;
	for(int i=0;i<num;++i){
		if(tmp[i]>=t) return false;
	}
	return 1;
}

int main(){

	//cout<<(1<<18)-1<<endl;
	ios::sync_with_stdio(false);
	for(int i=0;i<20;++i){
		tmp[i]=maxn;
	}
	int n;
	cin>>n;
	for(int i=0;i<n;++i){
		cin>>a[i];
	}
	sort(a,a+n);
	for(int i=0;i<n;++i){
		for(int j=17;j>=0;--j){
			if(tmp[j]==maxn&&(1<<j)&a[i]){
				tmp[j]=a[i];
				break;
			}
		}
	}
	// for(int j=0;j<=17;++j){
	// 	cout<<tmp[j]<<" ";
	// }
	// cout<<endl;
	for(int i=18;i;--i){
		if(check(i)){
			cout<<i<<endl;

			int cnt=0;
			ans[cnt]=0;
			for(int j=0;j<i;++j){
				for(int k=cnt;k>=0;--k){
					ans[++cnt]=ans[k]^tmp[j];
				}
			}
			for(int j=0;j<=cnt;++j){
				cout<<ans[j]<<" ";
			}
			cout<<endl;
			return 0;
		}
	}
	cout<<0<<endl<<0<<endl;
	return 0;
}


// In the name of Allah. Ya ali!
#include<bits/stdc++.h>
#define double long double
#define debug(x)  cerr << #x << " = " << x << endl
typedef long long ll;
const ll MAX_N = 2e5+100;
const ll MOD = 1e9+7;
using namespace std;

int a[MAX_N];
int n;

int main()
{
    cin >> n;
    for(int i = 1;i<=n;++i)
        cin >> a[i];
    set<int> st;
    st.insert(0);
    for(int i = 1;i<=n;++i)
    {
        if (st.count(a[i]))
            continue;
        set<int> newst;
        for(auto x:st)
            newst.insert(a[i]^x);
        for(auto x:newst)
            st.insert(x);
    }
    int mx = 0;
    for(auto x:st)
    {
        if (!x)
            continue;
        if (x==mx+1)
            mx++;
        else
            break;
    }
    int x = (int)floor(log2(mx+1));
    vector<int> vec;
    vec.push_back(0);
    st.clear();
    for(int i = 1;i<=n;++i)
    {
        if (a[i]<(1<<x))
            st.insert(a[i]);
    }
    set<int> tst;
    tst.insert(0);
    for(int i = 1;i<=x;++i)
    {
        while(st.size())
        {
            int u = *st.begin();
            st.erase(u);
            if (tst.count(u))
                continue;
            for(int j = (1<<(i-1))-1;j>=0;--j)
                vec.push_back(vec[j]^u),tst.insert(vec[j]^u);
            break;
        }
    }
    cout << x << "\n";
    for(auto x:vec)
        printf("%d ",x);
    return 0;
}


// In the name of Allah. Ya ali!
#include<bits/stdc++.h>
#define double long double
#define debug(x)  cerr << #x << " = " << x << endl
typedef long long ll;
const ll MAX_N = 2e5+100;
const ll MOD = 1e9+7;
using namespace std;

int a[MAX_N];
int n;

int main()
{
    cin >> n;
    for(int i = 1;i<=n;++i)
        cin >> a[i];
    set<int> st;
    st.insert(0);
    for(int i = 1;i<=n;++i)
    {
        if (st.count(a[i]))
            continue;
        set<int> newst;
        for(auto x:st)
            newst.insert(a[i]^x);
        for(auto x:newst)
            st.insert(x);
    }
    int mx = 0;
    for(auto x:st)
    {
        if (!x)
            continue;
        if (x==mx+1)
            mx++;
        else
            break;
    }
    int x = (int)floor(log2(mx+1));
    vector<int> vec;
    vec.push_back(0);
    st.clear();
    for(int i = 1;i<=n;++i)
    {
        if (a[i]<(1<<x))
            st.insert(a[i]);
    }
    set<int> tst;
    tst.insert(0);
    for(int i = 1;i<=x;++i)
    {
        while(st.size())
        {
            int u = *st.begin();
            st.erase(u);
            if (tst.count(u))
                continue;
            for(int j = (1<<(i-1))-1;j>=0;--j)
                vec.push_back(vec[j]^u),tst.insert(vec[j]^u);
            break;
        }
    }
    cout << (log2(vec.size())) << "\n";
    for(auto x:vec)
        printf("%d ",x);
    return 0;
}


// In the name of Allah. Ya ali!
#include<bits/stdc++.h>
#define double long double
#define debug(x)  cerr << #x << " = " << x << endl
typedef long long ll;
const ll MAX_N = 2e5+100;
const ll MOD = 1e9+7;
using namespace std;

int a[MAX_N];
int n;

int main()
{
    cin >> n;
    for(int i = 1;i<=n;++i)
        cin >> a[i];
    set<int> st;
    st.insert(0);
    for(int i = 1;i<=n;++i)
    {
        if (st.count(a[i]))
            continue;
        set<int> newst;
        for(auto x:st)
            newst.insert(a[i]^x);
        for(auto x:newst)
            st.insert(x);
    }
    int mx = 0;
    for(auto x:st)
    {
        if (!x)
            continue;
        if (x==mx+1)
            mx++;
        else
            break;
    }
    int x = (int)floor(log2(mx+1));
    vector<int> vec;
    vec.push_back(0);
    st.clear();
    for(int i = 1;i<=n;++i)
    {
        if (a[i]<(1<<x))
            st.insert(a[i]);
    }
    set<int> tst;
    tst.insert(0);
    for(int i = 1;i<=x;++i)
    {
        while(st.size())
        {
            int u = *st.begin();
            st.erase(u);
            if (tst.count(u))
                continue;
            for(int j = 0;j<(1<<(i-1));++j)
                vec.push_back(vec[j]^u),tst.insert(vec[j]^u);
            break;
        }
    }
    cout << x << "\n";
    for(auto x:vec)
        printf("%d ",x);
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long int LL;
typedef pair<int, int> par;
typedef pair<double, double> pdd;
#define x first
#define y second

vector<int> num;

void solve(int cnt) {
    vector<int> arr, ori;
    for (int x: num) {
        if (x < (1 << cnt)) {
            arr.push_back(x);
        }
    }
    ori = arr;

    int n = arr.size();
    for (int i = 0; i < cnt; i++) {
        int id = -1;
        for (int j = i; j < n; j++) {
            if (arr[j] & (1 << i)) {
                id = j;
                break;
            }
        }
        if (id == -1) return;
        swap(arr[i], arr[id]);
        swap(ori[i], ori[id]);
        for (int j = i + 1; j < n; j++) {
            if (arr[j] & (1 << i)) {
                arr[j] ^= arr[i];
            }
        }
    }
    arr = ori;
    while (arr.size() > cnt) arr.pop_back();

    /* printf("%d\n", cnt);
    for (int x: arr) {
        printf("%d ", x);
    }
    puts("");
    exit(0); */
    vector<int> opr;
    for (int x: arr) {
        int sz = opr.size();
        opr.push_back(x);
        for (int i = sz - 1; i >= 0; i--) {
            opr.push_back(opr[i]);
        }
    }

    int ans = 0;
    printf("%d", ans);
    for (int x: opr) {
        ans ^= x;
        printf(" %d", ans);
    }
    puts("");
    exit(0);
}

int main() {
    int n;
    scanf("%d", &n);
    for (int i = 0; i < n; i++) {
        int x;
        scanf("%d", &x);
        num.push_back(x);
    }
    for (int i = 18; i >= 1; i--) {
        solve(i);
    }
    puts("0\n0");
}


#include<bits/stdc++.h>
using namespace std;
typedef long long int LL;
typedef pair<int, int> par;
typedef pair<double, double> pdd;
#define x first
#define y second

vector<int> num;

void solve(int cnt) {
    vector<int> arr, ori;
    for (int x: num) {
        if (x < (1 << cnt)) {
            arr.push_back(x);
        }
    }
    ori = arr;

    int n = arr.size();
    for (int i = 0; i < cnt; i++) {
        int id = -1;
        for (int j = i; j < n; j++) {
            if (arr[j] & (1 << i)) {
                id = j;
                break;
            }
        }
        if (id == -1) return;
        swap(arr[i], arr[id]);
        swap(ori[i], ori[id]);
        for (int j = i + 1; j < n; j++) {
            if (arr[j] & (1 << i)) {
                arr[j] ^= arr[i];
            }
        }
    }
    arr = ori;
    while (arr.size() > cnt) arr.pop_back();

    printf("%d\n", cnt);
    /* for (int x: arr) {
        printf("%d ", x);
    }
    puts("");
    exit(0); */
    vector<int> opr;
    for (int x: arr) {
        int sz = opr.size();
        opr.push_back(x);
        for (int i = sz - 1; i >= 0; i--) {
            opr.push_back(opr[i]);
        }
    }

    int ans = 0;
    printf("%d", ans);
    for (int x: opr) {
        ans ^= x;
        printf(" %d", ans);
    }
    puts("");
    exit(0);
}

int main() {
    int n;
    scanf("%d", &n);
    for (int i = 0; i < n; i++) {
        int x;
        scanf("%d", &x);
        num.push_back(x);
    }
    for (int i = 18; i >= 1; i--) {
        solve(i);
    }
    puts("0\n0");
}


#include<bits/stdc++.h>
using namespace std;
typedef long long int LL;
typedef pair<int, int> par;
typedef pair<double, double> pdd;
#define x first
#define y second

vector<int> num;

void solve(int cnt) {
    vector<vector<int>> digit(cnt + 1);
    int mask = 0;

    for (int x: num) {
        if (x >= (1 << cnt))
            continue;
        digit[__builtin_popcount(x)].push_back(x);
    }

    vector<int> arr;
    for (int i = 1; i <= cnt; i++) {
        for (int x: digit[i]) {
            if (__builtin_popcount(x ^ (x & mask)) == 1) {
                arr.push_back(x);
                mask |= x;
            }
        }
    }

    if (arr.size() != cnt) return;

    printf("%d\n", cnt);
    /* for (int x: arr) {
        printf("%d ", x);
    }
    puts(""); */
    vector<int> opr;
    for (int x: arr) {
        int sz = opr.size();
        opr.push_back(x);
        for (int i = sz - 1; i >= 0; i--) {
            opr.push_back(opr[i]);
        }
    }

    int ans = 0;
    printf("%d", ans);
    for (int x: opr) {
        ans ^= x;
        printf(" %d", ans);
    }
    puts("");
    exit(0);
}

int main() {
    int n;
    scanf("%d", &n);
    for (int i = 0; i < n; i++) {
        int x;
        scanf("%d", &x);
        num.push_back(x);
    }
    for (int i = 20; i >= 1; i--) {
        solve(i);
    }
    puts("0\n0");
}