Problem p04027 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

#define _p(...) (void)printf(__VA_ARGS__)
#define forr(x, arr) for (auto &&x : arr)
#define _overload3(_1, _2, _3, name, ...) name
#define _rep2(i, n) _rep3(i, 0, n)
#define _rep3(i, a, b) for (int i = int(a); i < int(b); ++i)
#define rep(...) _overload3(__VA_ARGS__, _rep3, _rep2, )(__VA_ARGS__)
#define _rrep2(i, n) _rrep3(i, 0, n)
#define _rrep3(i, a, b) for (int i = int(b) - 1; i >= int(a); i--)
#define rrep(...) _overload3(__VA_ARGS__, _rrep3, _rrep2, )(__VA_ARGS__)
#define ALL(x) (x).begin(), (x).end()
#define BIT(n) (1LL << (n))
#define SZ(x) ((int)(x).size())
#define fst first
#define snd second
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int, int> pii;
typedef vector<pii> vpii;
typedef long long ll;

typedef vector<ll> vl;
typedef vector<vl> vvl;

const long long mod = 1000000007;

long long powMod(long long x, long long y) {
  long long r = 1, a = x % mod;
  while (y) {
    if (y & 1)
      r = (r * a) % mod;
    a = a * a % mod;
    y /= 2;
  }
  return r;
}

void Main() {
  int N, K;
  scanf("%d%d", &N, &K);
  vi A(N), B(N);
  rep(i, N) scanf("%d", &A[i]);
  rep(i, N) scanf("%d", &B[i]);

  vvl dp(N + 1, vl(K + 1));
  dp[0][0] = 1;

  // ppow[i][j] : i^j
  vvl ppow(401, vl(K + 1));
  rep(i, 401) {
    ppow[i][0] = 1;
    rep(j, K) { (ppow[i][j + 1] = ppow[i][j] * i % mod) %= mod; }
  }

  // spow[i][j] : 0^j + 1^j + 2^j + ... + (i-1)^j
  vvl spow(401, vl(K + 1));
  rep(i, 400) {
    rep(j, K + 1) { spow[i + 1][j] = (spow[i][j] + ppow[i][j]) % mod; }
  }

  rep(i, N) rep(j, K + 1) {
    // dp[i][j] : i-1 番までに j 個
    int a = A[i];
    int b = B[i];
    rep(k, j + 1) {
      // i番がk個
      // p : (1^k + 2^k + 3^k + ...b^k) - (1^k + 2^k + 3^k + ... + (a-1)^k) =
      // a^k + (a+1)^k + ... + b^k
      ll p = (spow[b + 1][k] - spow[a][k] + mod) % mod;
      (dp[i + 1][j] += dp[i][j - k] * p) %= mod;
    }
  }

  ll ans = dp[N][K];

  _p("%lld\n", ans);
}
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  Main();
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;

#define _p(...) (void)printf(__VA_ARGS__)
#define forr(x, arr) for (auto &&x : arr)
#define _overload3(_1, _2, _3, name, ...) name
#define _rep2(i, n) _rep3(i, 0, n)
#define _rep3(i, a, b) for (int i = int(a); i < int(b); ++i)
#define rep(...) _overload3(__VA_ARGS__, _rep3, _rep2, )(__VA_ARGS__)
#define _rrep2(i, n) _rrep3(i, 0, n)
#define _rrep3(i, a, b) for (int i = int(b) - 1; i >= int(a); i--)
#define rrep(...) _overload3(__VA_ARGS__, _rrep3, _rrep2, )(__VA_ARGS__)
#define ALL(x) (x).begin(), (x).end()
#define BIT(n) (1LL << (n))
#define SZ(x) ((int)(x).size())
#define fst first
#define snd second
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int, int> pii;
typedef vector<pii> vpii;
typedef long long ll;

typedef vector<ll> vl;
typedef vector<vl> vvl;

const long long mod = 1000000007;

long long powMod(long long x, long long y) {
  long long r = 1, a = x % mod;
  while (y) {
    if (y & 1)
      r = (r * a) % mod;
    a = a * a % mod;
    y /= 2;
  }
  return r;
}

void Main() {
  int N, K;
  scanf("%d%d", &N, &K);
  vi A(N), B(N);
  rep(i, N) scanf("%d", &A[i]);
  rep(i, N) scanf("%d", &B[i]);

  vvl dp(N + 1, vl(K + 1));
  dp[0][0] = 1;

  // ppow[i][j] : i^j
  vvl ppow(401, vl(K + 1));
  rep(i, 401) {
    ppow[i][0] = 1;
    rep(j, K) { (ppow[i][j + 1] = ppow[i][j] * i % mod) %= mod; }
  }

  // spow[i][j] : 0^j + 1^j + 2^j + ... + (i-1)^j
  vvl spow(401 + 1, vl(K + 1));
  rep(i, 401) {
    rep(j, K + 1) { spow[i + 1][j] = (spow[i][j] + ppow[i][j]) % mod; }
  }

  rep(i, N) rep(j, K + 1) {
    // dp[i][j] : i-1 番までに j 個
    int a = A[i];
    int b = B[i];
    rep(k, j + 1) {
      // i番がk個
      // p : (1^k + 2^k + 3^k + ...b^k) - (1^k + 2^k + 3^k + ... + (a-1)^k) =
      // a^k + (a+1)^k + ... + b^k
      ll p = (spow[b + 1][k] - spow[a][k] + mod) % mod;
      (dp[i + 1][j] += dp[i][j - k] * p) %= mod;
    }
  }

  ll ans = dp[N][K];

  _p("%lld\n", ans);
}
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  Main();
  return 0;
}