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

//~ while (clock()<=69*CLOCKS_PER_SEC)
//~ #pragma comment(linker, "/stack:200000000")
#pragma GCC optimize("O3")
//~ #pragma GCC target ("avx2")
//~ #pragma GCC optimize("Ofast")
//~ #pragma GCC
//target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") ~ #pragma
//GCC optimize("unroll-loops")
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace __gnu_pbds;
using namespace std;

template <typename T>
using ordered_set =
    tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

#define sim template <class c
#define ris return *this
#define dor > debug &operator<<
#define eni(x)                                                                 \\
  sim > typename enable_if<sizeof dud<c>(0) x 1, debug &>::type operator<<(    \\
            c i) {
sim > struct rge {
  c b, e;
};
sim > rge<c> range(c i, c j) { return rge<c>{i, j}; }
sim > auto dud(c *x) -> decltype(cerr << *x, 0);
sim > char dud(...);
struct debug {
#ifdef LOCAL
  ~debug() { cerr << endl; }
  eni(!=) cerr << boolalpha << i;
  ris;
} eni(==) ris << range(begin(i), end(i));
}
sim, class b dor(pair<b, c> d) {
  ris << "(" << d.first << ", " << d.second << ")";
}
sim dor(rge<c> d) {
  *this << "[";
  for (auto it = d.b; it != d.e; ++it)
    *this << ", " + 2 * (it == d.b) << *it;
  ris << "]";
}
#else
  sim dor(const c &) { ris; }
#endif
}
;
#define imie(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] "

#define shandom_ruffle random_shuffle

using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using vi = vector<int>;
using vll = vector<ll>;
const int nax = 5007;
const int vax = 10 * 1007 * 1007;
const ll mod = 1000 * 1000 * 1000 + 7;

int n, k;

int dp[nax][nax];

ll sil[vax];
ll odw[nax];

ll wyn;

void sdod(ll &a, ll b) { a = (a + b) % mod; }

void sdod(int &a, ll b) { a = (a + b) % mod; }

ll dziel(ll a, ll b) {
  ll wyk = mod - 2;
  while (wyk) {
    if (wyk & 1)
      a = (a * b) % mod;
    b = (b * b) % mod;
    wyk >>= 1;
  }
  return a;
}

ll kom(int a, int b) {
  if (b < 0 || b > a)
    return 0;
  return (sil[a] * (odw[b] * odw[a - b] % mod)) % mod;
}

int spo[nax][nax];

void ogar(int moge, int musze, ll mno) {
  if (!mno || !moge)
    return;
  int jest = n - k;
  if (jest < musze)
    return;
  wyn = (wyn + mno * spo[moge][musze]) % mod;
  //~ debug() << imie(moge) << imie(musze) << imie(mno);
}

int pchaj[nax][nax];

int main() {
  sil[0] = 1;
  for (int i = 1; i < vax; i++)
    sil[i] = (sil[i - 1] * i) % mod;
  odw[nax - 1] = dziel(1, sil[nax - 1]);
  for (int i = nax - 2; i >= 0; i--)
    odw[i] = (odw[i + 1] * (i + 1)) % mod;
  scanf("%d%d", &n, &k);
  int poza = n - k;
  for (int j = 0; j <= k; j++) {
    for (int i = max(1, j); i <= k; i++) {
      if (!j) {
        spo[i][j] = (sil[i + poza - 1] * odw[i - 1]) % mod;
        continue;
      }
      spo[i][j] = (spo[i][j - 1] - spo[i - 1][j - 1] + mod) % mod;
    }
  }
  dp[k][0] = 1;
  for (int i = k; i >= 0; i--) {
    for (int j = 0; j <= k; j++) {
      sdod(dp[i][j], pchaj[i][j]);
      if (i)
        sdod(pchaj[i - 1][j], pchaj[i][j] * (ll)i);
      if (!dp[i][j] || !i)
        continue;
      sdod(dp[i - 1][j + 1], dp[i][j]);

      if (i >= 2)
        sdod(pchaj[i - 2][j], dp[i][j] * (i - 1LL));

      ogar(k - i, j, dp[i][j] * sil[i - 1] % mod);
    }
  }
  for (int i = 0; i <= k; i++)
    ogar(k, i, dp[0][i]);
  printf("%lld\\n", wyn);
  return 0;
}


Error: -11

Fixed submission:

//~ while (clock()<=69*CLOCKS_PER_SEC)
//~ #pragma comment(linker, "/stack:200000000")
#pragma GCC optimize("O3")
//~ #pragma GCC target ("avx2")
//~ #pragma GCC optimize("Ofast")
//~ #pragma GCC
//target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") ~ #pragma
//GCC optimize("unroll-loops")
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace __gnu_pbds;
using namespace std;

template <typename T>
using ordered_set =
    tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

#define sim template <class c
#define ris return *this
#define dor > debug &operator<<
#define eni(x)                                                                 \\
  sim > typename enable_if<sizeof dud<c>(0) x 1, debug &>::type operator<<(    \\
            c i) {
sim > struct rge {
  c b, e;
};
sim > rge<c> range(c i, c j) { return rge<c>{i, j}; }
sim > auto dud(c *x) -> decltype(cerr << *x, 0);
sim > char dud(...);
struct debug {
#ifdef LOCAL
  ~debug() { cerr << endl; }
  eni(!=) cerr << boolalpha << i;
  ris;
} eni(==) ris << range(begin(i), end(i));
}
sim, class b dor(pair<b, c> d) {
  ris << "(" << d.first << ", " << d.second << ")";
}
sim dor(rge<c> d) {
  *this << "[";
  for (auto it = d.b; it != d.e; ++it)
    *this << ", " + 2 * (it == d.b) << *it;
  ris << "]";
}
#else
  sim dor(const c &) { ris; }
#endif
}
;
#define imie(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] "

#define shandom_ruffle random_shuffle

using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using vi = vector<int>;
using vll = vector<ll>;
const int nax = 5007;
const int vax = 10 * 1007 * 1007;
const ll mod = 1000 * 1000 * 1000 + 7;

int n, k;

ll dp[nax][nax];

ll sil[vax];
ll odw[vax];

ll wyn;

void dod(ll &a, ll b) {
  if ((a += b) >= mod)
    a -= mod;
}

void sdod(ll &a, ll b) { a = (a + b) % mod; }

ll dziel(ll a, ll b) {
  ll wyk = mod - 2;
  while (wyk) {
    if (wyk & 1)
      a = (a * b) % mod;
    b = (b * b) % mod;
    wyk >>= 1;
  }
  return a;
}

ll kom(int a, int b) {
  if (b < 0 || b > a)
    return 0;
  return (sil[a] * (odw[b] * odw[a - b] % mod)) % mod;
}

ll spo[nax][nax];

void ogar(int moge, int musze, ll mno) {
  if (!mno || !moge)
    return;
  int jest = n - k;
  if (jest < musze)
    return;
  wyn = (wyn + mno * spo[moge][musze]) % mod;
  //~ debug() << imie(moge) << imie(musze) << imie(mno);
}

ll pchaj[nax][nax];

int main() {
  sil[0] = 1;
  for (int i = 1; i < vax; i++)
    sil[i] = (sil[i - 1] * i) % mod;
  odw[vax - 1] = dziel(1, sil[vax - 1]);
  for (int i = vax - 2; i >= 0; i--)
    odw[i] = (odw[i + 1] * (i + 1)) % mod;
  scanf("%d%d", &n, &k);
  int poza = n - k;
  for (int j = 0; j <= k; j++) {
    for (int i = max(1, j); i <= k; i++) {
      if (!j) {
        spo[i][j] = (sil[i + poza - 1] * odw[i - 1]) % mod;
        continue;
      }
      spo[i][j] = (spo[i][j - 1] - spo[i - 1][j - 1] + mod) % mod;
    }
  }
  dp[k][0] = 1;
  for (int i = k; i >= 0; i--) {
    for (int j = 0; j <= k; j++) {
      sdod(dp[i][j], pchaj[i][j] * odw[i]);
      sdod(pchaj[i - 1][j], pchaj[i][j]);
      if (!dp[i][j] || !i)
        continue;
      sdod(dp[i - 1][j + 1], dp[i][j]);
      //~ for (int l=2; l<=i; l++)
      //~ sdod(dp[i-l][j], dp[i][j]*sil[i-1]%mod*odw[i-l]%mod);

      if (i >= 2)
        sdod(pchaj[i - 2][j], dp[i][j] * sil[i - 1]);

      ogar(k - i, j, dp[i][j] * sil[i - 1] % mod);
    }
  }
  //~ for (int i=k; i>=0; i--)
  //~ {
  //~ for (int j=0; j<=k; j++)
  //~ {
  //~ if (!dp[i][j])
  //~ continue;
  //~ debug() << i << " " << j << "  " << dp[i][j];
  //~ }
  //~ }
  for (int i = 0; i <= k; i++)
    ogar(k, i, dp[0][i]);
  printf("%lld\\n", wyn);
  return 0;
}