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

#include <algorithm>
#include <cassert>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>

#define rep(i, n) for (int i = 0; i < (int)n; i++)
#define all(c) (c).begin(), (c).end()
#define mp make_pair
#define pb push_back
#define each(i, c)                                                             \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); i++)
#define dbg(x) cerr << __LINE__ << ": " << #x << " = " << (x) << endl

using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> pi;
const int inf = (int)1e9;
const double INF = 1e12, EPS = 1e-9;

const int mod = 2011;
int pow(int n, int m) {
  int res = 1;
  for (; m; m /= 2) {
    if (m & 1)
      res = res * n % mod;
    n = n * n % mod;
  }
  return res;
}
int inv(int n) { return pow(n, mod - 2); }

int n;
string in[20];
int expr(int &, int &, int &, int &);

int f(int y, int x, int Y, int X) {
  int res = -1;
  for (int i = y; i < Y; i++)
    if (in[i][x] != '.') {
      res = res == -1 ? i : -2;
    }
  return res;
}
int powexpr(int &y, int &x, int &Y, int &X) {
  int p, res;
  while (x < X && (p = f(y, x, Y, X)) < 0)
    x++;
  if (in[p][x] == '(') {
    x++;
    res = expr(y, x, Y, X);
    x++;
  } else {
    res = 0;
    while (x < X && isdigit(in[p][x]))
      res *= 10, res += in[p][x++] - '0';
  }
  if (x < X) {
    p = f(y, x, Y, X);
    if (p >= 0) {
      int e = 0;
      while (x < X && isdigit(in[p][x]))
        e *= 10, e += in[p][x++] - '0';
      res = pow(res, e);
    }
  }
  return res;
}
int factor(int &y, int &x, int &Y, int &X) {
  int p;
  bool sign = 0;
  while (1) {
    while (x < X && (p = f(y, x, Y, X)) < 0)
      x++;
    if (x + 1 < X && in[p][x] == '-' && in[p][x + 1] == '.')
      sign ^= 1, x += 2;
    else
      break;
  }
  if (x + 1 < X && in[p][x] == '-' && in[p][x + 1] == '-') {
    int q = x;
    while (q < X && in[p][q] == '-')
      q++;

    int a = y, b = x + 1, c = p, d = q - 1;
    int num = expr(a, b, c, d);

    a = p + 1, b = x + 1, c = Y, d = q - 1;
    int den = expr(a, b, c, d);

    num = num * inv(den) % mod;
    x = q + 1;
    return sign ? (mod - num) % mod : num;
  }
  return ((sign ? -1 : 1) * powexpr(y, x, Y, X) % mod + mod) % mod;
}
int term(int &y, int &x, int &Y, int &X) {
  int res = inf, p;
  while (1) {
    while (x < X && (p = f(y, x, Y, X)) < 0)
      x++;
    int a = factor(y, x, Y, X);
    res = res == inf ? a : (a * res) % mod;
    p = 0;
    while (x < X && (p = f(y, x, Y, X)) < 0)
      x++;
    if (p < 0 || x >= X || in[p][x] != '*')
      break;
    x++;
  }
  return res;
}
int expr(int &y, int &x, int &Y, int &X) {
  int res = 0, p, op = 0;
  while (1) {
    while (x < X && (p = f(y, x, Y, X)) < 0)
      x++;
    int a = term(y, x, Y, X);
    (res += op ? mod - a : a) %= mod;
    p = 0;
    while (x < X && (p = f(y, x, Y, X)) < 0)
      x++;
    if (p < 0 || x >= X || in[p][x] != '+' && in[p][x] != '-')
      break;
    op = in[p][x] == '-';
    x++;
  }
  return res;
}

int main() {
  while (cin >> n, n) {
    rep(i, n) cin >> in[i];
    int y = 0, x = 0, Y = n, X = in[0].size();
    cout << expr(y, x, Y, X) << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cassert>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>

#define rep(i, n) for (int i = 0; i < (int)n; i++)
#define all(c) (c).begin(), (c).end()
#define mp make_pair
#define pb push_back
#define each(i, c)                                                             \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); i++)
#define dbg(x) cerr << __LINE__ << ": " << #x << " = " << (x) << endl

using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> pi;
const int inf = (int)1e9;
const double INF = 1e12, EPS = 1e-9;

const int mod = 2011;
int pow(int n, int m) {
  int res = 1;
  for (; m; m /= 2) {
    if (m & 1)
      res = res * n % mod;
    n = n * n % mod;
  }
  return res;
}
int inv(int n) { return pow(n, mod - 2); }

int n;
string in[20];
int expr(int &, int &, int &, int &);

int f(int y, int x, int Y, int X) {
  int res = -1;
  for (int i = y; i < Y; i++)
    if (in[i][x] != '.') {
      res = res == -1 ? i : -2;
    }
  return res;
}
int powexpr(int &y, int &x, int &Y, int &X) {
  int p, res;
  while (x < X && (p = f(y, x, Y, X)) < 0)
    x++;
  if (in[p][x] == '(') {
    x++;
    res = expr(y, x, Y, X);
    while (x < X && (p = f(y, x, Y, X)) < 0)
      x++;
    assert(in[p][x] == ')');
    x++;
  } else {
    res = 0;
    while (x < X && isdigit(in[p][x]))
      res *= 10, res += in[p][x++] - '0';
  }
  if (x < X) {
    p = f(y, x, Y, X);
    if (p >= 0) {
      int e = 0;
      while (x < X && isdigit(in[p][x]))
        e *= 10, e += in[p][x++] - '0';
      res = pow(res, e);
    }
  }
  return res;
}
int factor(int &y, int &x, int &Y, int &X) {
  int p;
  bool sign = 0;
  while (1) {
    while (x < X && (p = f(y, x, Y, X)) < 0)
      x++;
    if (in[p][x] == '-' && in[p][x + 1] == '.')
      sign ^= 1, x += 2;
    else
      break;
  }
  if (in[p][x] == '-' && in[p][x + 1] == '-') {
    int q = x;
    while (q < X && in[p][q] == '-')
      q++;

    int a = y, b = x + 1, c = p, d = q - 1;
    int num = expr(a, b, c, d);

    a = p + 1, b = x + 1, c = Y, d = q - 1;
    int den = expr(a, b, c, d);

    num = num * inv(den) % mod;
    x = q + 1;
    return sign ? (mod - num) % mod : num;
  }
  return ((sign ? -1 : 1) * powexpr(y, x, Y, X) % mod + mod) % mod;
}
int term(int &y, int &x, int &Y, int &X) {
  int res = inf, p;
  while (1) {
    while (x < X && (p = f(y, x, Y, X)) < 0)
      x++;
    int a = factor(y, x, Y, X);
    res = res == inf ? a : (a * res) % mod;
    p = 0;
    while (x < X && (p = f(y, x, Y, X)) < 0)
      x++;
    if (p < 0 || in[p][x] != '*')
      break;
    x++;
  }
  return res;
}
int expr(int &y, int &x, int &Y, int &X) {
  int res = 0, p, op = 0;
  while (1) {
    while (x < X && (p = f(y, x, Y, X)) < 0)
      x++;
    int a = term(y, x, Y, X);
    (res += op ? mod - a : a) %= mod;
    p = 0;
    while (x < X && (p = f(y, x, Y, X)) < 0)
      x++;
    if (p < 0 || in[p][x] != '+' && in[p][x] != '-')
      break;
    op = in[p][x] == '-';
    x++;
  }
  return res;
}

int main() {
  while (cin >> n, n) {
    rep(i, n) cin >> in[i];
    int y = 0, x = 0, Y = n, X = in[0].size();
    cout << expr(y, x, Y, X) << endl;
  }
  return 0;
}