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(2, 3, "Ofast")
  namespace MyMinMax {
  template <typename T>
  inline T mn(const T x, const T y) {
    return x < y ? x : y;
  }
  template <typename T>
  inline T mx(const T x, const T y) {
    return x > y ? x : y;
  }
  template <typename T>
  inline bool chmin(T &x, const T y) {
    return (x > y) && ((x = y), 1);
  }
  template <typename T>
  inline bool chmax(T &x, const T y) {
    return (x < y) && ((x = y), 1);
  }
  template <typename T, typename... Args>
  inline T mx(const T x, const Args... args) {
    return mx(x, mx(args...));
  }
  template <typename T, typename... Args>
  inline T mn(const T x, const Args... args) {
    return mn(x, mn(args...));
  }
  }  // namespace MyMinMax
  using namespace MyMinMax;
  namespace IO {
  const int DPAIRSIZ = 1 << 18;
  char BB[DPAIRSIZ], *SS = BB, *TT = BB;
  inline char getcha() {
    return SS == TT && (TT = (SS = BB) + fread(BB, 1, DPAIRSIZ, stdin), SS == TT)
               ? EOF
               : *SS++;
  }
  template <typename T = int>
  inline T read() {
    T x = 0;
    int fu = 1;
    char c = getcha();
    while (c < 48) {
      if (c == 45) {
        fu = -1;
      }
      c = getcha();
    }
    while (c >= 48) {
      x = x * 10 + c - 48;
      c = getcha();
    }
    x *= fu;
    return x;
  }
  template <typename T>
  inline void read(T &x) {
    x = 0;
    int fu = 1;
    char c = getcha();
    while (c < 48) {
      if (c == 45) {
        fu = -1;
      }
      c = getcha();
    }
    while (c >= 48) {
      x = x * 10 + c - 48;
      c = getcha();
    }
    x *= fu;
  }
  template <typename T>
  inline void read(T *bg, T *ed) {
    while (bg != ed) read(*bg++);
  }
  inline void read(char &ch) {
    ch = getcha();
    while (ch <= 32) ch = getcha();
  }
  inline void read(char *s) {
    char ch = getcha();
    while (ch <= 32) ch = getcha();
    while (ch > 32) *s++ = ch, ch = getcha();
    *s = '\0';
  }
  inline void getl(char *s) {
    char ch = getcha();
    while (ch < 32) ch = getcha();
    while (ch >= 32) *s++ = ch, ch = getcha();
    *s = '\0';
  }
  inline void pread(char *&s) {
    char ch = getcha();
    while (ch <= 32) ch = getcha();
    while (ch > 32) *s++ = ch, ch = getcha();
    *s = '\0';
  }
  inline void pgetl(char *&s) {
    char ch = getcha();
    while (ch <= 32) ch = getcha();
    while (ch > 32) *s++ = ch, ch = getcha();
    *s = '\0';
  }
  template <typename T, typename... Args>
  inline void read(T &x, Args &...args) {
    read(x);
    read(args...);
  }
  char out[DPAIRSIZ], *Out = out;
  inline void putcha(char x) {
    *Out++ = x;
    if (Out - out >= (DPAIRSIZ)) fwrite(out, 1, Out - out, stdout), Out = out;
  }
  template <typename T>
  inline void fprint(T x) {
    if (x < 0) putcha(45), x = -x;
    if (x > 9) fprint(x / 10);
    putcha(x % 10 + 48);
  }
  inline void print() { putcha(10); }
  template <typename T>
  inline void print(T x) {
    fprint(x);
    putcha(10);
  }
  inline void print(char *ch) {
    while (*ch != '\0') putcha(*(ch++));
    putcha(10);
  }
  inline void put(char *ch) {
    while (*ch != '\0') putcha(*(ch++));
  }
  inline void print(const char *ch) {
    while (*ch != '\0') putcha(*(ch++));
    putcha(10);
  }
  inline void put(const char *ch) {
    while (*ch != '\0') putcha(*(ch++));
  }
  template <typename T, typename... Args>
  inline void print(T x, Args... args) {
    fprint(x);
    putcha(32);
    print(args...);
  }
  template <typename... Args>
  inline void print(const char *ch, Args... args) {
    while (*ch != '\0') putcha(*(ch++));
    putcha(32);
    print(args...);
  }
  template <typename... Args>
  inline void print(char *ch, Args... args) {
    while (*ch != '\0') putcha(*(ch++));
    putcha(32);
    print(args...);
  }
  template <typename T, typename... Args>
  inline void printl(T x, Args... args) {
    fprint(x);
    putcha(10);
    printl(args...);
  }
  template <typename... Args>
  inline void printl(const char *ch, Args... args) {
    while (*ch != '\0') putcha(*(ch++));
    putcha(10);
    printl(args...);
  }
  template <typename... Args>
  inline void printl(char *ch, Args... args) {
    while (*ch != '\0') putcha(*(ch++));
    putcha(10);
    printl(args...);
  }
  template <typename T>
  inline void sprint(T x) {
    fprint(x);
    putcha(32);
  }
  template <typename T, typename... Args>
  inline void sprint(T x, Args... args) {
    fprint(x);
    putcha(32);
    sprint(args...);
  }
  template <typename T>
  inline void sprint(T *bg, T *ed) {
    while (bg != ed) sprint(*bg++);
  }
  template <typename T>
  inline void print(T *bg, T *ed) {
    while (bg != ed) sprint(*bg++);
    putcha(10);
  }
  template <typename T>
  inline void printl(T *bg, T *ed) {
    while (bg != ed) print(*bg++);
  }
  class AutoFlush {
   public:
    ~AutoFlush() { fwrite(out, 1, Out - out, stdout); }
  } __AutoFlush;
  }  // namespace IO
  using namespace IO;
  const int INF = 0x3f3f3f3f;
  const long long INFll = 0x3f3f3f3f3f3f3f3fll;
  const int MAXN = 1e6 + 5;
  int n, a[MAXN];
  std::vector<int> v[8192], ans;
  bool vis[8192][5005], b[8192];
  signed main() {
    read(n);
    read(a + 1, a + n + 1);
    vis[0][0] = 1;
    b[0] = 1;
    for (int i = (1), iEND = (n); i <= iEND; ++i)
      v[a[i]].push_back(a[i]), vis[a[i]][0] = 1;
    for (int i = (1), iEND = (n); i <= iEND; ++i) {
      while (!v[a[i]].empty()) {
        const int x = v[a[i]].back();
        v[a[i]].pop_back();
        for (int j = (a[i] + 1), jEND = (5000); j <= jEND; ++j) {
          if (vis[x][j]) break;
          v[j].push_back(x ^ j);
          vis[x][j] = 1;
        }
        b[x] = 1;
      }
    }
    for (int i = (0), iEND = (8192); i < iEND; ++i)
      if (b[i]) ans.push_back(i);
    print(ans.size());
    for (auto it : ans) sprint(it);
    print();
  }
  