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

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
constexpr ll MOD = 1000000007;
class SyntaxAnalysis {
  using T = ll;

public:
  SyntaxAnalysis(const string &S) : S{S} {}
  T Expression() { return pos = 0, expression(); }

private:
  static constexpr char PLUS = '+';
  static constexpr char MINUS = '-';
  static constexpr char PRODUCT = '%';
  static constexpr char DIVIDE = '/';
  static constexpr char BRACE = '<';
  static T plus(const ll n, const ll k) { return n + k; }
  static T minus(const ll n, const ll k) { return n - k; }
  static T product(const ll n, const ll k) { return k >= 64 ? 0LL : n >> k; }
  static T divide(const ll n, const ll k) { return n / k; }
  static T brace(const ll n) { return n * n % MOD; }
  static bool is_digit(const char c) { return '0' <= c and c <= '9'; }
  static T encode(const char c) { return c - '0'; }
  ll number() {
    ll ans = 0;
    for (; pos < S.size() and is_digit(S[pos]); pos++) {
      ans = ans * 10LL + encode(S[pos]);
    }
    return ans;
  }
  ll factor() {
    if (S[pos] == BRACE) {
      ll ans = 0;
      return pos++, ans = brace(expression()), pos++, ans;
    } else {
      return number();
    }
  }
  ll term() {
    ll ans = factor();
    for (; pos < S.size();) {
      if (S[pos] == PRODUCT) {
        pos++, ans = product(ans, factor());
      } else if (S[pos] == DIVIDE) {
        pos++, ans = divide(ans, factor());
      } else {
        break;
      }
    }
    return ans;
  }
  ll expression() {
    ll ans = term();
    for (ll f = 0; pos < S.size();) {
      if (S[pos] == PLUS) {
        pos++, f = term(), ans = plus(ans, f);
      } else if (S[pos] == MINUS) {
        pos++, f = term(), ans = minus(ans, f);
      } else {
        break;
      }
    }
    return ans;
  }
  int pos = 0;
  string S;
};

int main() {
  while (true) {
    auto input = []() {
      string s;
      getline(cin, s);
      string ans;
      for (ll i = s.size() - 1, st = 0, v = false; i >= 0; i--) {
        const char c = s[i];
        if (isdigit(c)) {
          ans.push_back(c), v = true;
        }
        if (c == '>') {
          if (v) {
            ans.push_back('%'), v = false, i--;
          } else {
            ans.push_back('>'), st++;
          }
        }
        if (c == '<') {
          ans.push_back('<'), v = true, st--;
        }
      }
      reverse(ans.begin(), ans.end());
      return ans;
    };
    const string S = input();
    if (S.empty()) {
      break;
    }
    SyntaxAnalysis sy(S);
    cout << sy.Expression() << endl;
  }
  return 0;
}


Error: 0
S = <<12%2>>
S = 123%1%1
S = 1000000000%129
S = <<<<<2>>>>>
S = <<<2013>%11>%10>
S = 


Fixed submission:

#include <bits/stdc++.h>
#define show(x) cerr << #x << " = " << (x) << endl
using namespace std;
using ll = long long;
constexpr ll MOD = 1000000007;
class SyntaxAnalysis {
  using T = ll;

public:
  SyntaxAnalysis(const string &S) : S{S} {}
  T Expression() { return pos = 0, expression(); }

private:
  static constexpr char PLUS = '+';
  static constexpr char MINUS = '-';
  static constexpr char PRODUCT = '%';
  static constexpr char DIVIDE = '/';
  static constexpr char BRACE = '<';
  static T plus(const ll n, const ll k) { return n + k; }
  static T minus(const ll n, const ll k) { return n - k; }
  static T product(const ll n, const ll k) { return k >= 64 ? 0LL : n >> k; }
  static T divide(const ll n, const ll k) { return n / k; }
  static T brace(const ll n) { return n * n % MOD; }
  static bool is_digit(const char c) { return '0' <= c and c <= '9'; }
  static T encode(const char c) { return c - '0'; }
  ll number() {
    ll ans = 0;
    for (; pos < S.size() and is_digit(S[pos]); pos++) {
      ans = ans * 10LL + encode(S[pos]);
    }
    return ans;
  }
  ll factor() {
    if (S[pos] == BRACE) {
      ll ans = 0;
      return pos++, ans = brace(expression()), pos++, ans;
    } else {
      return number();
    }
  }
  ll term() {
    ll ans = factor();
    for (; pos < S.size();) {
      if (S[pos] == PRODUCT) {
        pos++, ans = product(ans, factor());
      } else if (S[pos] == DIVIDE) {
        pos++, ans = divide(ans, factor());
      } else {
        break;
      }
    }
    return ans;
  }
  ll expression() {
    ll ans = term();
    for (ll f = 0; pos < S.size();) {
      if (S[pos] == PLUS) {
        pos++, f = term(), ans = plus(ans, f);
      } else if (S[pos] == MINUS) {
        pos++, f = term(), ans = minus(ans, f);
      } else {
        break;
      }
    }
    return ans;
  }
  int pos = 0;
  string S;
};

int main() {
  while (true) {
    auto input = []() {
      string s;
      getline(cin, s);
      string ans;
      for (ll i = s.size() - 1, st = 0, v = false; i >= 0; i--) {
        const char c = s[i];
        if (isdigit(c)) {
          ans.push_back(c), v = true;
        }
        if (c == '>') {
          if (v) {
            ans.push_back('%'), v = false, i--;
          } else {
            ans.push_back('>'), st++;
          }
        }
        if (c == '<') {
          ans.push_back('<'), v = true, st--;
        }
      }
      reverse(ans.begin(), ans.end());
      return ans;
    };
    const string S = input();
    show(S);
    if (S.empty()) {
      break;
    }
    SyntaxAnalysis sy(S);
    cout << sy.Expression() << endl;
  }
  return 0;
}