Problem p03314 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
typedef long long int ll;
typedef long double ld;
#define pb push_back
#define pii pair<int, int>
#define F first
#define S second
#define endl '\n'
#define int long long
#define sync                                                                   \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(0);                                                                  \
  cout.tie(0)
#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math")
#define kill(x) return cout << x << '\n', 0;
using namespace std;
const int N = 25e3 + 10, K = 410, mod = 1e9 + 7;
ll dp[N][K], d[N][K][2], sum[N][K][2];
ll par[N][K], P[N][K][2], Pa[N][K][2];
ll a[N];
ll vis[K];
ll power(ll n, ll k) {
  if (k == 0)
    return 1;
  if (k % 2 == 1) {
    ll x = power(n, k / 2);
    return x * x % mod * n % mod;
  }
  ll x = power(n, k / 2);
  return x * x % mod;
}
int32_t main() {
  ll n, k, m;
  cin >> n >> k >> m;
  for (int i = 1; i <= m; i++)
    cin >> a[i];
  ll pl = 0, pr = 0;
  for (int i = 1; i <= m; i++) {
    ll p1 = 0;
    memset(vis, 0, sizeof vis);
    for (int j = 1; j <= i; j++) {
      vis[a[j]]++;
      if (vis[a[j]] == 2)
        p1 = 1;
    }
    if (!p1)
      pl = i;
  }
  reverse(a + 1, a + m + 1);
  for (int i = 1; i <= m; i++) {
    ll p1 = 0;
    memset(vis, 0, sizeof vis);
    for (int j = 1; j <= i; j++) {
      vis[a[j]]++;
      if (vis[a[j]] == 2)
        p1 = 1;
    }
    if (!p1)
      pr = i;
  }
  if (pl == m && m < k) {
    cout << 1 / 0;
    for (int i = 1; i <= k; i++)
      d[0][i][1] = 1, P[0][i][1] = i;
    for (int i = m; i <= k; i++) {
      sum[0][i][1] = 1;
      Pa[0][i][1] = Pa[0][i - 1][1] + sum[0][i][1];
    }
    for (int i = 1; i <= n; i++) {
      for (int j = 1; j < k; j++) {
        d[i][j][0] = d[i - 1][j + 1][0] * (k - j) % mod;
        sum[i][j][0] = sum[i - 1][j + 1][0] * (k - j) % mod;
        d[i][j][0] += P[i - 1][j][0];
        sum[i][j][0] += Pa[i - 1][j][0];
        sum[i][j][0] %= mod;
        d[i][j][0] %= mod;
        if (j == k - 1) {
          d[i][j][0] += d[i - 1][j + 1][1] * (k - j) % mod;
          d[i][j][0] %= mod;
          sum[i][j][0] += sum[i - 1][j + 1][1] * (k - j) % mod;
          sum[i][j][0] %= mod;
        }
      }
      for (int j = 1; j <= k; j++) {
        d[i][j][1] = d[i - 1][j + 1][1] * (k - j) % mod;
        sum[i][j][1] = sum[i - 1][j + 1][1] * (k - j) % mod;
        d[i][j][1] += P[i - 1][j][1];
        sum[i][j][1] += Pa[i - 1][j][1];
        sum[i][j][1] %= mod;
        d[i][j][1] %= mod;
      }
      for (int j = m; j <= k; j++) {
        for (int y = 0; y < 2; y++) {
          sum[i][j][y] += d[i][j][y];
          sum[i][j][y] %= mod;
        }
      }
      for (int j = 1; j <= k; j++) {
        // cout << i << " " << j << " " << sum[i][j][0] << " " << sum[i][j][1]
        // << endl;
        for (int y = 0; y < 2; y++) {
          P[i][j][y] = P[i][j - 1][y] + d[i][j][y];
          P[i][j][y] %= mod;
          Pa[i][j][y] = Pa[i][j - 1][y] + sum[i][j][y];
          Pa[i][j][y] %= mod;
        }
      }
    }
    ll ans = sum[n - 1][1][0] * k % mod;
    // cout << sum[n-1][1][0]*k << endl;
    ll o = 1;
    for (int i = k; i > k - m; i--) {
      o *= i;
      o %= mod;
    }
    cout << ans * power(o, mod - 2) % mod << endl;
    return 0;
  }
  for (int i = 1; i < k; i++)
    dp[0][i] = 1, par[0][i] = i;
  for (int i = 1; i < N; i++) {
    for (int j = 1; j < k; j++) {
      dp[i][j] = dp[i - 1][j + 1] * (k - j) % mod;
      dp[i][j] += par[i - 1][j];
      dp[i][j] %= mod;
    }
    for (int j = 1; j < k; j++) {
      par[i][j] = par[i][j - 1] + dp[i][j];
      par[i][j] %= mod;
    }
  }
  ll p1 = 0;
  for (int i = 1; i <= m - k + 1; i++) {
    memset(vis, 0, sizeof vis);
    ll p2 = 0;
    for (int j = i; j < i + k; j++) {
      vis[a[j]]++;
      if (vis[a[j]] == 2)
        p2 = 1;
    }
    if (!p2)
      p1 = 1;
  }

  ll po = 1;
  for (int i = 0; i < n - m; i++) {
    po *= k;
    po %= mod;
  }
  if (p1) {
    ll ans = (n - m + 1) * po % mod;
    kill(ans);
    return 0;
  }

  po *= (n - m + 1);
  po %= mod;

  ll ans = 0;
  // cout << pl << " "  << pr << endl;
  for (int i = 1; i <= n - m + 1; i++) {
    ll x1 = i - 1, x2 = n - (i + m - 1);
    ans += dp[x1][pl] * dp[x2][pr] % mod;
    ans %= mod;
    //  cout << x1 << " " << x2 << " " << dp[x1][pl] << " " << dp[x2][pr] <<
    //  endl;
  }
  po -= ans;
  po += mod;
  cout << po % mod << endl;
}


Fixed submission:
#include <bits/stdc++.h>
typedef long long int ll;
typedef long double ld;
#define pb push_back
#define pii pair<int, int>
#define F first
#define S second
#define endl '\n'
#define int long long
#define sync                                                                   \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(0);                                                                  \
  cout.tie(0)
#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math")
#define kill(x) return cout << x << '\n', 0;
using namespace std;
const int N = 25e3 + 10, K = 410, mod = 1e9 + 7;
ll dp[N][K], d[N][K][2], sum[N][K][2];
ll par[N][K], P[N][K][2], Pa[N][K][2];
ll a[N];
ll vis[K];
ll power(ll n, ll k) {
  if (k == 0)
    return 1;
  if (k % 2 == 1) {
    ll x = power(n, k / 2);
    return x * x % mod * n % mod;
  }
  ll x = power(n, k / 2);
  return x * x % mod;
}
int32_t main() {
  ll n, k, m;
  cin >> n >> k >> m;
  for (int i = 1; i <= m; i++)
    cin >> a[i];
  ll pl = 0, pr = 0;
  for (int i = 1; i <= m; i++) {
    ll p1 = 0;
    memset(vis, 0, sizeof vis);
    for (int j = 1; j <= i; j++) {
      vis[a[j]]++;
      if (vis[a[j]] == 2)
        p1 = 1;
    }
    if (!p1)
      pl = i;
  }
  reverse(a + 1, a + m + 1);
  for (int i = 1; i <= m; i++) {
    ll p1 = 0;
    memset(vis, 0, sizeof vis);
    for (int j = 1; j <= i; j++) {
      vis[a[j]]++;
      if (vis[a[j]] == 2)
        p1 = 1;
    }
    if (!p1)
      pr = i;
  }
  if (pl == m && m < k) {
    // cout << 1/0;
    for (int i = 1; i <= k; i++)
      d[0][i][1] = 1, P[0][i][1] = i;
    for (int i = m; i <= k; i++) {
      sum[0][i][1] = 1;
      Pa[0][i][1] = Pa[0][i - 1][1] + sum[0][i][1];
    }
    for (int i = 1; i <= n; i++) {
      for (int j = 1; j < k; j++) {
        d[i][j][0] = d[i - 1][j + 1][0] * (k - j) % mod;
        sum[i][j][0] = sum[i - 1][j + 1][0] * (k - j) % mod;
        d[i][j][0] += P[i - 1][j][0];
        sum[i][j][0] += Pa[i - 1][j][0];
        sum[i][j][0] %= mod;
        d[i][j][0] %= mod;
        if (j == k - 1) {
          d[i][j][0] += d[i - 1][j + 1][1] * (k - j) % mod;
          d[i][j][0] %= mod;
          sum[i][j][0] += sum[i - 1][j + 1][1] * (k - j) % mod;
          sum[i][j][0] %= mod;
        }
      }
      for (int j = 1; j <= k; j++) {
        d[i][j][1] = d[i - 1][j + 1][1] * (k - j) % mod;
        sum[i][j][1] = sum[i - 1][j + 1][1] * (k - j) % mod;
        d[i][j][1] += P[i - 1][j][1];
        sum[i][j][1] += Pa[i - 1][j][1];
        sum[i][j][1] %= mod;
        d[i][j][1] %= mod;
      }
      for (int j = m; j <= k; j++) {
        for (int y = 0; y < 2; y++) {
          sum[i][j][y] += d[i][j][y];
          sum[i][j][y] %= mod;
        }
      }
      for (int j = 1; j <= k; j++) {
        // cout << i << " " << j << " " << sum[i][j][0] << " " << sum[i][j][1]
        // << endl;
        for (int y = 0; y < 2; y++) {
          P[i][j][y] = P[i][j - 1][y] + d[i][j][y];
          P[i][j][y] %= mod;
          Pa[i][j][y] = Pa[i][j - 1][y] + sum[i][j][y];
          Pa[i][j][y] %= mod;
        }
      }
    }
    ll ans = sum[n - 1][1][0] * k % mod;
    // cout << sum[n-1][1][0]*k << endl;
    ll o = 1;
    for (int i = k; i > k - m; i--) {
      o *= i;
      o %= mod;
    }
    cout << ans * power(o, mod - 2) % mod << endl;
    return 0;
  }
  for (int i = 1; i < k; i++)
    dp[0][i] = 1, par[0][i] = i;
  for (int i = 1; i < N; i++) {
    for (int j = 1; j < k; j++) {
      dp[i][j] = dp[i - 1][j + 1] * (k - j) % mod;
      dp[i][j] += par[i - 1][j];
      dp[i][j] %= mod;
    }
    for (int j = 1; j < k; j++) {
      par[i][j] = par[i][j - 1] + dp[i][j];
      par[i][j] %= mod;
    }
  }
  ll p1 = 0;
  for (int i = 1; i <= m - k + 1; i++) {
    memset(vis, 0, sizeof vis);
    ll p2 = 0;
    for (int j = i; j < i + k; j++) {
      vis[a[j]]++;
      if (vis[a[j]] == 2)
        p2 = 1;
    }
    if (!p2)
      p1 = 1;
  }

  ll po = 1;
  for (int i = 0; i < n - m; i++) {
    po *= k;
    po %= mod;
  }
  if (p1) {
    ll ans = (n - m + 1) * po % mod;
    kill(ans);
    return 0;
  }

  po *= (n - m + 1);
  po %= mod;

  ll ans = 0;
  // cout << pl << " "  << pr << endl;
  for (int i = 1; i <= n - m + 1; i++) {
    ll x1 = i - 1, x2 = n - (i + m - 1);
    ans += dp[x1][pl] * dp[x2][pr] % mod;
    ans %= mod;
    //  cout << x1 << " " << x2 << " " << dp[x1][pl] << " " << dp[x2][pr] <<
    //  endl;
  }
  po -= ans;
  po += mod;
  cout << po % mod << endl;
}