description: This is a harder version of the problem with bigger constraints.
  
  Korney Korneevich dag up an array a of length n. Korney Korneevich has recently read about the operation [bitwise XOR](https://en.wikipedia.org/wiki/Bitwise_operation#XOR), so he wished to experiment with it. For this purpose, he decided to find all integers x ≥ 0 such that there exists an increasing subsequence of the array a, in which the bitwise XOR of numbers is equal to x.
  
  It didn't take a long time for Korney Korneevich to find all such x, and he wants to check his result. That's why he asked you to solve this problem!
  
  A sequence s is a subsequence of a sequence b if s can be obtained from b by deletion of several (possibly, zero or all) elements.
  
  A sequence s_1, s_2, … , s_m is called increasing if s_1 < s_2 < … < s_m.
  
  Input
  
  The first line contains a single integer n (1 ≤ n ≤ 10^6).
  
  The second line contains n integers a_1, a_2, …, a_n (0 ≤ a_i ≤ 5000) — the elements of the array a.
  
  Output
  
  In the first line print a single integer k — the number of found x values.
  
  In the second line print k integers in increasing order x_1, x_2, … x_k (0 ≤ x_1 < … < x_k) — found x values.
  
  Examples
  
  Input
  
  
  4
  4 2 2 4
  
  
  Output
  
  
  4
  0 2 4 6 
  
  
  Input
  
  
  8
  1 0 1 7 12 5 3 2
  
  
  Output
  
  
  12
  0 1 2 3 4 5 6 7 10 11 12 13 
  
  Note
  
  In the first test case:
  
    * To get value x = 0 it is possible to choose and empty subsequence 
    * To get value x = 2 it is possible to choose a subsequence [2] 
    * To get value x = 4 it is possible to choose a subsequence [4] 
    * To get value x = 6 it is possible to choose a subsequence [2, 4] 
solution: import java.io.BufferedWriter;
  import java.io.IOException;
  import java.io.InputStream;
  import java.io.OutputStreamWriter;
  
  import java.util.ArrayList;
  import java.util.Arrays;
  import java.util.Collections;
  import java.util.HashMap;
  import java.util.HashSet;
  import java.util.Iterator;
  import java.util.LinkedList;
  import java.util.PriorityQueue;
  import java.util.Map.Entry;
  
  
  import java.util.Random;
  import java.util.TreeSet;
  
  public final class CF_750_D2_F2v5
  {
  
  
  	static boolean verb=true;
  	static void log(Object X){if (verb) System.err.println(X);}
  	static void log(Object[] X){if (verb) {for (Object U:X) System.err.print(U+" ");System.err.println("");}}
  	static void log(int[] X){if (verb) {for (int U:X) System.err.print(U+" ");System.err.println("");}}
  	static void logWln(int[] X){if (verb) {for (int U:X) System.err.print(U+" ");}}
  	static void log(int[] X,int L){if (verb) {for (int i=0;i<L;i++) System.err.print(X[i]+" ");System.err.println("");}}
  	static void log(long[] X){if (verb) {for (long U:X) System.err.print(U+" ");System.err.println("");}}
  
  	static void logWln(Object X){if (verb) System.err.print(X);}
  	static void info(Object o){	System.out.println(o);}
  	static void output(Object o){outputWln(""+o+"\n");	}
  	static void outputFlush(Object o){try {out.write(""+ o+"\n");out.flush();} catch (Exception e) {}}
  	static void outputWln(Object o){try {out.write(""+ o);} catch (Exception e) {}}
  
  
  	static void logBin(int[] tm) {for (int x:tm) logWln(bin(8,x)+" ");log("");}
  
  	static String bin(int L,int x) {
  		String s=Integer.toBinaryString(x);
  		while (s.length()<L) s="0"+s;
  		return s;
  	}
  
  	// Global vars
  	static BufferedWriter out;
  	static InputReader reader;
  
  
  	static void test() {
  		log("testing");
  
  		Random r=new Random();
  		int NTESTS=1000000;
  		int NMAX=6;
  		int VMAX=10;
  		for (int t=0;t<NTESTS;t++) {
  
  
  		}
  		log("testing done");
  
  	}
  
  
  
  
  	static class Composite implements Comparable<Composite>{
  
  		int a;
  		int mn;
  		
  		public int compareTo(Composite X) {
  			if (mn!=X.mn)
  				return mn-X.mn;
  			return a-X.a;
  		}
  
  		public Composite(int a, int mn) {
  			this.a = a;
  			this.mn = mn;
  		}
  		
  
  		
  
  	}
  
  	static void process() throws Exception {
  
  
  		out = new BufferedWriter(new OutputStreamWriter(System.out));
  		reader = new InputReader(System.in);
  
  
  		int VX=8192;
  		log("allocating");
  		boolean[][] znx=new boolean[VX][VX];
  		int[][] stack=new int[VX][VX];
  		int[] st=new int[VX];
  		log("starting");
  		int n=reader.readInt();
  		//log(VX*VX);
  		int[] a=new int[n];
  		int[] cnt=new int[VX];
  		for (int i=0;i<n;i++) {
  			a[i]=reader.readInt();
  			cnt[a[i]]++;
  		}
  		int[] idx=new int[VX];
  		int[][] lst=new int[VX][];
  		for (int u=0;u<VX;u++)
  			lst[u]=new int[cnt[u]];
  		for (int i=0;i<n;i++) {
  			int x=a[i];
  			lst[x][idx[x]++]=i;
  		}
  	
  		
  		for (int u=0;u<VX;u++) {
  			if (cnt[u]>0) {
  				stack[u][st[u]++]=0;
  			}
  		}
  		
  		int[] mem=new int[VX];
  	
  
  		int[] ptr=new int[VX];
  		int[] top=new int[VX];
  		Arrays.fill(top, VX);
  		top[0]=0;
  		for (int i=0;i<n;i++) {
  			int x=a[i];
  			cnt[x]--;
  
  			//log("===== processing x:"+x);
  			for (int e=0;e<st[x];e++) {
  				int u=stack[x][e];
  				// we process
  				int v=u^x;
  				mem[v]=1;
  				//log("introducing v:"+v);
  				// this must be available to all y>x
  				for (int y=x+1;y<top[v];y++) {
  					if (cnt[y]>0) {
  						//if (!znx[y][v]) {
  							znx[y][v]=true;
  							stack[y][st[y]++]=v;
  						//}
  					}
  				}
  				top[v]=Math.min(top[v],x+1);
  			}
  			// we clean
  			st[x]=0;
  		}
  
  		mem[0]=1;
  		int cn=0;
  		for (int i=0;i<VX;i++)
  			if (mem[i]==1)
  				cn++;
  		StringBuffer sb=new StringBuffer();
  		sb.append(cn+"\n");
  
  		for (int i=0;i<VX;i++) {
  			if (mem[i]==1) {
  				sb.append(i+" ");
  			}
  		}
  		output(sb);
  
  
  
  
  		try {
  			out.close();
  		} catch (Exception Ex) {
  		}
  
  
  	}
  
  
  
  
  
  	public static void main(String[] args) throws Exception {
  		process();
  
  	}
  
  	static final class InputReader {
  		private final InputStream stream;
  		private final byte[] buf = new byte[1024];
  		private int curChar;
  		private int numChars;
  
  		public InputReader(InputStream stream) {
  			this.stream = stream;
  		}
  
  		private int read() throws IOException {
  			if (curChar >= numChars) {
  				curChar = 0;
  				numChars = stream.read(buf);
  				if (numChars <= 0) {
  					return -1;
  				}
  			}
  			return buf[curChar++];
  		}
  
  		public final String readString() throws IOException {
  			int c = read();
  			while (isSpaceChar(c)) {
  				c = read();
  			}
  			StringBuilder res = new StringBuilder();
  			do {
  				res.append((char) c);
  				c = read();
  			} while (!isSpaceChar(c));
  			return res.toString();
  		}
  
  		public final String readString(int L) throws IOException {
  			int c = read();
  			while (isSpaceChar(c)) {
  				c = read();
  			}
  			StringBuilder res = new StringBuilder(L);
  			do {
  				res.append((char) c);
  				c = read();
  			} while (!isSpaceChar(c));
  			return res.toString();
  		}
  
  		public final int readInt() throws IOException {
  			int c = read();
  			boolean neg = false;
  			while (isSpaceChar(c)) {
  				c = read();
  			}
  			char d = (char) c;
  			// log("d:"+d);
  			if (d == '-') {
  				neg = true;
  				c = read();
  			}
  			int res = 0;
  			do {
  				res *= 10;
  				res += c - '0';
  				c = read();
  			} while (!isSpaceChar(c));
  			// log("res:"+res);
  			if (neg)
  				return -res;
  			return res;
  
  		}
  
  		public final long readLong() throws IOException {
  			int c = read();
  			boolean neg = false;
  			while (isSpaceChar(c)) {
  				c = read();
  			}
  			char d = (char) c;
  			// log("d:"+d);
  			if (d == '-') {
  				neg = true;
  				c = read();
  			}
  			long res = 0;
  			do {
  				res *= 10;
  				res += c - '0';
  				c = read();
  			} while (!isSpaceChar(c));
  			// log("res:"+res);
  			if (neg)
  				return -res;
  			return res;
  
  		}
  
  		private boolean isSpaceChar(int c) {
  			return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
  		}
  	}
  
  }
  