Problem p03820 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <assert.h>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <map>
#include <queue>
#include <set>
#include <vector>
#define LL long long
#define mp(x, y) make_pair(x, y)
#define pll pair<long long, long long>
#define pii pair<int, int>
#define SZ(x) ((int)x.size())
using namespace std;
inline int read() {
  int f = 1, x = 0;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-')
      f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    x = x * 10 + ch - '0';
    ch = getchar();
  }
  return x * f;
}
int stack[20];
template <typename T> inline void write(T x) {
  if (x < 0) {
    putchar('-');
    x = -x;
  }
  if (!x) {
    putchar('0');
    return;
  }
  int top = 0;
  while (x)
    stack[++top] = x % 10, x /= 10;
  while (top)
    putchar(stack[top--] + '0');
}
template <typename T> inline void pr1(T x) {
  write(x);
  putchar(' ');
}
template <typename T> inline void pr2(T x) {
  write(x);
  putchar('\n');
}
const int MAXN = 2005;
const int mod = 1e9 + 7;
int pow_mod(int a, int b) {
  int ret = 1;
  for (; b; b >>= 1, a = 1LL * a * a % mod)
    if (b & 1)
      ret = 1LL * ret * a % mod;
  return ret;
}
void ad(int &x, int y) {
  x += y;
  if (x >= mod)
    x -= mod;
}
int f[MAXN][MAXN], g[MAXN], n, K;
int pre[MAXN], inv[MAXN];
int C(int n, int m) { return 1LL * pre[n] * inv[m] % mod * inv[n - m] % mod; }
int main() {
  pre[0] = 1;
  for (int i = 1; i < MAXN; i++)
    pre[i] = 1LL * pre[i - 1] * i % mod;
  inv[MAXN - 1] = pow_mod(pre[MAXN - 1], mod - 2);
  for (int i = MAXN - 2; i >= 0; i--)
    inv[i] = 1LL * inv[i + 1] * (i + 1) % mod;
  n = read();
  K = read();
  if (K == 1)
    return pr2(pow_mod(2, n - 2)), 0;
  for (int i = n; i >= 2; i--)
    f[i][1] = 1;
  for (int i = n; i >= 1; i--) {
    for (int j = 1; j <= (n - i + 1); j++)
      if (f[i][j]) {
        ad(f[i][j], g[j]);
        if ((n - i + 1) - j)
          ad(f[i][j + 1], f[i][j]);
      }
    for (int j = 1; j <= (n - i + 1); j++)
      ad(g[j + 1], f[i][j]);
  }
  int ans = 0;
  for (int i = n; i >= 1; i--)
    ad(ans, f[i][K - 1]);
  if (K != n)
    ans = 1LL * ans * pow_mod(2, n - K - 1) % mod;
  pr2(ans);
  return 0;
}


Fixed submission:
#include <algorithm>
#include <assert.h>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <map>
#include <queue>
#include <set>
#include <vector>
#define LL long long
#define mp(x, y) make_pair(x, y)
#define pll pair<long long, long long>
#define pii pair<int, int>
#define SZ(x) ((int)x.size())
using namespace std;
inline int read() {
  int f = 1, x = 0;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-')
      f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    x = x * 10 + ch - '0';
    ch = getchar();
  }
  return x * f;
}
int stack[20];
template <typename T> inline void write(T x) {
  if (x < 0) {
    putchar('-');
    x = -x;
  }
  if (!x) {
    putchar('0');
    return;
  }
  int top = 0;
  while (x)
    stack[++top] = x % 10, x /= 10;
  while (top)
    putchar(stack[top--] + '0');
}
template <typename T> inline void pr1(T x) {
  write(x);
  putchar(' ');
}
template <typename T> inline void pr2(T x) {
  write(x);
  putchar('\n');
}
const int MAXN = 2005;
const int mod = 1e9 + 7;
int pow_mod(int a, int b) {
  int ret = 1;
  for (; b; b >>= 1, a = 1LL * a * a % mod)
    if (b & 1)
      ret = 1LL * ret * a % mod;
  return ret;
}
void ad(int &x, int y) {
  x += y;
  if (x >= mod)
    x -= mod;
}
int f[MAXN][MAXN], g[MAXN], n, K;
int pre[MAXN], inv[MAXN];
int C(int n, int m) { return 1LL * pre[n] * inv[m] % mod * inv[n - m] % mod; }
int main() {
  pre[0] = 1;
  for (int i = 1; i < MAXN; i++)
    pre[i] = 1LL * pre[i - 1] * i % mod;
  inv[MAXN - 1] = pow_mod(pre[MAXN - 1], mod - 2);
  for (int i = MAXN - 2; i >= 0; i--)
    inv[i] = 1LL * inv[i + 1] * (i + 1) % mod;
  n = read();
  K = read();
  if (K == 1) {
    if (n >= 2)
      return pr2(pow_mod(2, n - 2)), 0;
    return pr2(1), 0;
  }
  for (int i = n; i >= 2; i--)
    f[i][1] = 1;
  for (int i = n; i >= 1; i--) {
    for (int j = 1; j <= (n - i + 1); j++)
      if (f[i][j]) {
        ad(f[i][j], g[j]);
        if ((n - i + 1) - j)
          ad(f[i][j + 1], f[i][j]);
      }
    for (int j = 1; j <= (n - i + 1); j++)
      ad(g[j + 1], f[i][j]);
  }
  int ans = 0;
  for (int i = n; i >= 1; i--)
    ad(ans, f[i][K - 1]);
  if (K != n)
    ans = 1LL * ans * pow_mod(2, n - K - 1) % mod;
  pr2(ans);
  return 0;
}