Problem p01465 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <stdio.h>
#define BEGIN_STACK_EXTEND(size)                                               \\
  void *stack_extend_memory_ = malloc(size);                                   \\
  void *stack_extend_origin_memory_;                                           \\
  char *stack_extend_dummy_memory_ = (char *)alloca(                           \\
      (1 + (int)(((long long)stack_extend_memory_) & 127)) * 16);              \\
  *stack_extend_dummy_memory_ = 0;                                             \\
  asm volatile("mov %%rsp, %%rbx\\nmov %%rax, %%rsp"                            \\
               : "=b"(stack_extend_origin_memory_)                             \\
               : "a"((char *)stack_extend_memory_ + (size)-1024));

#define END_STACK_EXTEND                                                       \\
  asm volatile("mov %%rax, %%rsp" ::"a"(stack_extend_origin_memory_));         \\
  free(stack_extend_memory_);
using namespace std;
char str[1100000];
int mod = 1000000007;
int cur;
pair<int, int> expr();
pair<int, int> fact() {
  if (str[cur] == '~') {
    cur++;
    pair<int, int> ret = fact();
    swap(ret.first, ret.second);
    return ret;
  }
  if (str[cur] == '(') {
    cur++;
    pair<int, int> ret = expr();
    cur++;
    return ret;
  }
  while (str[cur] == 'x' || ('0' <= str[cur] && str[cur] <= '9'))
    cur++;
  return make_pair(1, 1);
}
pair<int, int> term() {
  pair<int, int> ret = fact();
  while (str[cur] == '&') {
    cur++;
    pair<int, int> tmp = fact();
    pair<int, int> to =
        make_pair((int)(((long long)ret.first * tmp.first) % mod),
                  (int)(((long long)ret.second * tmp.second +
                         (long long)ret.first * tmp.second +
                         (long long)ret.second * tmp.first) %
                        mod));
    ret = to;
  }
  return ret;
}
pair<int, int> expr() {
  pair<int, int> ret = term();
  while (str[cur] == '|') {
    cur++;
    pair<int, int> tmp = term();
    pair<int, int> to =
        make_pair((int)(((long long)ret.first * tmp.first +
                         (long long)ret.first * tmp.second +
                         (long long)ret.second * tmp.first) %
                        mod),
                  (int)(((long long)ret.second * tmp.second) % mod));
    ret = to;
  }
  return ret;
}
int main() {
  BEGIN_STACK_EXTEND(60 * 1024 * 1024);
  scanf("%s", str);
  printf("%d\\n", expr().first);
  END_STACK_EXTEND;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <stdio.h>
#define BEGIN_STACK_EXTEND(size)                                               \\
  void *stack_extend_memory_ = malloc(size);                                   \\
  void *stack_extend_origin_memory_;                                           \\
  char *stack_extend_dummy_memory_ = (char *)alloca(                           \\
      (1 + (int)(((long long)stack_extend_memory_) & 127)) * 16);              \\
  *stack_extend_dummy_memory_ = 0;                                             \\
  asm volatile("mov %%rsp, %%rbx\\nmov %%rax, %%rsp"                            \\
               : "=b"(stack_extend_origin_memory_)                             \\
               : "a"((char *)stack_extend_memory_ + (size)-1024));
#define END_STACK_EXTEND                                                       \\
  asm volatile("mov %%rax, %%rsp" ::"a"(stack_extend_origin_memory_));         \\
  free(stack_extend_memory_);
using namespace std;
char str[1100000];
int mod = 1000000007;
int cur;
pair<long long, long long> expr();
pair<long long, long long> fact() {
  if (str[cur] == '~') {
    cur++;
    pair<long long, long long> ret = fact();
    swap(ret.first, ret.second);
    return ret;
  }
  if (str[cur] == '(') {
    cur++;
    pair<long long, long long> ret = expr();
    cur++;
    return ret;
  }
  while (str[cur] == 'x' || ('0' <= str[cur] && str[cur] <= '9'))
    cur++;
  return make_pair(1, 1);
}
pair<long long, long long> term() {
  pair<long long, long long> ret = fact();
  while (str[cur] == '&') {
    cur++;
    pair<long long, long long> tmp = fact();
    pair<long long, long long> to =
        make_pair(ret.first * tmp.first % mod,
                  (ret.second * tmp.second + ret.first * tmp.second +
                   ret.second * tmp.first) %
                      mod);
    ret = to;
  }
  return ret;
}
pair<long long, long long> expr() {
  pair<long long, long long> ret = term();
  while (str[cur] == '|') {
    cur++;
    pair<long long, long long> tmp = term();
    pair<long long, long long> to =
        make_pair((ret.first * tmp.first + ret.first * tmp.second +
                   ret.second * tmp.first) %
                      mod,
                  ret.second * tmp.second % mod);
    ret = to;
  }
  return ret;
}
int main() {
  BEGIN_STACK_EXTEND(60 * 1024 * 1024);
  scanf("%s", str);
  printf("%lld\\n", expr().first);
  END_STACK_EXTEND;
}