Problem p03314 (C++)

Buggy submission (status: Runtime Error):
// Comment
// Hard. Think All - non colorful.
// And also Dp[i][j] =
// 		In interval [1, i],
// 		last j are pairly diff, last j+1 are not

#include <bits/stdc++.h>

using namespace std;

#define SZ(v) ((int)(v).size())
#define ALL(v) (v).begin(), (v).end()
#define one first
#define two second
using ll = long long;
using pi = pair<int, int>;
const int INF = 0x3f3f3f3f;
const ll LINF = 1ll * INF * INF;

const int MOD = 1e9 + 7;
const int MAX_N = 3e4 + 10;
const int MAX_K = 5e2 + 10;

int N, K, M;
int Nr[MAX_N];
ll Sum[MAX_N][MAX_K];
ll Dy[MAX_N][MAX_K];
ll SumG[MAX_N][MAX_K];
ll G[MAX_N][MAX_K];
ll Fac[MAX_N];

ll Base;
ll Pow(ll a, ll b) {
  ll p = a, r = 1;
  while (b) {
    if (b % 2)
      r *= p, r %= MOD;
    b /= 2;
    p *= p;
    p %= MOD;
  }
  return r;
}
ll Inv(ll a) { return Pow(a, MOD - 2); }

int main() {
  cin >> N >> K >> M;
  for (int i = 1; i <= M; i++)
    scanf("%d", &Nr[i]);
  Fac[0] = 1;
  for (int i = 1; i <= max(N, K); i++)
    Fac[i] = Fac[i - 1] * i % MOD;

  vector<int> cnt(K + 1, 0);
  for (int i = 1; i < K; i++)
    cnt[Nr[i]]++;
  bool isColorful = false;
  for (int i = K; i <= N; i++) {
    cnt[Nr[i]]++;
    bool isAll = true;
    for (int k = 1; k <= K; k++)
      if (cnt[k] == 0)
        isAll = false;
    if (isAll) {
      isColorful = true;
      break;
    }
    cnt[Nr[i - (K - 1)]]--;
  }

  bool allDiff = true;
  for (int i = 1; i <= K; i++)
    if (cnt[i] >= 2)
      allDiff = false;

  Base = N - M + 1;
  for (int i = 0; i < N - M; i++)
    Base = Base * K % MOD;

  Dy[0][0] = 1;
  for (int i = 1; i <= N; i++) {
    for (int j = 1; j < K; j++) {
      Dy[i][j] += Dy[i - 1][j - 1] * (K - (j - 1));
      Dy[i][j] += Sum[i - 1][j];
      Dy[i][j] %= MOD;
    }
    for (int j = K; j >= 1; j--) {
      Sum[i][j] = (Sum[i][j + 1] + Dy[i][j]) % MOD;
    }
  }

  // for(int i=1; i<=N; i++, puts("")) for(int j=1; j<=K; j++) printf("%5lld ",
  // Dy[i][j]); puts("");

  if (isColorful) {
    printf("%lld\n", Base);
  } else if (allDiff) {
    for (int i = 1; i <= N; i++) {
      for (int j = 1; j < K; j++) {
        G[i][j] += G[i - 1][j - 1] * (K - (j - 1));
        G[i][j] += SumG[i - 1][j];
        G[i][j] %= MOD;
      }
      for (int j = M; j < K; j++) {
        G[i][j] += Dy[i][j];
        G[i][j] %= MOD;
      }
      for (int j = K; j >= 1; j--) {
        SumG[i][j] = (SumG[i][j + 1] + G[i][j]) % MOD;
      }
    }

    ll mul = Fac[K - M] * Inv(Fac[K]) % MOD;
    ll ans = SumG[N][1] * mul % MOD;
    printf("%lld\n", ((Base - ans) % MOD + MOD) % MOD);
  } else {
    vector<int> cnt;
    cnt = vector<int>(K + 1, 0);
    int lix = -1, rix = -1;
    for (int i = 1; i <= M; i++) {
      cnt[Nr[i]]++;
      if (cnt[Nr[i]] == 2) {
        lix = i;
        break;
      }
    }
    cnt = vector<int>(K + 1, 0);
    for (int i = M; i >= 1; i--) {
      cnt[Nr[i]]++;
      if (cnt[Nr[i]] == 2) {
        rix = i;
        break;
      }
    }

    int lv = lix - 1;
    int rv = M - rix;
    ll mul = Fac[K - lv] * Fac[K - rv] % MOD;
    mul *= Inv(Fac[K]);
    mul %= MOD;
    mul *= Inv(Fac[K]);
    mul %= MOD;

    ll ans = 0;
    for (int i = 1; i <= N - M + 1; i++) {
      int l = i, r = i + M - 1;

      vector<pi> list;
      list.emplace_back(l, lix);
      list.emplace_back(N + 1 - r, N + 1 - rix);
      vector<ll> res;

      for (pi pp : list) {
        int start, end;
        tie(start, end) = pp;
        end--;
        int fix = end - start + 1;

        ll now = 0;
        for (int j = start; j >= 1; j--) {
          int k = end - j + 1;
          if (k >= K)
            break;
          now += Dy[end][k];
          now %= MOD;
        }

        res.push_back(now);
      }
      ans += res[0] * res[1] % MOD;
      ans %= MOD;

      lix++;
      rix++;
    }

    ans *= mul;
    ans %= MOD;
    printf("%lld\n", ((Base - ans) % MOD + MOD) % MOD);
  }
  return 0;
}


Fixed submission:
// Comment
// Hard. Think All - non colorful.
// And also Dp[i][j] =
// 		In interval [1, i],
// 		last j are pairly diff, last j+1 are not

#include <bits/stdc++.h>

using namespace std;

#define SZ(v) ((int)(v).size())
#define ALL(v) (v).begin(), (v).end()
#define one first
#define two second
using ll = long long;
using pi = pair<int, int>;
const int INF = 0x3f3f3f3f;
const ll LINF = 1ll * INF * INF;

const int MOD = 1e9 + 7;
const int MAX_N = 3e4 + 10;
const int MAX_K = 5e2 + 10;

int N, K, M;
int Nr[MAX_N];
ll Sum[MAX_N][MAX_K];
ll Dy[MAX_N][MAX_K];
ll SumG[MAX_N][MAX_K];
ll G[MAX_N][MAX_K];
ll Fac[MAX_N];

ll Base;
ll Pow(ll a, ll b) {
  ll p = a, r = 1;
  while (b) {
    if (b % 2)
      r *= p, r %= MOD;
    b /= 2;
    p *= p;
    p %= MOD;
  }
  return r;
}
ll Inv(ll a) { return Pow(a, MOD - 2); }

int main() {
  cin >> N >> K >> M;
  for (int i = 1; i <= M; i++)
    scanf("%d", &Nr[i]);
  Fac[0] = 1;
  for (int i = 1; i <= max(N, K); i++)
    Fac[i] = Fac[i - 1] * i % MOD;

  vector<int> cnt(K + 1, 0);
  for (int i = 1; i < K; i++)
    cnt[Nr[i]]++;
  bool isColorful = false;
  for (int i = K; i <= N; i++) {
    cnt[Nr[i]]++;
    bool isAll = true;
    for (int k = 1; k <= K; k++)
      if (cnt[k] == 0)
        isAll = false;
    if (isAll) {
      isColorful = true;
      break;
    }
    cnt[Nr[i - (K - 1)]]--;
  }

  cnt = vector<int>(K + 1, 0);
  for (int i = 1; i <= N; i++)
    cnt[Nr[i]]++;
  bool allDiff = true;
  for (int i = 1; i <= K; i++)
    if (cnt[i] >= 2)
      allDiff = false;

  Base = N - M + 1;
  for (int i = 0; i < N - M; i++)
    Base = Base * K % MOD;

  Dy[0][0] = 1;
  for (int i = 1; i <= N; i++) {
    for (int j = 1; j < K; j++) {
      Dy[i][j] += Dy[i - 1][j - 1] * (K - (j - 1));
      Dy[i][j] += Sum[i - 1][j];
      Dy[i][j] %= MOD;
    }
    for (int j = K; j >= 1; j--) {
      Sum[i][j] = (Sum[i][j + 1] + Dy[i][j]) % MOD;
    }
  }

  // for(int i=1; i<=N; i++, puts("")) for(int j=1; j<=K; j++) printf("%5lld ",
  // Dy[i][j]); puts("");

  if (isColorful) {
    printf("%lld\n", Base);
  } else if (allDiff) {
    for (int i = 1; i <= N; i++) {
      for (int j = 1; j < K; j++) {
        G[i][j] += G[i - 1][j - 1] * (K - (j - 1));
        G[i][j] += SumG[i - 1][j];
        G[i][j] %= MOD;
      }
      for (int j = M; j < K; j++) {
        G[i][j] += Dy[i][j];
        G[i][j] %= MOD;
      }
      for (int j = K; j >= 1; j--) {
        SumG[i][j] = (SumG[i][j + 1] + G[i][j]) % MOD;
      }
    }

    ll mul = Fac[K - M] * Inv(Fac[K]) % MOD;
    ll ans = SumG[N][1] * mul % MOD;
    printf("%lld\n", ((Base - ans) % MOD + MOD) % MOD);
  } else {
    vector<int> cnt;
    cnt = vector<int>(K + 1, 0);
    int lix = -1, rix = -1;
    for (int i = 1; i <= M; i++) {
      cnt[Nr[i]]++;
      if (cnt[Nr[i]] == 2) {
        lix = i;
        break;
      }
    }
    cnt = vector<int>(K + 1, 0);
    for (int i = M; i >= 1; i--) {
      cnt[Nr[i]]++;
      if (cnt[Nr[i]] == 2) {
        rix = i;
        break;
      }
    }

    int lv = lix - 1;
    int rv = M - rix;
    ll mul = Fac[K - lv] * Fac[K - rv] % MOD;
    mul *= Inv(Fac[K]);
    mul %= MOD;
    mul *= Inv(Fac[K]);
    mul %= MOD;

    ll ans = 0;
    for (int i = 1; i <= N - M + 1; i++) {
      int l = i, r = i + M - 1;

      vector<pi> list;
      list.emplace_back(l, lix);
      list.emplace_back(N + 1 - r, N + 1 - rix);
      vector<ll> res;

      for (pi pp : list) {
        int start, end;
        tie(start, end) = pp;
        end--;
        int fix = end - start + 1;

        ll now = 0;
        for (int j = start; j >= 1; j--) {
          int k = end - j + 1;
          if (k >= K)
            break;
          now += Dy[end][k];
          now %= MOD;
        }

        res.push_back(now);
      }
      ans += res[0] * res[1] % MOD;
      ans %= MOD;

      lix++;
      rix++;
    }

    ans *= mul;
    ans %= MOD;
    printf("%lld\n", ((Base - ans) % MOD + MOD) % MOD);
  }
  return 0;
}