Problem p03314 (C++)

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

typedef long long ll;
typedef pair<ll, ll> P;

#define fr first
#define sc second

const ll MOD = 1000000007;

ll modpow(ll x, ll k) {
  if (k == 0)
    return 1;
  ll ret = modpow(x, k / 2);
  ret *= ret;
  ret %= MOD;
  if (k % 2 == 1) {
    ret *= x;
    ret %= MOD;
  }
  return ret;
}

int main() {
  ll n, k, m;
  ll a[402];
  scanf("%lld%lld%lld", &n, &k, &m);
  for (int i = 0; i < m; i++) {
    scanf("%lld", &a[i]);
  }

  bool col = false;
  for (int i = 0; i < m - k + 1; i++) {
    set<int> S;
    for (int j = 0; j < k; j++) {
      S.insert(a[i + j]);
    }
    col |= S.size() == k;
  }
  if (col) {
    cout << (n - m + 1) * modpow(k, n - m) % MOD << endl;
    return 0;
  }

  static ll dp[2][25010][402];
  static ll sc[2][25010][402];
  set<int> X;
  for (int i = 0; i < m; i++) {
    X.insert(a[i]);
  }
  if (X.size() == m) {
    for (int t = 0; t < 2; t++) {
      for (int i = 0; i < 25010; i++) {
        for (int j = 0; j < 402; j++) {
          dp[t][i][j] = sc[t][i][j] = 0;
        }
      }
    }
    dp[0][1][1] = k;
    for (int i = 1; i <= n; i++) {
      for (int t = 0; t < 2; t++)
        for (int j = m; j <= k; j++) {
          sc[t][i][j] += dp[t][i][j];
          if (sc[t][i][j] >= MOD)
            sc[t][i][j] -= MOD;
        }
      /*cout<<"====="<<endl<<i<<endl;
      for(int t=0;t<2;t++){
              for(int j=1;j<=k;j++){
                      cout<<dp[t][i][j]<<","<<sc[t][i][j]<<" ";
              }
              cout<<endl;
      }*/
      if (i == n)
        break;
      for (int t = 0; t < 2; t++)
        for (int j = 1; j < k; j++) {
          dp[t][i + 1][1] += dp[t][i][j];
          dp[t][i + 1][j + 1] += MOD - dp[t][i][j];
          ll x = dp[t][i][j] * (k - j);
          x %= MOD;
          dp[t][i + 1][j + 1] += x;
          dp[t][i + 1][j + 2] += MOD - x;
        }
      for (int t = 0; t < 2; t++)
        for (int j = 1; j <= k; j++) {
          dp[t][i + 1][j] += dp[t][i + 1][j - 1];
          dp[t][i + 1][j] %= MOD;
        }
      for (int t = 0; t < 2; t++) {
        dp[t][i + 1][k] += dp[t][i][k] * k;
        dp[t][i + 1][k] %= MOD;
      }
      dp[1][i + 1][k - 1] += dp[1][i + 1][k];
      dp[1][i + 1][k - 1] += dp[0][i + 1][k];
      dp[1][i + 1][k - 1] %= MOD;
      dp[0][i + 1][k] = dp[1][i + 1][k] = 0;
      for (int t = 0; t < 2; t++)
        for (int j = 1; j < k; j++) {
          sc[t][i + 1][1] += sc[t][i][j];
          sc[t][i + 1][j + 1] += MOD - sc[t][i][j];
          ll x = sc[t][i][j] * (k - j);
          x %= MOD;
          sc[t][i + 1][j + 1] += x;
          sc[t][i + 1][j + 2] += MOD - x;
        }
      for (int t = 0; t < 2; t++)
        for (int j = 1; j <= k; j++) {
          sc[t][i + 1][j] += sc[t][i + 1][j - 1];
          sc[t][i + 1][j] %= MOD;
        }
      for (int t = 0; t < 2; t++) {
        sc[t][i + 1][k] += sc[t][i][k] * k;
        sc[t][i + 1][k] %= MOD;
      }
      sc[1][i + 1][k - 1] += sc[1][i + 1][k];
      sc[1][i + 1][k - 1] += sc[0][i + 1][k];
      sc[1][i + 1][k - 1] %= MOD;
      sc[0][i + 1][k] = sc[1][i + 1][k] = 0;
    }
    ll ret = 0;
    for (int j = 1; j <= k; j++)
      ret += sc[1][n][j];
    ret %= MOD;
    // cout<<ret<<endl;
    for (int i = k - m + 1; i <= k; i++) {
      ret *= modpow(i, MOD - 2);
      ret %= MOD;
    }
    cout << ret << endl;
    return 0;
  }

  for (int t = 0; t < 2; t++) {
    set<int> S;
    for (int i = m - 1; i >= 0; i--) {
      if (S.find(a[i]) != S.end())
        break;
      S.insert(a[i]);
    }
    for (int i = 0; i < 25010; i++) {
      for (int j = 0; j < 402; j++) {
        dp[t][i][j] = 0;
      }
    }
    dp[t][0][S.size()] = 1;
    for (int i = 0; i + 1 < 25010; i++) {
      for (int j = 1; j < k; j++) {
        dp[t][i + 1][1] += dp[t][i][j];
        dp[t][i + 1][j + 1] += MOD - dp[t][i][j];
        ll x = dp[t][i][j] * (k - j);
        x %= MOD;
        dp[t][i + 1][j + 1] += x;
        dp[t][i + 1][j + 2] += MOD - x;
      }
      for (int j = 1; j <= k; j++) {
        dp[t][i + 1][j] += dp[t][i + 1][j - 1];
        dp[t][i + 1][j] %= MOD;
      }
      dp[t][i + 1][k] += dp[t][i][k] * k;
      dp[t][i + 1][k] %= MOD;
    }
    reverse(a, a + m);
  }

  ll ret = 0;
  for (int i = 0; i <= n - m; i++) {
    int j = n - m - i;
    // cout<<dp[0][i][k]<<" "<<dp[1][j][k]<<endl;
    ret += dp[0][i][k] * modpow(k, j);
    ret += dp[1][j][k] * modpow(k, i);
    ret += MOD - (dp[0][i][k] * dp[1][j][k] % MOD);
    ret %= MOD;
  }
  /*static ll DP[402];
  DP[0]=m;
  ll zan=k-m;
  for(int i=1;i<k-m;i++){
          DP[i]=zan*(m+i)%MOD;
          zan*=k-m-i; zan%=MOD;
  }
  for(int i=0;i<k-m;i++){
          zan=*/

  cout << ret % MOD << endl;
}


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

typedef long long ll;
typedef pair<ll, ll> P;

#define fr first
#define sc second

const ll MOD = 1000000007;

ll modpow(ll x, ll k) {
  if (k == 0)
    return 1;
  ll ret = modpow(x, k / 2);
  ret *= ret;
  ret %= MOD;
  if (k % 2 == 1) {
    ret *= x;
    ret %= MOD;
  }
  return ret;
}

int main() {
  static ll n, k, m;
  static ll a[25002];
  scanf("%lld%lld%lld", &n, &k, &m);
  for (int i = 0; i < m; i++) {
    scanf("%lld", &a[i]);
  }

  bool col = false;
  for (int i = 0; i < m - k + 1; i++) {
    set<int> S;
    for (int j = 0; j < k; j++) {
      S.insert(a[i + j]);
    }
    col |= S.size() == k;
  }
  if (col) {
    cout << (n - m + 1) * modpow(k, n - m) % MOD << endl;
    return 0;
  }

  static ll dp[2][25010][402];
  static ll sc[2][25010][402];
  set<int> X;
  for (int i = 0; i < m; i++) {
    X.insert(a[i]);
  }
  if (X.size() == m) {
    for (int t = 0; t < 2; t++) {
      for (int i = 0; i < 25010; i++) {
        for (int j = 0; j < 402; j++) {
          dp[t][i][j] = sc[t][i][j] = 0;
        }
      }
    }
    dp[0][1][1] = k;
    for (int i = 1; i <= n; i++) {
      for (int t = 0; t < 2; t++)
        for (int j = m; j <= k; j++) {
          sc[t][i][j] += dp[t][i][j];
          if (sc[t][i][j] >= MOD)
            sc[t][i][j] -= MOD;
        }
      /*cout<<"====="<<endl<<i<<endl;
      for(int t=0;t<2;t++){
              for(int j=1;j<=k;j++){
                      cout<<dp[t][i][j]<<","<<sc[t][i][j]<<" ";
              }
              cout<<endl;
      }*/
      if (i == n)
        break;
      for (int t = 0; t < 2; t++)
        for (int j = 1; j < k; j++) {
          dp[t][i + 1][1] += dp[t][i][j];
          dp[t][i + 1][j + 1] += MOD - dp[t][i][j];
          ll x = dp[t][i][j] * (k - j);
          x %= MOD;
          dp[t][i + 1][j + 1] += x;
          dp[t][i + 1][j + 2] += MOD - x;
        }
      for (int t = 0; t < 2; t++)
        for (int j = 1; j <= k; j++) {
          dp[t][i + 1][j] += dp[t][i + 1][j - 1];
          dp[t][i + 1][j] %= MOD;
        }
      for (int t = 0; t < 2; t++) {
        dp[t][i + 1][k] += dp[t][i][k] * k;
        dp[t][i + 1][k] %= MOD;
      }
      dp[1][i + 1][k - 1] += dp[1][i + 1][k];
      dp[1][i + 1][k - 1] += dp[0][i + 1][k];
      dp[1][i + 1][k - 1] %= MOD;
      dp[0][i + 1][k] = dp[1][i + 1][k] = 0;
      for (int t = 0; t < 2; t++)
        for (int j = 1; j < k; j++) {
          sc[t][i + 1][1] += sc[t][i][j];
          sc[t][i + 1][j + 1] += MOD - sc[t][i][j];
          ll x = sc[t][i][j] * (k - j);
          x %= MOD;
          sc[t][i + 1][j + 1] += x;
          sc[t][i + 1][j + 2] += MOD - x;
        }
      for (int t = 0; t < 2; t++)
        for (int j = 1; j <= k; j++) {
          sc[t][i + 1][j] += sc[t][i + 1][j - 1];
          sc[t][i + 1][j] %= MOD;
        }
      for (int t = 0; t < 2; t++) {
        sc[t][i + 1][k] += sc[t][i][k] * k;
        sc[t][i + 1][k] %= MOD;
      }
      sc[1][i + 1][k - 1] += sc[1][i + 1][k];
      sc[1][i + 1][k - 1] += sc[0][i + 1][k];
      sc[1][i + 1][k - 1] %= MOD;
      sc[0][i + 1][k] = sc[1][i + 1][k] = 0;
    }
    ll ret = 0;
    for (int j = 1; j <= k; j++)
      ret += sc[1][n][j];
    ret %= MOD;
    // cout<<ret<<endl;
    for (int i = k - m + 1; i <= k; i++) {
      ret *= modpow(i, MOD - 2);
      ret %= MOD;
    }
    cout << ret << endl;
    return 0;
  }

  for (int t = 0; t < 2; t++) {
    set<int> S;
    for (int i = m - 1; i >= 0; i--) {
      if (S.find(a[i]) != S.end())
        break;
      S.insert(a[i]);
    }
    for (int i = 0; i < 25010; i++) {
      for (int j = 0; j < 402; j++) {
        dp[t][i][j] = 0;
      }
    }
    dp[t][0][S.size()] = 1;
    for (int i = 0; i + 1 < 25010; i++) {
      for (int j = 1; j < k; j++) {
        dp[t][i + 1][1] += dp[t][i][j];
        dp[t][i + 1][j + 1] += MOD - dp[t][i][j];
        ll x = dp[t][i][j] * (k - j);
        x %= MOD;
        dp[t][i + 1][j + 1] += x;
        dp[t][i + 1][j + 2] += MOD - x;
      }
      for (int j = 1; j <= k; j++) {
        dp[t][i + 1][j] += dp[t][i + 1][j - 1];
        dp[t][i + 1][j] %= MOD;
      }
      dp[t][i + 1][k] += dp[t][i][k] * k;
      dp[t][i + 1][k] %= MOD;
    }
    reverse(a, a + m);
  }

  ll ret = 0;
  for (int i = 0; i <= n - m; i++) {
    int j = n - m - i;
    // cout<<dp[0][i][k]<<" "<<dp[1][j][k]<<endl;
    ret += dp[0][i][k] * modpow(k, j);
    ret += dp[1][j][k] * modpow(k, i);
    ret += MOD - (dp[0][i][k] * dp[1][j][k] % MOD);
    ret %= MOD;
  }
  /*static ll DP[402];
  DP[0]=m;
  ll zan=k-m;
  for(int i=1;i<k-m;i++){
          DP[i]=zan*(m+i)%MOD;
          zan*=k-m-i; zan%=MOD;
  }
  for(int i=0;i<k-m;i++){
          zan=*/

  cout << ret % MOD << endl;
}