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

#include <bits/stdc++.h>
using namespace std;
namespace Base {
#define mr make_pair
typedef long long ll;
typedef double db;
const int inf = 0x3f3f3f3f, INF = 0x7fffffff;
const ll infll = 0x3f3f3f3f3f3f3f3fll, INFll = 0x7fffffffffffffffll;
template <typename T> void read(T &x) {
  x = 0;
  int fh = 1;
  double num = 1.0;
  char ch = getchar();
  while (!isdigit(ch)) {
    if (ch == '-')
      fh = -1;
    ch = getchar();
  }
  while (isdigit(ch)) {
    x = x * 10 + ch - '0';
    ch = getchar();
  }
  if (ch == '.') {
    ch = getchar();
    while (isdigit(ch)) {
      num /= 10;
      x = x + num * (ch - '0');
      ch = getchar();
    }
  }
  x = x * fh;
}
template <typename T> void chmax(T &x, T y) { x = x < y ? y : x; }
template <typename T> void chmin(T &x, T y) { x = x > y ? y : x; }
} // namespace Base
using namespace Base;

const int P = 1e9 + 7, N = 5010, M = 1e7 + 10;
int mul[M], inv[M], m, n, ans, c[N][N];
int power(int x, int y) {
  int i = x;
  x = 1;
  while (y > 0) {
    if (y & 1)
      x = 1ll * i * x % P;
    i = 1ll * i * i % P;
    y /= 2;
  }
  return x;
}
int main() {
  read(m);
  read(n);
  mul[0] = 1;
  for (int i = 1; i <= m; i++)
    mul[i] = 1ll * mul[i - 1] * i % P;
  inv[m] = power(mul[m], P - 2);
  for (int i = m - 1; i >= 0; i--)
    inv[i] = 1ll * inv[i + 1] * (i + 1) % P;
  for (int i = 0; i <= n; i++) {
    c[i][0] = 1;
    for (int j = 1; j <= i; j++)
      c[i][j] = (c[i - 1][j - 1] + c[i - 1][j]) % P;
  }
  for (int i = n + 1; i >= 2; i--) {
    int fh;
    for (int j = 0; j <= i - 1; j++) {
      if (j % 2 == 0)
        fh = c[i - 1][j];
      else
        fh = -c[i - 1][j];
      int a = i - 1 - j, b = m - n, c = max(0, n - i);
      ans = (ans +
             1ll * fh * mul[a + b + c] % P * a % P * inv[a + b] % P *
                 mul[a + b - 1] % P +
             P) %
            P;
    }
  }
  cout << ans << endl;
  return 0;
}


Error: -11

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
namespace Base {
#define mr make_pair
typedef long long ll;
typedef double db;
const int inf = 0x3f3f3f3f, INF = 0x7fffffff;
const ll infll = 0x3f3f3f3f3f3f3f3fll, INFll = 0x7fffffffffffffffll;
template <typename T> void read(T &x) {
  x = 0;
  int fh = 1;
  double num = 1.0;
  char ch = getchar();
  while (!isdigit(ch)) {
    if (ch == '-')
      fh = -1;
    ch = getchar();
  }
  while (isdigit(ch)) {
    x = x * 10 + ch - '0';
    ch = getchar();
  }
  if (ch == '.') {
    ch = getchar();
    while (isdigit(ch)) {
      num /= 10;
      x = x + num * (ch - '0');
      ch = getchar();
    }
  }
  x = x * fh;
}
template <typename T> void chmax(T &x, T y) { x = x < y ? y : x; }
template <typename T> void chmin(T &x, T y) { x = x > y ? y : x; }
} // namespace Base
using namespace Base;

const int P = 1e9 + 7, N = 5010, M = 1e7 + 10;
int mul[M], inv[M], m, n, ans, c[N][N];
int power(int x, int y) {
  int i = x;
  x = 1;
  while (y > 0) {
    if (y & 1)
      x = 1ll * i * x % P;
    i = 1ll * i * i % P;
    y /= 2;
  }
  return x;
}
int main() {
  read(m);
  read(n);
  mul[0] = 1;
  for (int i = 1; i <= m; i++)
    mul[i] = 1ll * mul[i - 1] * i % P;
  inv[m] = power(mul[m], P - 2);
  for (int i = m - 1; i >= 0; i--)
    inv[i] = 1ll * inv[i + 1] * (i + 1) % P;
  for (int i = 0; i <= n; i++) {
    c[i][0] = 1;
    for (int j = 1; j <= n; j++)
      c[i][j] = (c[i - 1][j - 1] + c[i - 1][j]) % P;
  }
  for (int i = n + 1; i >= 2; i--) {
    int fh;
    for (int j = 0; j <= i - 1; j++) {
      if (j % 2 == 0)
        fh = c[i - 1][j];
      else
        fh = -c[i - 1][j];
      int a = i - 1 - j, b = m - n, c = max(0, n - i);
      ans = (ans +
             1ll * fh * mul[a + b + c] * a % P * inv[a + b] % P *
                 mul[a + b - 1] % P +
             P) %
            P;
    }
  }
  cout << ans << endl;
  return 0;
}