Problem p03820 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define rep(i, a, b) for (int i = (a); i <= (b); i++)
#define re(i, a, b) for (int i = (a); i < (b); i++)
#define repd(i, a, b) for (int i = (a); i >= (b); i--)
#define clr(a) memset(a, 0, sizeof(a));
#define il inline
#define sz(a) ((int)a.size())
#define all(a) a.begin(), a.end()
#define mp make_pair
#define pb push_back
#define w1 first
#define w2 second
#define adm(a, b, c)                                                           \
  {                                                                            \
    a = a + b;                                                                 \
    if (a >= c)                                                                \
      a -= c;                                                                  \
    else if (a < 0)                                                            \
      a += c;                                                                  \
  }
typedef double db;
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
typedef pair<ld, ld> pa;
const int N = 5000 + 5, M = 1e7 + 5, INF = 1e9, mod = 1e9 + 7;
const ll linf = 1e18;
const double eps = 1e-8, pi = acos(-1);
il int gmin(int &a, int b) {
  if (a > b)
    a = b;
}
il ll gmin(ll &a, ll b) {
  if (a > b)
    a = b;
}
il int gmax(int &a, int b) {
  if (a < b)
    a = b;
}
il ll gmax(ll &a, ll b) {
  if (a < b)
    a = b;
}
il void read(ll &x) {
  ll f = 1, t = 0;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-')
      f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    t = t * 10 + ch - '0';
    ch = getchar();
  }
  x = t * f;
}
il ll read(ll &x, ll &y) {
  read(x);
  read(y);
}
il void read(int &x) {
  int f = 1, t = 0;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-')
      f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    t = t * 10 + ch - '0';
    ch = getchar();
  }
  x = t * f;
}
il int read(int &x, int &y) {
  read(x);
  read(y);
}
il void read(int &a, int &b, int &c) {
  read(a);
  read(b);
  read(c);
}
il void read(ll &a, ll &b, ll &c) {
  read(a);
  read(b);
  read(c);
}
il int read() {
  int x;
  read(x);
  return x;
}
il ll qpow(ll a, ll b, ll p) {
  ll ret = 1;
  for (; b; b >>= 1, a = a * a % p)
    if (b & 1)
      ret = ret * a % p;
  return ret;
}
il ll qpow(ll a, ll b) {
  ll ret = 1;
  for (; b; b >>= 1, a = a * a % mod)
    if (b & 1)
      ret = ret * a % mod;
  return ret;
}
il ll qmul(ll a, ll b, ll p) {
  ll ret = 0;
  for (; b; b >>= 1, a = (a << 1) % p)
    if (b & 1)
      adm(ret, a, p);
  return ret;
}
il ll qmul(ll a, ll b) {
  ll ret = 0;
  for (; b; b >>= 1, a = (a << 1) % mod)
    if (b & 1)
      adm(ret, a, mod);
  return ret;
}
il void judge() {
  freopen("data.in", "r", stdin);
  freopen("data.out", "w", stdout);
}
il void gen() { freopen("data.in", "w", stdout); }
int n, k;
ll fac[N], ifac[N], f[N][N][2];
ll C(ll n, ll m) {
  if (n < 0 || m < 0 || n < m)
    return 0;
  return fac[n] * ifac[m] % mod * ifac[n - m] % mod;
}
ll cata(int x) {
  if (!x)
    return 1;
  if (x == 1)
    return 1;
  return C(x + x, x) - C(x + x, x - 1);
}
void add(ll &a, ll b) {
  a += b;
  a %= mod;
}
int main() {
  read(n, k);
  /*fac[0]=1;
  rep(i,1,n)fac[i]=fac[i-1]*i%mod;
  ifac[n]=qpow(fac[n],mod-2,mod);
  repd(i,n-1,0)ifac[i]=ifac[i+1]*(i+1)%mod;
  ll res=C(n-1,k-1);
  cout<<res*qpow(2,n-k-1,mod)%mod;*/
  f[n][0][0] = 1;
  repd(i, n, 1) repd(j, n, 0) {
    add(f[i - 1][j][0], f[i][j][0] + f[i][j][1]);
    add(f[i - 1][j + 1][1], f[i][j][0] + f[i][j][1]);
    if (j)
      add(f[i][j - 1][0], f[i][j][0]);
  }
  ll res = (f[1][n - k][0] + f[1][n - k][1]) % mod;
  if (n - k - 1)
    res = res * qpow(2, n - k - 1, mod) % mod;
  cout << res << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define rep(i, a, b) for (int i = (a); i <= (b); i++)
#define re(i, a, b) for (int i = (a); i < (b); i++)
#define repd(i, a, b) for (int i = (a); i >= (b); i--)
#define clr(a) memset(a, 0, sizeof(a));
#define il inline
#define sz(a) ((int)a.size())
#define all(a) a.begin(), a.end()
#define mp make_pair
#define pb push_back
#define w1 first
#define w2 second
#define adm(a, b, c)                                                           \
  {                                                                            \
    a = a + b;                                                                 \
    if (a >= c)                                                                \
      a -= c;                                                                  \
    else if (a < 0)                                                            \
      a += c;                                                                  \
  }
typedef double db;
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
typedef pair<ld, ld> pa;
const int N = 5000 + 5, M = 1e7 + 5, INF = 1e9, mod = 1e9 + 7;
const ll linf = 1e18;
const double eps = 1e-8, pi = acos(-1);
il int gmin(int &a, int b) {
  if (a > b)
    a = b;
}
il ll gmin(ll &a, ll b) {
  if (a > b)
    a = b;
}
il int gmax(int &a, int b) {
  if (a < b)
    a = b;
}
il ll gmax(ll &a, ll b) {
  if (a < b)
    a = b;
}
il void read(ll &x) {
  ll f = 1, t = 0;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-')
      f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    t = t * 10 + ch - '0';
    ch = getchar();
  }
  x = t * f;
}
il ll read(ll &x, ll &y) {
  read(x);
  read(y);
}
il void read(int &x) {
  int f = 1, t = 0;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-')
      f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    t = t * 10 + ch - '0';
    ch = getchar();
  }
  x = t * f;
}
il int read(int &x, int &y) {
  read(x);
  read(y);
}
il void read(int &a, int &b, int &c) {
  read(a);
  read(b);
  read(c);
}
il void read(ll &a, ll &b, ll &c) {
  read(a);
  read(b);
  read(c);
}
il int read() {
  int x;
  read(x);
  return x;
}
il ll qpow(ll a, ll b, ll p) {
  ll ret = 1;
  for (; b; b >>= 1, a = a * a % p)
    if (b & 1)
      ret = ret * a % p;
  return ret;
}
il ll qpow(ll a, ll b) {
  ll ret = 1;
  for (; b; b >>= 1, a = a * a % mod)
    if (b & 1)
      ret = ret * a % mod;
  return ret;
}
il ll qmul(ll a, ll b, ll p) {
  ll ret = 0;
  for (; b; b >>= 1, a = (a << 1) % p)
    if (b & 1)
      adm(ret, a, p);
  return ret;
}
il ll qmul(ll a, ll b) {
  ll ret = 0;
  for (; b; b >>= 1, a = (a << 1) % mod)
    if (b & 1)
      adm(ret, a, mod);
  return ret;
}
il void judge() {
  freopen("data.in", "r", stdin);
  freopen("data.out", "w", stdout);
}
il void gen() { freopen("data.in", "w", stdout); }
int n, k;
ll fac[N], ifac[N], f[N][N][2];
ll C(ll n, ll m) {
  if (n < 0 || m < 0 || n < m)
    return 0;
  return fac[n] * ifac[m] % mod * ifac[n - m] % mod;
}
ll cata(int x) {
  if (!x)
    return 1;
  if (x == 1)
    return 1;
  return C(x + x, x) - C(x + x, x - 1);
}
void add(ll &a, ll b) {
  a += b;
  a %= mod;
}
int main() {
  read(n, k);
  /*fac[0]=1;
  rep(i,1,n)fac[i]=fac[i-1]*i%mod;
  ifac[n]=qpow(fac[n],mod-2,mod);
  repd(i,n-1,0)ifac[i]=ifac[i+1]*(i+1)%mod;
  ll res=C(n-1,k-1);
  cout<<res*qpow(2,n-k-1,mod)%mod;*/
  f[n][0][0] = 1;
  repd(i, n, 1) repd(j, n, 0) {
    add(f[i - 1][j][0], f[i][j][0] + f[i][j][1]);
    add(f[i - 1][j + 1][1], f[i][j][0] + f[i][j][1]);
    if (j)
      add(f[i][j - 1][0], f[i][j][0]);
  }
  ll res = (f[1][n - k][0] + f[1][n - k][1]) % mod;
  if (n - k - 1 >= 0)
    res = res * qpow(2, n - k - 1, mod) % mod;
  cout << res << endl;
}