Problem p03820 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string.h>
#include <string>
#include <utility>
#include <vector>
// #include <unordered_set>
// #include <unordered_map>
// #include <bits/stdc++.h>

#define MP(A, B) make_pair(A, B)
#define PB(A) push_back(A)
#define SIZE(A) ((int)A.size())
#define LEN(A) ((int)A.length())
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define fi first
#define se second

using namespace std;

template <typename T> inline bool upmin(T &x, T y) {
  return y < x ? x = y, 1 : 0;
}
template <typename T> inline bool upmax(T &x, T y) {
  return x < y ? x = y, 1 : 0;
}

typedef long long ll;
typedef unsigned long long ull;
typedef long double lod;
typedef pair<int, int> PR;
typedef vector<int> VI;

const lod eps = 1e-11;
const lod pi = acos(-1);
const int oo = 1 << 30;
const ll loo = 1ll << 62;
const int mods = 1e9 + 7;
const int MAXN = 2005;
const int INF = 0x3f3f3f3f; // 1061109567
/*--------------------------------------------------------------------*/
inline int read() {
  int f = 1, x = 0;
  char c = getchar();
  while (c < '0' || c > '9') {
    if (c == '-')
      f = -1;
    c = getchar();
  }
  while (c >= '0' && c <= '9') {
    x = (x << 3) + (x << 1) + (c ^ 48);
    c = getchar();
  }
  return x * f;
}
int f[MAXN][MAXN], s[MAXN][MAXN];
inline int upd(int x, int y) { return x + y >= mods ? x + y - mods : x + y; }
inline int quick_pow(int x, int y) {
  int ret = 1;
  for (; y; y >>= 1) {
    if (y & 1)
      ret = 1ll * ret * x % mods;
    x = 1ll * x * x % mods;
  }
  return ret;
}
int main() {
  int n = read(), k = read();
  f[0][n + 1] = s[0][n + 1] = 1;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n - i + 1; j++)
      f[i][j] = upd(s[i - 1][n + 1], mods - s[i - 1][j - 1]),
      s[i][j] = upd(s[i][j - 1], f[i][j]);
    s[i][n + 1] = s[i][n - i + 1];
  }
  printf("%d\n", 1ll * upd(f[k][1], mods - f[k - 1][1]) *
                     quick_pow(2, n - k - 1) % mods);
  return 0;
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string.h>
#include <string>
#include <utility>
#include <vector>
// #include <unordered_set>
// #include <unordered_map>
// #include <bits/stdc++.h>

#define MP(A, B) make_pair(A, B)
#define PB(A) push_back(A)
#define SIZE(A) ((int)A.size())
#define LEN(A) ((int)A.length())
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define fi first
#define se second

using namespace std;

template <typename T> inline bool upmin(T &x, T y) {
  return y < x ? x = y, 1 : 0;
}
template <typename T> inline bool upmax(T &x, T y) {
  return x < y ? x = y, 1 : 0;
}

typedef long long ll;
typedef unsigned long long ull;
typedef long double lod;
typedef pair<int, int> PR;
typedef vector<int> VI;

const lod eps = 1e-11;
const lod pi = acos(-1);
const int oo = 1 << 30;
const ll loo = 1ll << 62;
const int mods = 1e9 + 7;
const int MAXN = 2005;
const int INF = 0x3f3f3f3f; // 1061109567
/*--------------------------------------------------------------------*/
inline int read() {
  int f = 1, x = 0;
  char c = getchar();
  while (c < '0' || c > '9') {
    if (c == '-')
      f = -1;
    c = getchar();
  }
  while (c >= '0' && c <= '9') {
    x = (x << 3) + (x << 1) + (c ^ 48);
    c = getchar();
  }
  return x * f;
}
int f[MAXN][MAXN], s[MAXN][MAXN];
inline int upd(int x, int y) { return x + y >= mods ? x + y - mods : x + y; }
inline int quick_pow(int x, int y) {
  if (y < 0)
    return 1;
  int ret = 1;
  for (; y; y >>= 1) {
    if (y & 1)
      ret = 1ll * ret * x % mods;
    x = 1ll * x * x % mods;
  }
  return ret;
}
int main() {
  int n = read(), k = read();
  f[0][n + 1] = s[0][n + 1] = 1;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n - i + 1; j++)
      f[i][j] = upd(s[i - 1][n + 1], mods - s[i - 1][j - 1]),
      s[i][j] = upd(s[i][j - 1], f[i][j]);
    s[i][n + 1] = s[i][n - i + 1];
  }
  printf("%d\n", 1ll * upd(f[k][1], mods - f[k - 1][1]) *
                     quick_pow(2, n - k - 1) % mods);
  return 0;
}