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: #include <bits/stdc++.h>
  using namespace std;
  long long mod = 1e9 + 7;
  int head[1000005];
  struct edge {
    int to, next;
  };
  edge e[1000005 << 1];
  int cmt = 0;
  int T;
  int n, m;
  int tree[1000005 << 2];
  void Add(int x, int k) {
    while (x <= n) {
      tree[x] += k;
      x += x & -x;
    }
  }
  int Sum(int x) {
    int ans = 0;
    while (x != 0) {
      ans += tree[x];
      x -= x & -x;
    }
    return ans;
  }
  void add(int u, int v) {
    e[++cmt].to = v;
    e[cmt].next = head[u];
    head[u] = cmt;
  }
  template <typename Tp>
  inline void read(Tp &x) {
    x = 0;
    int f = 1;
    char ch = getchar();
    while (!isdigit(ch)) {
      if (ch == '-') f = -1;
      ch = getchar();
    }
    while (isdigit(ch)) {
      x = x * 10 + ch - '0';
      ch = getchar();
    }
    x *= f;
  }
  int buf[42];
  template <typename Tp>
  inline void write(Tp x) {
    int p = 0;
    if (x < 0) {
      putchar('-');
      x = -x;
    }
    if (x == 0) {
      putchar('0');
      return;
    }
    while (x) {
      buf[++p] = x % 10;
      x /= 10;
    }
    for (int i = p; i; i--) putchar('0' + buf[i]);
  }
  long long quick(long long a, long long b) {
    long long ret = 1;
    long long now = a;
    while (b) {
      if (b & 1) {
        ret *= now;
        ret %= mod;
      }
      now *= now;
      now %= mod;
      b >>= 1;
    }
    return ret;
  }
  long long Inv(long long b) {
    if (b == 1) return 1;
    return (mod - mod / b) * Inv(mod % b) % mod;
  }
  long long fac[1000005], inv[1000005];
  void getc() {
    fac[0] = inv[0] = 1;
    for (int i = 1; i < n; i++) fac[i] = fac[i - 1] * i % mod;
    inv[n - 1] = quick(fac[n - 1], mod - 2);
    for (int i = n - 2; i; i--) inv[i] = inv[i + 1] * (i + 1) % mod;
  }
  long long c(int a, int b) { return fac[n] * inv[m] % mod * inv[n - m] % mod; }
  long long C(long long a, long long b, long long p) {
    if (b > a) return 0;
    long long res = 1;
    for (long long i = 1, j = a; i <= b; i++, j--) {
      res = res * j % p;
      res = res * quick(i, mod - 2) % p;
    }
    return res;
  }
  long long lucas(long long n, long long m, long long p) {
    return m ? lucas(n / p, m / p, p) * C(n % p, m % p, p) % p : 1;
  }
  map<int, int> mp;
  int r[1 << 13], ans[1 << 13], cnt = 0;
  ;
  int Ans[1000005];
  vector<int> g[1000005];
  int main() {
    T = 1;
    ans[0] = 1;
    while (T--) {
      read(n);
      for (int i = 0; i < 1 << 13; i++) {
        g[i].push_back(0);
        r[i] = 1 << 13;
      }
      for (int i = 1; i <= n; i++) {
        int x;
        read(x);
        for (auto v : g[x]) {
          int xv = x ^ v;
          ans[xv] = 1;
          while (r[xv] > x) {
            r[xv]--;
            if (r[xv] != x) {
              g[r[xv]].push_back(xv);
            }
          }
        }
        g[x].clear();
      }
      for (int i = 0; i < 1 << 13; i++) {
        if (ans[i]) Ans[++cnt] = i;
      }
      write(cnt);
      putchar('\n');
      for (int i = 1; i <= cnt; i++) printf("%d ", Ans[i]);
      cout << endl;
    }
    return 0;
  }
  