Language: C++

#error ""

#include <cstdio>
#include <cstdint>
#include <cassert>
#include <cctype>
#include <algorithm>
#include <vector>
#include <string>

constexpr intmax_t MOD=2011;

intmax_t apply(intmax_t lhs, char op, intmax_t rhs) {
  switch (op) {
  case '+': return (lhs + rhs) % MOD;
  case '-': return ((lhs - rhs) % MOD + MOD) % MOD;
  case '*': return (lhs * rhs) % MOD;
  }
  assert(false);
}

intmax_t modpow(intmax_t base, intmax_t iexp) {
  fprintf(stderr, "%jd^%jd\n", base, iexp);
  intmax_t res=1;
  for (intmax_t dbl=base; iexp; iexp>>=1) {
    if (iexp & 1) (res *= dbl) %= MOD;
    (dbl *= dbl) %= MOD;
  }
  return res;
}

intmax_t moddiv(intmax_t num, intmax_t den) {
  intmax_t div=modpow(den, MOD-2);
  return num*div % MOD;
}

intmax_t parse(const std::vector<std::string> &s, size_t &i,
               size_t top, size_t bottom, size_t left, size_t right,
               size_t preced=0, size_t base=-1,
               const std::vector<std::string> &ops={"+-", "*"}) {

  while (base+1 == 0) {
    for (size_t r=top; r<bottom; ++r) {
      if (s[r][i] != '.') {
        base = r;
        break;
      }
    }
    if (base+1 == 0) ++i;
  }
  assert(base+1 != 0);
  fprintf(stderr, "s[%zu]: LR[%zu, %zu), TB[%zu, %zu), base:%zu, prec:%zu\n",
          i, left, right, top, bottom, base, preced);

  if (preced == ops.size()) {
    intmax_t res=114514;
    if (s[base][i] == '(') {
      res = parse(s, i+=2, top, bottom, left, right, 0, base);
      assert(s[base][i] == ')');
      ++i;
    } else if (s[base][i] == '-') {
      if (s[base][i+1] == '-') {
        size_t vl=i, vr=i+1;
        while (s[base][++vr] == '-') {}

        size_t j=left+1;
        intmax_t numer=parse(s, j, top, base, vl, vr, 0);
        j = left+1;
        intmax_t denom=parse(s, j, base+1, bottom, vl, vr, 0);
        res = moddiv(numer, denom);
        i = vr+1;
      } else {
        res = -parse(s, i+=2, top, bottom, left, right, preced, base);
      }
    } else if (isdigit(s[base][i])) {
      res = s[base][i]-'0';
      ++i;
    }

    if (i+1 < right && top+1 <= base && isdigit(s[base-1][i+1])) {
      fprintf(stderr, "s[%zu][%zu]: %c\n", base-1, i+1, s[base-1][i+1]);
      fprintf(stderr, "%jd^%jd\n", res, intmax_t(s[base-1][i+1]-'0'));
      res = modpow(res, s[base-1][i+1]-'0');
      ++i;
    }
    ++i;
    fprintf(stderr, "Returns %jd\n", res);
    return res;
  }

  intmax_t lhs=parse(s, i, top, bottom, left, right, preced+1, base);
  while (i < right) {
    char op=s[base][i];
    if (!std::count(ops[preced].begin(), ops[preced].end(), op)) break;
    intmax_t rhs=parse(s, i+=2, top, bottom, left, right, preced+1, base);
    lhs = apply(lhs, op, rhs);
  }
  return lhs;
}

int testcase_ends() {
  size_t n;
  scanf("%zu", &n);
  if (n == 0) return 1;

  std::vector<std::string> s;
  for (size_t i=0; i<n; ++i) {
    char buf[128];
    scanf("%s", buf);
    s.emplace_back(buf);
  }

  size_t i=0;
  intmax_t res=parse(s, i, 0, s.size(), 0, s[0].length());
  printf("%jd\n", res);
  return 0;
}

int main() {
  while (!testcase_ends()) {}
}

s[0]: LR[0, 23), TB[0, 4), base:1, prec:0
s[0]: LR[0, 23), TB[0, 4), base:1, prec:1
s[0]: LR[0, 23), TB[0, 4), base:1, prec:2
s[2]: LR[0, 23), TB[0, 4), base:1, prec:0
s[2]: LR[0, 23), TB[0, 4), base:1, prec:1
s[2]: LR[0, 23), TB[0, 4), base:1, prec:2
Returns 1
s[6]: LR[0, 23), TB[0, 4), base:1, prec:1
s[6]: LR[0, 23), TB[0, 4), base:1, prec:2
s[8]: LR[6, 10), TB[0, 1), base:0, prec:0
s[8]: LR[6, 10), TB[0, 1), base:0, prec:1
s[8]: LR[6, 10), TB[0, 1), base:0, prec:2
Returns 4
s[7]: LR[6, 10), TB[2, 4), base:3, prec:0
s[7]: LR[6, 10), TB[2, 4), base:3, prec:1
s[7]: LR[6, 10), TB[2, 4), base:3, prec:2
Returns 3
3^2009
s[0][12]: 2
1342^2: L80
1342^2
Returns 1119
a.out: D.cpp:57: intmax_t parse(const std::__debug::vector<std::__cxx11::basic_string<char> >&, size_t&, size_t, size_t, size_t, size_t, size_t, size_t, const std::__debug::vector<std::__cxx11::basic_string<char> >&): Assertion `s[base][i] == ')'' failed.
Aborted (core dumped)



Compiler output:
a.cc:1:2: error: #error ""
    1 | #error ""
      |  ^~~~~
a.cc:145:229: error: stray '`' in program
  145 | a.out: D.cpp:57: intmax_t parse(const std::__debug::vector<std::__cxx11::basic_string<char> >&, size_t&, size_t, size_t, size_t, size_t, size_t, size_t, const std::__debug::vector<std::__cxx11::basic_string<char> >&): Assertion `s[base][i] == ')'' failed.
      |                                                                                                                                                                                                                                     ^
a.cc:145:247: warning: missing terminating ' character
  145 | a.out: D.cpp:57: intmax_t parse(const std::__debug::vector<std::__cxx11::basic_string<char> >&, size_t&, size_t, size_t, size_t, size_t, size_t, size_t, const std::__debug::vector<std::__cxx11::basic_string<char> >&): Assertion `s[base][i] == ')'' failed.
      |                                                                                                                                                                                                                                                       ^
a.cc:145:247: error: missing terminating ' character
  145 | a.out: D.cpp:57: intmax_t parse(const std::__debug::vector<std::__cxx11::basic_string<char> >&, size_t&, size_t, size_t, size_t, size_t, size_t, size_t, const std::__debug::vector<std::__cxx11::basic_string<char> >&): Assertion `s[base][i] == ')'' failed.
      |                                                                                                                                                                                                                                                       ^~~~~~~~~
a.cc:123:1: error: 's' does not name a type
  123 | s[0]: LR[0, 23), TB[0, 4), base:1, prec:0
      | ^