Problem p03748 (C++)

Buggy submission (status: Memory Limit Exceeded):
const long long MOD = 1e9 + 7;
const long long INF = 1e9;
const long long INFLL = 1e18;

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<vector<int>> vvi;
typedef vector<ll> vll;
typedef complex<double> cd;

#define forn(i, n) for (int(i) = 0; (i) != (n); (i)++)
#define all(v) (v).begin(), (v).end()
#define rall(v) (v).rbegin(), (v).rend()
#define popcount(x) __builtin_popcount(x)
#define popcountll(x) __builtin_popcountll(x)
#define fi first
#define se second
#define re return
#define pb push_back
#define uniq(x)                                                                \
  sort(all(x));                                                                \
  (x).resize(unique(all(x)) - (x).begin())

#ifdef LOCAL
#define dbg(x) cerr << __LINE__ << " " << #x << " " << x << endl
#define ln cerr << __LINE__ << endl
#else
#define dbg(x) void(0)
#define ln void(0)
#endif // LOCAL

int cx[4] = {-1, 0, 1, 0};
int cy[4] = {0, -1, 0, 1};
string Yes[2] = {"No\n", "Yes\n"};
string YES[2] = {"NO\n", "YES\n"};
string Possible[2] = {"Impossible\n", "Possible\n"};
string POSSIBLE[2] = {"IMPOSSIBLE\n", "POSSIBLE\n"};

int ok(int x, int n) { return 0 <= x && x < n; }

ll log10(ll x) {
  if (x < 10)
    re 1;
  re 1 + log10(x / 10);
}

ll ds(ll x) {
  if (x < 10)
    return x;
  re x % 10 + ds(x / 10);
}

double sqr(double x) { return x * x; }

bool in(int bit, int mask) { return (mask & (1 << bit)) > 0; }

void Del(vector<int> &v, int pos) {
  swap(v[pos], v[v.size() - 1]);
  v.pop_back();
}

long long g(vector<long long> &p, int pos) {
  if (ok(pos, p.size()))
    return p[pos];
  if (pos < 0 || p.size() == 0)
    return 0;
  return p.back();
}

int g(vector<int> &p, int pos) {
  if (ok(pos, p.size()))
    return p[pos];
  if (pos < 0 || p.size() == 0)
    return 0;
  return p.back();
}

ll inq(ll x, ll y) {
  if (!y)
    re 1 % MOD;
  ll l = inq(x, y / 2);
  if (y % 2)
    re l *l % MOD *x % MOD;
  re l *l % MOD;
}

ll rev(ll x) { return inq(x, MOD - 2); }

int __precomputed_combinatorics = 0;
vector<ll> __fact, __ufact, __rev;

inline void __precompute_combinatorics(int __n) {
  if (__precomputed_combinatorics >= __n)
    return;
  __fact.resize(__n);
  __ufact.resize(__n);
  __rev.resize(__n);
  __rev[1] = 1;
  for (int i = max(2, __precomputed_combinatorics); i < __n; i++)
    __rev[i] = MOD - __rev[MOD % i] * (MOD / i) % MOD;
  __fact[0] = 1, __ufact[0] = 1;
  for (int i = max(1, __precomputed_combinatorics); i < __n; i++)
    __fact[i] = __fact[i - 1] * i % MOD,
    __ufact[i] = __ufact[i - 1] * __rev[i] % MOD;
  __precomputed_combinatorics = __n;
}

ll fact(int x) {
  if (__precomputed_combinatorics <= x)
    __precompute_combinatorics(x + 1);
  return __fact[x];
}

ll cnk(int n, int k) {
  if (k < 0 || k > n)
    return 0;
  if (__precomputed_combinatorics <= n)
    __precompute_combinatorics(n + 1);
  return __fact[n] * __ufact[n - k] % MOD * __ufact[k] % MOD;
}

struct dsu {
  vector<int> root;
  vector<int> sz;

  dsu(int _n) {
    root.resize(_n);
    iota(all(root), 0);
    sz.resize(_n, 1);
  }

  dsu() { dsu(0); }

  void Reset(int _n) {
    root.resize(_n);
    sz.resize(_n);
    for (int i = 0; i < root.size(); i++) {
      root[i] = i;
      sz[i] = 1;
    }
  }

  void Reset() { Reset(root.size()); }

  int Root(int x) {
    if (x == root[x])
      return x;
    return root[x] = Root(root[x]);
  }

  void Merge(int v, int u) {
    v = Root(v), u = Root(u);
    if (v == u)
      return;
    if (sz[v] < sz[u]) {
      sz[u] += sz[v];
      root[v] = u;
    } else {
      sz[v] += sz[u];
      root[u] = v;
    }
  }
};

struct dsu_cut {
  vector<int> root;
  vector<int> sz;
  stack<int> cuts;

  dsu_cut(int _n) {
    root.resize(_n);
    iota(all(root), 0);
    sz.resize(_n, 1);
  }

  dsu_cut() { dsu_cut(0); }

  void Cut() {
    if (!cuts.size())
      return;
    int v = cuts.top();
    sz[root[v]] -= sz[v];
    root[v] = v;
    cuts.pop();
  }

  void Reset(int _n) {
    root.resize(_n);
    sz.resize(_n);
    for (int i = 0; i < root.size(); i++) {
      root[i] = i;
      sz[i] = 1;
    }
  }

  void Reset() {
    if (cuts.size() < root.size()) {
      while (cuts.size()) {
        Cut();
      }
    } else {
      Reset(root.size());
    }
  }

  int Root(int x) {
    if (x == root[x])
      return x;
    return Root(root[x]);
  }

  void Merge(int v, int u) {
    v = Root(v), u = Root(u);
    if (v == u)
      return;
    if (sz[v] < sz[u]) {
      sz[u] += sz[v];
      root[v] = u;
      cuts.push(v);
    } else {
      sz[v] += sz[u];
      root[u] = v;
      cuts.push(u);
    }
  }
};

void bfs(int v, vi &dist, vector<vi> &graph) {
  fill(all(dist), -1);
  dist[v] = 0;
  vi q = {v};
  for (int i = 0; i < q.size(); i++) {
    for (auto u : graph[q[i]]) {
      if (dist[u] == -1) {
        dist[u] = dist[q[i]] + 1;
        q.push_back(u);
      }
    }
  }
}

vector<int> z_func(string &s) {
  // Calculates z-function of string s
  // z[0] = s.size(), s should not be empty
  //
  // Tested on the following problem:
  // https://codeforces.com/edu/course/2/lesson/3/3/practice/contest/272263/problem/A
  //
  vector<int> z(s.size());
  z[0] = s.size();
  int L = 0, R = 0;
  for (int i = 1; i < s.size(); i++) {
    z[i] = max(0, min(z[i - L], R - i));
    while (i + z[i] < s.size() && s[i + z[i]] == s[z[i]])
      z[i]++;
    if (i + z[i] > R) {
      R = i + z[i];
      L = i;
    }
  }
  return z;
}

vector<int> p_func(string &s) {
  // Calculates prefix function of string s
  //
  //
  vector<int> p(s.size());
  for (int i = 1; i < s.size(); i++) {
    int j = p[i - 1];
    while (j > 0 && s[i] != s[j])
      j = p[j - 1];
    if (s[i] == s[j])
      j++;
    p[i] = j;
  }
  return p;
}

vector<int> d1_func(string &s) {
  // Calculates max length of palindrome centered in positions and i for each i
  //
  //
  vector<int> d1(s.size());
  int L = 0, R = -1;
  for (int i = 0; i < s.size(); i++) {
    int k = 0;
    if (i <= R)
      k = min(R - i + 1, d1[R - i + L]);
    while (i + k < s.size() && i - k >= 0 && s[i - k] == s[i + k])
      k++;
    d1[i] = k--;
    if (i + k > R) {
      L = i - k;
      R = i + k;
    }
  }
  return d1;
}

vector<int> d2_func(string &s) {
  // Calculates max length of palindrome centered in positions i - 1 and i for
  // each i
  //
  //
  vector<int> d2(s.size());
  int L = 0, R = -1;
  for (int i = 1; i < s.size(); i++) {
    int k = 0;
    if (i <= R)
      k = min(R - i + 1, d2[R - i + L + 1]);
    while (i + k < s.size() && i - k - 1 >= 0 && s[i - k - 1] == s[i + k])
      k++;
    d2[i] = k--;
    if (i + k > R) {
      L = i - k - 1;
      R = i + k;
    }
  }
  return d2;
}

void add(int pos, ll x, vector<ll> &fenw) {
  // Performs add query on Fenwick tree
  //
  // Tested on the following problem:
  // https://atcoder.jp/contests/practice2/tasks/practice2_b
  //
  while (pos < fenw.size()) {
    fenw[pos] += x;
    pos |= (pos + 1);
  }
}

ll get(int pos, vector<ll> &fenw) {
  // Performs get query on Fenwick tree
  //
  // Tested on the following problem:
  // https://atcoder.jp/contests/practice2/tasks/practice2_b
  //
  ll res = 0;
  while (pos >= 0) {
    res += fenw[pos];
    pos = (pos & (pos + 1)) - 1;
  }
  return res;
}

struct dcp_offine {
  // Given n, m and arrays v, u, t of length m
  // -1 <= t[i] <= 1 and 0 <= v[i], u[i] < n satisfied for t[i] != -1
  // t[i] = 0 asks to change state of edge (v[i], u[i])
  // t[i] = -1 asks to calculate number of connected components
  // t[i] = 1 asks to check if v[i] and u[i] are in the same connected component
  // After creating following holds:
  // ans[i] contains -1 for t[i] = 0 and answer for the i-th query otherwise
  //
  // Correctness t[i] <= 0 tested on the following problem:
  // https://codeforces.com/edu/course/2/lesson/7/3/practice/contest/289392/problem/C
  // Correctness tested on random tests with m = 40000
  // Speed tested on random tests with m = 2e6, average = 0.955ms/test (local)
  //
  int n, m;
  dsu_cut dcp;
  vector<int> ans;
  void solve(int l, int r, vi &v, vi &u, vi &t, vi &L, vi &R) {
    if (l >= r)
      return;
    if (l + 1 == r) {
      if (t[l] == -1) {
        ans[l] = n - dcp.cuts.size();
      }
      if (t[l] == 1) {
        ans[l] = dcp.Root(v[l]) == dcp.Root(u[l]);
      }
      return;
    }
    int m = (l + r) / 2;
    int ss = dcp.cuts.size();
    for (int i = l; i < m; i++) {
      if (R[i] >= r)
        dcp.Merge(v[i], u[i]);
    }
    solve(m, r, v, u, t, L, R);
    while (dcp.cuts.size() > ss)
      dcp.Cut();
    for (int i = r - 1; i >= m; i--) {
      if (L[i] < l)
        dcp.Merge(v[i], u[i]);
    }
    solve(l, m, v, u, t, L, R);
    while (dcp.cuts.size() > ss)
      dcp.Cut();
  }
  dcp_offine(int _n, int _m, vi v, vi u, vi t) {
    n = _n, m = _m;
    dcp.Reset(n);
    ans.resize(m, -1);
    map<pair<int, int>, int> mm;
    vi L(m, INF), R(m, -INF);
    for (int i = 0; i < m; i++) {
      if (t[i] != 0)
        continue;
      if (v[i] > u[i])
        swap(v[i], u[i]);
      if (mm.find({v[i], u[i]}) == mm.end()) {
        mm[{v[i], u[i]}] = i;
      } else {
        int t = mm[{v[i], u[i]}];
        R[t] = i;
        L[i] = t;
        mm.erase({v[i], u[i]});
      }
    }
    while (mm.size()) {
      t.push_back(0);
      ans.push_back(-1);
      L.push_back(INF);
      R.push_back(-INF);
      auto it = mm.begin();
      pair<int, int> s = (*it).first;
      int t = (*it).second;
      R[t] = m;
      L[m] = t;
      v.push_back(s.first);
      u.push_back(s.second);
      mm.erase(mm.begin());
      m++;
    }
    solve(0, m, v, u, t, L, R);
  }
};

template <class T> struct sparse_min {
  // Returns min on segment [l, r] of predefined array a
  //
  // Correctness tested on the following problem:
  // https://codeforces.com/contest/1304/problem/E
  // Speed tested on random tests with n = 4e6, q = 4e6, T = int, average =
  // 0.508ms/test (local) Speed tested on random tests with n = 4e6, q = 4e7, T
  // = int, average = 1.778ms/test (local)
  //
  vector<vector<T>> sp;
  vector<int> H;
  T get(int l, int r) {
    int h = H[r - l + 1];
    return min(sp[h][l], sp[h][r - (1 << h) + 1]);
  }
  sparse_min(vector<T> a) {
    int n = a.size();
    H = vector<int>(n + 1);
    for (int i = 3; i < n + 1; i++) {
      H[i] = H[i - 1] + (((i - 1) & (i - 2)) == 0);
    }
    int s = 0;
    while ((1 << s) < n)
      s++;
    sp = vector<vector<T>>(s, vector<T>(n));
    for (int i = 0; i < n; i++) {
      sp[0][i] = a[i];
    }
    for (int j = 1; j < s; j++) {
      for (int i = 0; i + (1 << j) - 1 < n; i++) {
        sp[j][i] = get(i, i + (1 << j) - 1);
      }
    }
  }
};

template <class T> struct sparse_max {
  // Returns min on segment [l, r] of predefined array a
  //
  // Correctness tested on the following problem:
  // https://codeforces.com/contest/1304/problem/E
  // Speed tested on random tests with n = 4e6, q = 4e6, T = int, average =
  // 0.508ms/test (local) Speed tested on random tests with n = 4e6, q = 4e7, T
  // = int, average = 1.778ms/test (local)
  //
  vector<vector<T>> sp;
  vector<int> H;
  T get(int l, int r) {
    int h = H[r - l + 1];
    return max(sp[h][l], sp[h][r - (1 << h) + 1]);
  }
  sparse_max(vector<T> a) {
    int n = a.size();
    H = vector<int>(n + 1);
    for (int i = 3; i < n + 1; i++) {
      H[i] = H[i - 1] + (((i - 1) & (i - 2)) == 0);
    }
    int s = 0;
    while ((1 << s) < n)
      s++;
    sp = vector<vector<T>>(s, vector<T>(n));
    for (int i = 0; i < n; i++) {
      sp[0][i] = a[i];
    }
    for (int j = 1; j < s; j++) {
      for (int i = 0; i + (1 << j) - 1 < n; i++) {
        sp[j][i] = get(i, i + (1 << j) - 1);
      }
    }
  }
};

struct scc_graph {
  // Find Strongly Connected Components of given graph
  //
  // Correctness tested on the following problem:
  // https://atcoder.jp/contests/arc069/tasks/arc069_d
  //
  int n;
  vector<vector<int>> graph;
  vector<vector<int>> inv_graph;
  vector<int> fr, to;
  vector<int> fn;
  vector<int> vis;
  vector<int> component;
  int col;
  void inv_dfs(int v) {
    vis[v] = 1;
    for (auto id : inv_graph[v]) {
      int u = fr[id];
      if (!vis[u]) {
        inv_dfs(u);
      }
    }
    fn.push_back(v);
  }
  void dfs(int v) {
    component[v] = col;
    for (auto id : graph[v]) {
      int u = to[id];
      if (component[u] == -1) {
        dfs(u);
      }
    }
  }
  scc_graph(int _n) {
    n = _n;
    graph = vector<vector<int>>(n);
    inv_graph = vector<vector<int>>(n);
  }
  void add_edge(int v, int u) {
    graph[v].push_back(fr.size());
    inv_graph[u].push_back(fr.size());
    fr.push_back(v);
    to.push_back(u);
  }
  vector<vector<int>> scc() {
    vis = vector<int>(n);
    component = vector<int>(n, -1);
    fn = {};
    col = 0;
    for (int i = 0; i < n; i++) {
      if (!vis[i]) {
        inv_dfs(i);
      }
    }
    reverse(fn.begin(), fn.end());
    for (auto i : fn) {
      if (component[i] == -1) {
        dfs(i);
        col++;
      }
    }
    vector<vector<int>> res(col);
    for (int i = 0; i < n; i++) {
      res[component[i]].push_back(i);
    }
    return res;
  }
};

const int N = 3010;

long long dp[N][N][4];

signed main() {
  srand(time(NULL));
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);

  int n, m;
  cin >> n >> m;
  n--, m--;
  dp[0][0][1] = 1;
  dp[0][n][2] = 1;
  for (int p = 1; p < n; p++)
    dp[0][p][0] = 1;
  for (int i = 0; i < m; i++) {
    for (int j = 0; j <= n; j++) {
      for (auto e : {j - 1, j + 1}) {
        if (0 <= e && e <= n) {
          for (int mask = 0; mask < 4; mask++) {
            int nm = mask;
            if (e == 0)
              nm |= 1;
            if (e == n)
              nm |= 2;
            dp[i + 1][e][nm] = (dp[i + 1][e][nm] + dp[i][j][mask]) % MOD;
          }
        }
      }
    }
  }
  long long ans = 0;
  long long rev2 = (MOD + 1) / 2;
  for (int sel = 0; sel <= m; sel++) {
    long long si = 0;
    for (int i = 0; i <= n; i++) {
      si = (si + dp[sel][i][1] * rev2 + dp[sel][i][2] * rev2 + dp[sel][i][3]) %
           MOD;
    }
    si = si * cnk(m, sel) % MOD;
    si = si * inq(2, m - sel) % MOD;
    ans = (ans + si) % MOD;
  }
  cout << ans * 4 % MOD;
}

/* Note:
Check constants at the beginning of the code (MOD, INF, INFLL)
Check corner cases.
    N = 1
No def int long long for now.
Add something here.
*/


Fixed submission:
const long long MOD = 1e9 + 7;
const long long INF = 1e9;
const long long INFLL = 1e18;

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<vector<int>> vvi;
typedef vector<ll> vll;
typedef complex<double> cd;

#define forn(i, n) for (int(i) = 0; (i) != (n); (i)++)
#define all(v) (v).begin(), (v).end()
#define rall(v) (v).rbegin(), (v).rend()
#define popcount(x) __builtin_popcount(x)
#define popcountll(x) __builtin_popcountll(x)
#define fi first
#define se second
#define re return
#define pb push_back
#define uniq(x)                                                                \
  sort(all(x));                                                                \
  (x).resize(unique(all(x)) - (x).begin())

#ifdef LOCAL
#define dbg(x) cerr << __LINE__ << " " << #x << " " << x << endl
#define ln cerr << __LINE__ << endl
#else
#define dbg(x) void(0)
#define ln void(0)
#endif // LOCAL

int cx[4] = {-1, 0, 1, 0};
int cy[4] = {0, -1, 0, 1};
string Yes[2] = {"No\n", "Yes\n"};
string YES[2] = {"NO\n", "YES\n"};
string Possible[2] = {"Impossible\n", "Possible\n"};
string POSSIBLE[2] = {"IMPOSSIBLE\n", "POSSIBLE\n"};

int ok(int x, int n) { return 0 <= x && x < n; }

ll log10(ll x) {
  if (x < 10)
    re 1;
  re 1 + log10(x / 10);
}

ll ds(ll x) {
  if (x < 10)
    return x;
  re x % 10 + ds(x / 10);
}

double sqr(double x) { return x * x; }

bool in(int bit, int mask) { return (mask & (1 << bit)) > 0; }

void Del(vector<int> &v, int pos) {
  swap(v[pos], v[v.size() - 1]);
  v.pop_back();
}

long long g(vector<long long> &p, int pos) {
  if (ok(pos, p.size()))
    return p[pos];
  if (pos < 0 || p.size() == 0)
    return 0;
  return p.back();
}

int g(vector<int> &p, int pos) {
  if (ok(pos, p.size()))
    return p[pos];
  if (pos < 0 || p.size() == 0)
    return 0;
  return p.back();
}

ll inq(ll x, ll y) {
  if (!y)
    re 1 % MOD;
  ll l = inq(x, y / 2);
  if (y % 2)
    re l *l % MOD *x % MOD;
  re l *l % MOD;
}

ll rev(ll x) { return inq(x, MOD - 2); }

int __precomputed_combinatorics = 0;
vector<ll> __fact, __ufact, __rev;

inline void __precompute_combinatorics(int __n) {
  if (__precomputed_combinatorics >= __n)
    return;
  __fact.resize(__n);
  __ufact.resize(__n);
  __rev.resize(__n);
  __rev[1] = 1;
  for (int i = max(2, __precomputed_combinatorics); i < __n; i++)
    __rev[i] = MOD - __rev[MOD % i] * (MOD / i) % MOD;
  __fact[0] = 1, __ufact[0] = 1;
  for (int i = max(1, __precomputed_combinatorics); i < __n; i++)
    __fact[i] = __fact[i - 1] * i % MOD,
    __ufact[i] = __ufact[i - 1] * __rev[i] % MOD;
  __precomputed_combinatorics = __n;
}

ll fact(int x) {
  if (__precomputed_combinatorics <= x)
    __precompute_combinatorics(x + 1);
  return __fact[x];
}

ll cnk(int n, int k) {
  if (k < 0 || k > n)
    return 0;
  if (__precomputed_combinatorics <= n)
    __precompute_combinatorics(n + 1);
  return __fact[n] * __ufact[n - k] % MOD * __ufact[k] % MOD;
}

struct dsu {
  vector<int> root;
  vector<int> sz;

  dsu(int _n) {
    root.resize(_n);
    iota(all(root), 0);
    sz.resize(_n, 1);
  }

  dsu() { dsu(0); }

  void Reset(int _n) {
    root.resize(_n);
    sz.resize(_n);
    for (int i = 0; i < root.size(); i++) {
      root[i] = i;
      sz[i] = 1;
    }
  }

  void Reset() { Reset(root.size()); }

  int Root(int x) {
    if (x == root[x])
      return x;
    return root[x] = Root(root[x]);
  }

  void Merge(int v, int u) {
    v = Root(v), u = Root(u);
    if (v == u)
      return;
    if (sz[v] < sz[u]) {
      sz[u] += sz[v];
      root[v] = u;
    } else {
      sz[v] += sz[u];
      root[u] = v;
    }
  }
};

struct dsu_cut {
  vector<int> root;
  vector<int> sz;
  stack<int> cuts;

  dsu_cut(int _n) {
    root.resize(_n);
    iota(all(root), 0);
    sz.resize(_n, 1);
  }

  dsu_cut() { dsu_cut(0); }

  void Cut() {
    if (!cuts.size())
      return;
    int v = cuts.top();
    sz[root[v]] -= sz[v];
    root[v] = v;
    cuts.pop();
  }

  void Reset(int _n) {
    root.resize(_n);
    sz.resize(_n);
    for (int i = 0; i < root.size(); i++) {
      root[i] = i;
      sz[i] = 1;
    }
  }

  void Reset() {
    if (cuts.size() < root.size()) {
      while (cuts.size()) {
        Cut();
      }
    } else {
      Reset(root.size());
    }
  }

  int Root(int x) {
    if (x == root[x])
      return x;
    return Root(root[x]);
  }

  void Merge(int v, int u) {
    v = Root(v), u = Root(u);
    if (v == u)
      return;
    if (sz[v] < sz[u]) {
      sz[u] += sz[v];
      root[v] = u;
      cuts.push(v);
    } else {
      sz[v] += sz[u];
      root[u] = v;
      cuts.push(u);
    }
  }
};

void bfs(int v, vi &dist, vector<vi> &graph) {
  fill(all(dist), -1);
  dist[v] = 0;
  vi q = {v};
  for (int i = 0; i < q.size(); i++) {
    for (auto u : graph[q[i]]) {
      if (dist[u] == -1) {
        dist[u] = dist[q[i]] + 1;
        q.push_back(u);
      }
    }
  }
}

vector<int> z_func(string &s) {
  // Calculates z-function of string s
  // z[0] = s.size(), s should not be empty
  //
  // Tested on the following problem:
  // https://codeforces.com/edu/course/2/lesson/3/3/practice/contest/272263/problem/A
  //
  vector<int> z(s.size());
  z[0] = s.size();
  int L = 0, R = 0;
  for (int i = 1; i < s.size(); i++) {
    z[i] = max(0, min(z[i - L], R - i));
    while (i + z[i] < s.size() && s[i + z[i]] == s[z[i]])
      z[i]++;
    if (i + z[i] > R) {
      R = i + z[i];
      L = i;
    }
  }
  return z;
}

vector<int> p_func(string &s) {
  // Calculates prefix function of string s
  //
  //
  vector<int> p(s.size());
  for (int i = 1; i < s.size(); i++) {
    int j = p[i - 1];
    while (j > 0 && s[i] != s[j])
      j = p[j - 1];
    if (s[i] == s[j])
      j++;
    p[i] = j;
  }
  return p;
}

vector<int> d1_func(string &s) {
  // Calculates max length of palindrome centered in positions and i for each i
  //
  //
  vector<int> d1(s.size());
  int L = 0, R = -1;
  for (int i = 0; i < s.size(); i++) {
    int k = 0;
    if (i <= R)
      k = min(R - i + 1, d1[R - i + L]);
    while (i + k < s.size() && i - k >= 0 && s[i - k] == s[i + k])
      k++;
    d1[i] = k--;
    if (i + k > R) {
      L = i - k;
      R = i + k;
    }
  }
  return d1;
}

vector<int> d2_func(string &s) {
  // Calculates max length of palindrome centered in positions i - 1 and i for
  // each i
  //
  //
  vector<int> d2(s.size());
  int L = 0, R = -1;
  for (int i = 1; i < s.size(); i++) {
    int k = 0;
    if (i <= R)
      k = min(R - i + 1, d2[R - i + L + 1]);
    while (i + k < s.size() && i - k - 1 >= 0 && s[i - k - 1] == s[i + k])
      k++;
    d2[i] = k--;
    if (i + k > R) {
      L = i - k - 1;
      R = i + k;
    }
  }
  return d2;
}

void add(int pos, ll x, vector<ll> &fenw) {
  // Performs add query on Fenwick tree
  //
  // Tested on the following problem:
  // https://atcoder.jp/contests/practice2/tasks/practice2_b
  //
  while (pos < fenw.size()) {
    fenw[pos] += x;
    pos |= (pos + 1);
  }
}

ll get(int pos, vector<ll> &fenw) {
  // Performs get query on Fenwick tree
  //
  // Tested on the following problem:
  // https://atcoder.jp/contests/practice2/tasks/practice2_b
  //
  ll res = 0;
  while (pos >= 0) {
    res += fenw[pos];
    pos = (pos & (pos + 1)) - 1;
  }
  return res;
}

struct dcp_offine {
  // Given n, m and arrays v, u, t of length m
  // -1 <= t[i] <= 1 and 0 <= v[i], u[i] < n satisfied for t[i] != -1
  // t[i] = 0 asks to change state of edge (v[i], u[i])
  // t[i] = -1 asks to calculate number of connected components
  // t[i] = 1 asks to check if v[i] and u[i] are in the same connected component
  // After creating following holds:
  // ans[i] contains -1 for t[i] = 0 and answer for the i-th query otherwise
  //
  // Correctness t[i] <= 0 tested on the following problem:
  // https://codeforces.com/edu/course/2/lesson/7/3/practice/contest/289392/problem/C
  // Correctness tested on random tests with m = 40000
  // Speed tested on random tests with m = 2e6, average = 0.955ms/test (local)
  //
  int n, m;
  dsu_cut dcp;
  vector<int> ans;
  void solve(int l, int r, vi &v, vi &u, vi &t, vi &L, vi &R) {
    if (l >= r)
      return;
    if (l + 1 == r) {
      if (t[l] == -1) {
        ans[l] = n - dcp.cuts.size();
      }
      if (t[l] == 1) {
        ans[l] = dcp.Root(v[l]) == dcp.Root(u[l]);
      }
      return;
    }
    int m = (l + r) / 2;
    int ss = dcp.cuts.size();
    for (int i = l; i < m; i++) {
      if (R[i] >= r)
        dcp.Merge(v[i], u[i]);
    }
    solve(m, r, v, u, t, L, R);
    while (dcp.cuts.size() > ss)
      dcp.Cut();
    for (int i = r - 1; i >= m; i--) {
      if (L[i] < l)
        dcp.Merge(v[i], u[i]);
    }
    solve(l, m, v, u, t, L, R);
    while (dcp.cuts.size() > ss)
      dcp.Cut();
  }
  dcp_offine(int _n, int _m, vi v, vi u, vi t) {
    n = _n, m = _m;
    dcp.Reset(n);
    ans.resize(m, -1);
    map<pair<int, int>, int> mm;
    vi L(m, INF), R(m, -INF);
    for (int i = 0; i < m; i++) {
      if (t[i] != 0)
        continue;
      if (v[i] > u[i])
        swap(v[i], u[i]);
      if (mm.find({v[i], u[i]}) == mm.end()) {
        mm[{v[i], u[i]}] = i;
      } else {
        int t = mm[{v[i], u[i]}];
        R[t] = i;
        L[i] = t;
        mm.erase({v[i], u[i]});
      }
    }
    while (mm.size()) {
      t.push_back(0);
      ans.push_back(-1);
      L.push_back(INF);
      R.push_back(-INF);
      auto it = mm.begin();
      pair<int, int> s = (*it).first;
      int t = (*it).second;
      R[t] = m;
      L[m] = t;
      v.push_back(s.first);
      u.push_back(s.second);
      mm.erase(mm.begin());
      m++;
    }
    solve(0, m, v, u, t, L, R);
  }
};

template <class T> struct sparse_min {
  // Returns min on segment [l, r] of predefined array a
  //
  // Correctness tested on the following problem:
  // https://codeforces.com/contest/1304/problem/E
  // Speed tested on random tests with n = 4e6, q = 4e6, T = int, average =
  // 0.508ms/test (local) Speed tested on random tests with n = 4e6, q = 4e7, T
  // = int, average = 1.778ms/test (local)
  //
  vector<vector<T>> sp;
  vector<int> H;
  T get(int l, int r) {
    int h = H[r - l + 1];
    return min(sp[h][l], sp[h][r - (1 << h) + 1]);
  }
  sparse_min(vector<T> a) {
    int n = a.size();
    H = vector<int>(n + 1);
    for (int i = 3; i < n + 1; i++) {
      H[i] = H[i - 1] + (((i - 1) & (i - 2)) == 0);
    }
    int s = 0;
    while ((1 << s) < n)
      s++;
    sp = vector<vector<T>>(s, vector<T>(n));
    for (int i = 0; i < n; i++) {
      sp[0][i] = a[i];
    }
    for (int j = 1; j < s; j++) {
      for (int i = 0; i + (1 << j) - 1 < n; i++) {
        sp[j][i] = get(i, i + (1 << j) - 1);
      }
    }
  }
};

template <class T> struct sparse_max {
  // Returns min on segment [l, r] of predefined array a
  //
  // Correctness tested on the following problem:
  // https://codeforces.com/contest/1304/problem/E
  // Speed tested on random tests with n = 4e6, q = 4e6, T = int, average =
  // 0.508ms/test (local) Speed tested on random tests with n = 4e6, q = 4e7, T
  // = int, average = 1.778ms/test (local)
  //
  vector<vector<T>> sp;
  vector<int> H;
  T get(int l, int r) {
    int h = H[r - l + 1];
    return max(sp[h][l], sp[h][r - (1 << h) + 1]);
  }
  sparse_max(vector<T> a) {
    int n = a.size();
    H = vector<int>(n + 1);
    for (int i = 3; i < n + 1; i++) {
      H[i] = H[i - 1] + (((i - 1) & (i - 2)) == 0);
    }
    int s = 0;
    while ((1 << s) < n)
      s++;
    sp = vector<vector<T>>(s, vector<T>(n));
    for (int i = 0; i < n; i++) {
      sp[0][i] = a[i];
    }
    for (int j = 1; j < s; j++) {
      for (int i = 0; i + (1 << j) - 1 < n; i++) {
        sp[j][i] = get(i, i + (1 << j) - 1);
      }
    }
  }
};

struct scc_graph {
  // Find Strongly Connected Components of given graph
  //
  // Correctness tested on the following problem:
  // https://atcoder.jp/contests/arc069/tasks/arc069_d
  //
  int n;
  vector<vector<int>> graph;
  vector<vector<int>> inv_graph;
  vector<int> fr, to;
  vector<int> fn;
  vector<int> vis;
  vector<int> component;
  int col;
  void inv_dfs(int v) {
    vis[v] = 1;
    for (auto id : inv_graph[v]) {
      int u = fr[id];
      if (!vis[u]) {
        inv_dfs(u);
      }
    }
    fn.push_back(v);
  }
  void dfs(int v) {
    component[v] = col;
    for (auto id : graph[v]) {
      int u = to[id];
      if (component[u] == -1) {
        dfs(u);
      }
    }
  }
  scc_graph(int _n) {
    n = _n;
    graph = vector<vector<int>>(n);
    inv_graph = vector<vector<int>>(n);
  }
  void add_edge(int v, int u) {
    graph[v].push_back(fr.size());
    inv_graph[u].push_back(fr.size());
    fr.push_back(v);
    to.push_back(u);
  }
  vector<vector<int>> scc() {
    vis = vector<int>(n);
    component = vector<int>(n, -1);
    fn = {};
    col = 0;
    for (int i = 0; i < n; i++) {
      if (!vis[i]) {
        inv_dfs(i);
      }
    }
    reverse(fn.begin(), fn.end());
    for (auto i : fn) {
      if (component[i] == -1) {
        dfs(i);
        col++;
      }
    }
    vector<vector<int>> res(col);
    for (int i = 0; i < n; i++) {
      res[component[i]].push_back(i);
    }
    return res;
  }
};

const int N = 3010;

int dp[N][N][4];

signed main() {
  srand(time(NULL));
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);

  int n, m;
  cin >> n >> m;
  n--, m--;
  dp[0][0][1] = 1;
  dp[0][n][2] = 1;
  for (int p = 1; p < n; p++)
    dp[0][p][0] = 1;
  for (int i = 0; i < m; i++) {
    for (int j = 0; j <= n; j++) {
      for (auto e : {j - 1, j + 1}) {
        if (0 <= e && e <= n) {
          for (int mask = 0; mask < 4; mask++) {
            int nm = mask;
            if (e == 0)
              nm |= 1;
            if (e == n)
              nm |= 2;
            dp[i + 1][e][nm] = (dp[i + 1][e][nm] + dp[i][j][mask]) % MOD;
          }
        }
      }
    }
  }
  long long ans = 0;
  long long rev2 = (MOD + 1) / 2;
  for (int sel = 0; sel <= m; sel++) {
    long long si = 0;
    for (int i = 0; i <= n; i++) {
      si = (si + dp[sel][i][1] * rev2 + dp[sel][i][2] * rev2 + dp[sel][i][3]) %
           MOD;
    }
    si = si * cnk(m, sel) % MOD;
    si = si * inq(2, m - sel) % MOD;
    ans = (ans + si) % MOD;
  }
  cout << ans * 4 % MOD;
}

/* Note:
Check constants at the beginning of the code (MOD, INF, INFLL)
Check corner cases.
    N = 1
No def int long long for now.
Add something here.
*/