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>
  #pragma GCC optimize(3)
  #pragma GCC optimize("Ofast")
  #pragma GCC target( \
      "sse,sse2,sse3,ssse3,sse4,sse4.1,sse4.2,popcnt,abm,mmx,avx,avx2")
  #pragma GCC optimize("unroll-loops")
  #pragma GCC optimize("profile-values,profile-reorder-functions,tracer")
  #pragma GCC optimize("vpt")
  #pragma GCC optimize("rename-registers")
  #pragma GCC optimize("move-loop-invariants")
  #pragma GCC optimize("unswitch-loops")
  #pragma GCC optimize("function-sections")
  #pragma GCC optimize("data-sections")
  #pragma GCC optimize("branch-target-load-optimize")
  #pragma GCC optimize("branch-target-load-optimize2")
  #pragma GCC optimize("btr-bb-exclusive")
  #pragma GCC optimize("inline")
  #pragma GCC optimize("-fgcse")
  #pragma GCC optimize("-fgcse-lm")
  #pragma GCC optimize("-fipa-sra")
  #pragma GCC optimize("-ftree-pre")
  #pragma GCC optimize("-ftree-vrp")
  #pragma GCC optimize("-fpeephole2")
  #pragma GCC optimize("-ffast-math")
  #pragma GCC optimize("-fsched-spec")
  #pragma GCC optimize("-falign-jumps")
  #pragma GCC optimize("-falign-loops")
  #pragma GCC optimize("-falign-labels")
  #pragma GCC optimize("-fdevirtualize")
  #pragma GCC optimize("-fcaller-saves")
  #pragma GCC optimize("-fcrossjumping")
  #pragma GCC optimize("-fthread-jumps")
  #pragma GCC optimize("-freorder-blocks")
  #pragma GCC optimize("-fschedule-insns")
  #pragma GCC optimize("inline-functions")
  #pragma GCC optimize("-ftree-tail-merge")
  #pragma GCC optimize("-fschedule-insns2")
  #pragma GCC optimize("-fstrict-aliasing")
  #pragma GCC optimize("-falign-functions")
  #pragma GCC optimize("-fcse-follow-jumps")
  #pragma GCC optimize("-fsched-interblock")
  #pragma GCC optimize("-fpartial-inlining")
  #pragma GCC optimize("no-stack-protector")
  #pragma GCC optimize("-freorder-functions")
  #pragma GCC optimize("-findirect-inlining")
  #pragma GCC optimize("-fhoist-adjacent-loads")
  #pragma GCC optimize("-frerun-cse-after-loop")
  #pragma GCC optimize("inline-small-functions")
  #pragma GCC optimize("-finline-small-functions")
  #pragma GCC optimize("-ftree-switch-conversion")
  #pragma GCC optimize("-foptimize-sibling-calls")
  #pragma GCC optimize("-fexpensive-optimizations")
  #pragma GCC optimize("inline-functions-called-once")
  #pragma GCC optimize("-fdelete-null-pointer-checks")
  using namespace std;
  template <typename T1, typename T2>
  void ckmin(T1 &a, T2 b) {
    if (a > b) a = b;
  }
  template <typename T1, typename T2>
  void ckmax(T1 &a, T2 b) {
    if (a < b) a = b;
  }
  int read() {
    int x = 0, f = 0;
    char ch = getchar();
    while (!isdigit(ch)) f |= ch == '-', ch = getchar();
    while (isdigit(ch)) x = 10 * x + ch - '0', ch = getchar();
    return f ? -x : x;
  }
  template <typename T>
  void print(T x) {
    if (x < 0) putchar('-'), x = -x;
    if (x >= 10) print(x / 10);
    putchar(x % 10 + '0');
  }
  template <typename T>
  void print(T x, char let) {
    print(x), putchar(let);
  }
  vector<int> qaq[8192];
  int dp[5005][8192];
  int a[1000005], n;
  int ask(int val, int pos) {
    int t = lower_bound(qaq[val].begin(), qaq[val].end(), pos) - qaq[val].begin();
    if (t >= (int(qaq[val].size())))
      return 1e9;
    else
      return qaq[val][t];
  }
  int main() {
    n = read();
    for (int i = 1; i <= n; i++) {
      a[i] = read();
      qaq[a[i]].push_back(i);
    }
    memset(dp, 0x3f, sizeof(dp));
    dp[0][0] = 0;
    for (int i = 0; i <= 5000; i++) {
      for (int j = 0; j < 8192; j++) {
        if (dp[i][j] > n) continue;
        ckmin(dp[i + 1][j], dp[i][j]);
        ckmin(dp[i + 1][j ^ (i + 1)], ask(i + 1, dp[i][j]));
      }
    }
    vector<int> QAQ;
    for (int j = 0; j < 8192; j++)
      if (dp[5001][j] <= n) QAQ.push_back(j);
    printf("%d\n", (int(QAQ.size())));
    for (auto v : QAQ) printf("%d ", v);
    puts("");
    return 0;
  }
  