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

#include <bits/stdc++.h>
using namespace std;
template <typename T1, typename T2> inline void chmin(T1 &a, T2 b) {
  if (a > b)
    a = b;
}
template <typename T1, typename T2> inline void chmax(T1 &a, T2 b) {
  if (a < b)
    a = b;
}
using Int = long long;
const char newl = '\\n';

template <typename T, T MOD = 1000000007> struct Mint {
  static constexpr T mod = MOD;
  T v;
  Mint() : v(0) {}
  Mint(signed v) : v(v) {}
  Mint(long long t) {
    v = t % MOD;
    if (v < 0)
      v += MOD;
  }

  Mint pow(long long k) {
    Mint res(1), tmp(v);
    while (k) {
      if (k & 1)
        res *= tmp;
      tmp *= tmp;
      k >>= 1;
    }
    return res;
  }

  static Mint add_identity() { return Mint(0); }
  static Mint mul_identity() { return Mint(1); }

  Mint inv() { return pow(MOD - 2); }

  Mint &operator+=(Mint a) {
    v += a.v;
    if (v >= MOD)
      v -= MOD;
    return *this;
  }
  Mint &operator-=(Mint a) {
    v += MOD - a.v;
    if (v >= MOD)
      v -= MOD;
    return *this;
  }
  Mint &operator*=(Mint a) {
    v = 1LL * v * a.v % MOD;
    return *this;
  }
  Mint &operator/=(Mint a) { return (*this) *= a.inv(); }

  Mint operator+(Mint a) const { return Mint(v) += a; }
  Mint operator-(Mint a) const { return Mint(v) -= a; }
  Mint operator*(Mint a) const { return Mint(v) *= a; }
  Mint operator/(Mint a) const { return Mint(v) /= a; }

  Mint operator-() const { return v ? Mint(MOD - v) : Mint(v); }

  bool operator==(const Mint a) const { return v == a.v; }
  bool operator!=(const Mint a) const { return v != a.v; }
  bool operator<(const Mint a) const { return v < a.v; }

  static Mint comb(long long n, int k) {
    Mint num(1), dom(1);
    for (int i = 0; i < k; i++) {
      num *= Mint(n - i);
      dom *= Mint(i + 1);
    }
    return num / dom;
  }
};
template <typename T, T MOD> constexpr T Mint<T, MOD>::mod;
template <typename T, T MOD> ostream &operator<<(ostream &os, Mint<T, MOD> m) {
  os << m.v;
  return os;
}

template <typename F> struct FixPoint : F {
  FixPoint(F &&f) : F(forward<F>(f)) {}
  template <typename... Args> decltype(auto) operator()(Args &&...args) const {
    return F::operator()(*this, forward<Args>(args)...);
  }
};
template <typename F> inline decltype(auto) MFP(F &&f) {
  return FixPoint<F>{forward<F>(f)};
}

// INSERT ABOVE HERE

using M = Mint<int>;
const int MAX = 5050;
M po[MAX], op[MAX];

M dfs(int h, int w);

M dp2[MAX][MAX];
int used2[MAX][MAX] = {};
M sum(int h, int w) {
  M &res = dp2[h][w];
  if (used2[h][w])
    return res;
  used2[h][w] = 1;

  res = M(0);
  if (w > 0)
    res += sum(h, w - 1);
  res += dfs(h, w) * op[w + 1] * M(w);
  return res;
}

M dp[MAX][MAX];
int used[MAX][MAX] = {};
M dfs(int h, int w) {
  M &res = dp[h][w];
  if (used[h][w])
    return res;
  used[h][w] = 1;

  assert(h >= 0 and w >= 0);
  if (h == 0 or w == 0)
    return res = M(1);
  if (w == 1)
    return res = po[h];
  if (h == 1)
    return res = po[w];

  res = M(0);
  res += dfs(h - 1, w) * M(w + 1);
  res += sum(h - 1, w - 1) * po[w];
  return res;
};
signed main() {
  cin.tie(0);
  ios::sync_with_stdio(0);

  int n, m;
  cin >> n >> m;
  po[0] = op[0] = M(1);
  for (int i = 0; i + 1 < MAX; i++)
    po[i + 1] = po[i] * M(2);
  for (int i = 0; i + 1 < MAX; i++)
    op[i + 1] = op[i] * M(2).inv();

  cout << dfs(n, m) << newl;
  return 0;
}


Error: -11

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
template <typename T1, typename T2> inline void chmin(T1 &a, T2 b) {
  if (a > b)
    a = b;
}
template <typename T1, typename T2> inline void chmax(T1 &a, T2 b) {
  if (a < b)
    a = b;
}
using Int = long long;
const char newl = '\\n';

template <typename T, T MOD = 1000000007> struct Mint {
  static constexpr T mod = MOD;
  T v;
  Mint() : v(0) {}
  Mint(signed v) : v(v) {}
  Mint(long long t) {
    v = t % MOD;
    if (v < 0)
      v += MOD;
  }

  Mint pow(long long k) {
    Mint res(1), tmp(v);
    while (k) {
      if (k & 1)
        res *= tmp;
      tmp *= tmp;
      k >>= 1;
    }
    return res;
  }

  static Mint add_identity() { return Mint(0); }
  static Mint mul_identity() { return Mint(1); }

  Mint inv() { return pow(MOD - 2); }

  Mint &operator+=(Mint a) {
    v += a.v;
    if (v >= MOD)
      v -= MOD;
    return *this;
  }
  Mint &operator-=(Mint a) {
    v += MOD - a.v;
    if (v >= MOD)
      v -= MOD;
    return *this;
  }
  Mint &operator*=(Mint a) {
    v = 1LL * v * a.v % MOD;
    return *this;
  }
  Mint &operator/=(Mint a) { return (*this) *= a.inv(); }

  Mint operator+(Mint a) const { return Mint(v) += a; }
  Mint operator-(Mint a) const { return Mint(v) -= a; }
  Mint operator*(Mint a) const { return Mint(v) *= a; }
  Mint operator/(Mint a) const { return Mint(v) /= a; }

  Mint operator-() const { return v ? Mint(MOD - v) : Mint(v); }

  bool operator==(const Mint a) const { return v == a.v; }
  bool operator!=(const Mint a) const { return v != a.v; }
  bool operator<(const Mint a) const { return v < a.v; }

  static Mint comb(long long n, int k) {
    Mint num(1), dom(1);
    for (int i = 0; i < k; i++) {
      num *= Mint(n - i);
      dom *= Mint(i + 1);
    }
    return num / dom;
  }
};
template <typename T, T MOD> constexpr T Mint<T, MOD>::mod;
template <typename T, T MOD> ostream &operator<<(ostream &os, Mint<T, MOD> m) {
  os << m.v;
  return os;
}

template <typename F> struct FixPoint : F {
  FixPoint(F &&f) : F(forward<F>(f)) {}
  template <typename... Args> decltype(auto) operator()(Args &&...args) const {
    return F::operator()(*this, forward<Args>(args)...);
  }
};
template <typename F> inline decltype(auto) MFP(F &&f) {
  return FixPoint<F>{forward<F>(f)};
}

// INSERT ABOVE HERE

using M = Mint<int>;
const int MAX = 5050;
M po[MAX], op[MAX];

M dfs(int h, int w);

M dp2[MAX][MAX];
int used2[MAX][MAX] = {};
M sum(int h, int w) {
  M &res = dp2[h][w];
  if (used2[h][w])
    return res;
  used2[h][w] = 1;

  res = M(0);
  if (w > 0)
    res += sum(h, w - 1);
  res += dfs(h, w) * op[w + 1] * M(w);
  return res;
}

M dp[MAX][MAX];
int used[MAX][MAX] = {};
M dfs(int h, int w) {
  // cout<<h<<' '<<w<<endl;
  M &res = dp[h][w];
  if (used[h][w])
    return res;
  used[h][w] = 1;

  assert(h >= 0 and w >= 0);
  if (h == 0 or w == 0)
    return res = M(1);
  if (w == 1)
    return res = po[h];
  if (h == 1)
    return res = po[w];

  res = M(0);

  //  (j)  (w-j)
  // 0...0/1...1

  /*
    for(int j=0;j<=w;j++)
    res+=dfs(h-1,j)*dfs(h-1,w-j);
  */
  res += dfs(h - 1, w) * M(w + 1);

  //  (j) (1)(?)(1)(k)
  // 0...0/1?????0(1*)
  /*
    for(int j=0;j+1<w;j++)
    for(int k=0;j+1+k<w;k++)
    res+=dfs(h-1,j+1+k)*po[w-(j+1+k+1)];
  */

  // j+k -> j
  /*
    for(int j=0;j+1<w;j++)
    res+=dfs(h-1,j+1)*po[w-(j+1+1)]*M(j+1);
  */
  // res+=dfs(h-1,j+1)/po[j+1+1]*M(j+1)*po[w];
  res += sum(h - 1, w - 1) * po[w];
  return res;
};
signed main() {
  cin.tie(0);
  ios::sync_with_stdio(0);

  int n, m;
  cin >> n >> m;
  po[0] = op[0] = M(1);
  for (int i = 0; i < n + m; i++)
    po[i + 1] = po[i] * M(2);
  for (int i = 0; i < n + m; i++)
    op[i + 1] = op[i] * M(2).inv();

  cout << dfs(n, m) << newl;
  return 0;
}