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

#include <algorithm>
#include <cassert>
#include <cstdio>
#include <stack>
using namespace std;

typedef long long int64;
const int64 mod = 1000000007LL;
typedef pair<int64, int64> llp;

stack<int64> s;
void push(int64 v) { s.push(v); }
int64 pop() {
  int64 v = s.top();
  s.pop();
  return v;
}

int64 temp;
int64 total, yes, no;
int64 r_yes, r_no;
int64 ans;
char str[1200000];
int it = 0;

int main() {
  scanf("%s", str);

  push(0);
  goto t_exp;

c_top:

  ans = r_yes;

  printf("%lld\\n", ans);

  return 0;

t_exp:

  total = 1;
  no = 1;

  while (true) {

    push(total);
    push(no);
    push(1);
    goto t_term;
  c_exp:
    no = pop();
    total = pop();

    total = (total * (r_yes + r_no)) % mod;
    no = (no * r_no) % mod;

    if (str[it] != '|')
      break;
    it += 1;
  }

  r_yes = (total + mod - no) % mod;
  r_no = no;
  goto ret;

t_term:

  total = 1;
  yes = 1;

  while (true) {

    push(total);
    push(yes);
    push(2);
    goto t_fac;
  c_term:
    yes = pop();
    total = pop();

    total = (total * (r_yes + r_no)) % mod;
    yes = (yes * r_yes) % mod;

    if (str[it] != '&')
      break;
    it += 1;
  }

  r_yes = yes;
  r_no = (total + mod - yes) % mod;
  goto ret;

t_fac:

  if (str[it] == 'x') {
    it += 1;
    while (str[it] >= '0' && str[it] <= '9')
      it += 1;
    r_yes = 1;
    r_no = 1;
    goto ret;
  }

  if (str[it] == '~') {
    it += 1;
    push(3);
    goto t_fac;
  c_fac1:
    temp = r_yes;
    r_yes = r_no;
    r_no = temp;
    goto ret;
  }

  if (str[it] == '(') {
    it += 1;
    push(4);
    goto t_exp;
  c_fac2:
    it += 1;
    goto ret;
  }

ret:

  temp = pop();
  if (temp == 0)
    goto c_top;
  if (temp == 1)
    goto c_exp;
  if (temp == 2)
    goto c_term;
  if (temp == 3)
    goto c_fac1;
  if (temp == 4)
    goto c_fac2;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cassert>
#include <cstdio>
using namespace std;

typedef long long int64;
const int64 mod = 1000000007LL;
typedef pair<int64, int64> llp;

llp expression(const char *str, int &it);
llp term(const char *str, int &it);
llp factor(const char *str, int &it);

llp expression(const char *str, int &it) {
  int64 total = 1;
  int64 no = 1;

  while (true) {

    auto res = term(str, it);
    int64 yn = (res.first + res.second) % mod;

    total = (total * yn) % mod;
    no = (no * res.second) % mod;

    if (str[it] != '|')
      break;
    it += 1;
  }

  return make_pair((total + mod - no) % mod, no);
}

llp term(const char *str, int &it) {
  int64 total = 1;
  int64 yes = 1;

  while (true) {

    auto res = factor(str, it);
    int64 yn = (res.first + res.second) % mod;

    total = (total * yn) % mod;
    yes = (yes * res.first) % mod;

    if (str[it] != '&')
      break;
    it += 1;
  }

  return make_pair(yes, (total + mod - yes) % mod);
}

llp factor(const char *str, int &it) {
  if (str[it] == 'x') {
    it += 1;
    while (str[it] >= '0' && str[it] <= '9')
      it += 1;
    return make_pair(1, 1);
  }

  if (str[it] == '~') {
    it += 1;
    auto res = factor(str, it);
    return make_pair(res.second, res.first);
  }

  if (str[it] == '(') {
    it += 1;
    auto res = expression(str, it);
    assert(str[it] == ')');
    it += 1;
    return res;
  }

  assert(false);
  return make_pair(-1, -1);
}

int main() {
  char str[1200000];
  int it = 0;

  scanf("%s", str);

  auto res = expression(str, it);

  printf("%lld\\n", res.first);

  return 0;
}