Problem p03314 (C++)

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

using namespace std;

const int N = 25000 + 5;
const int M = 400 + 5;
const int mod = (int)1e9 + 7;

inline int mul(int a, int b) { return (long long)a * b % mod; }
inline void sub(int &a, int b) {
  a -= b;
  if (a < 0) {
    a += mod;
  }
}
inline void add(int &a, int b) {
  a += b;
  if (a >= mod) {
    a -= mod;
  }
}
inline int modexp(int a, int x) {
  int res = 1;
  while (x) {
    if (x & 1) {
      res = mul(res, a);
    }
    a = mul(a, a);
    x >>= 1;
  }
  return res;
}

int n, m, k;
int a[N];
int fac[N];
int inv[N];

int dp[N][M];
int sum[N][M];

inline int getsum(int r, int ll, int rr) {
  int res = sum[r][rr] - sum[r][ll - 1];
  return res < 0 ? res + mod : res;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> n >> k >> m;
  int ans = mul(n - m + 1, modexp(k, n - m));

  map<int, int> mp;
  for (int i = 0; i < m; ++i) {
    cin >> a[i];
    ++mp[a[i]];
  }

  bool dis = 1;
  for (int i = 1; i <= k; ++i) {
    if (mp[i] > 1) {
      dis = 0;
    }
  }

  mp.clear();
  int num = 0;
  bool ok = 0;
  if (m >= k) {
    for (int i = 0; i < k; ++i) {
      num += mp[a[i]] == 0;
      ++mp[a[i]];
    }

    if (num != k) {
      for (int i = k; i < m; ++i) {
        num -= mp[a[i - k]] == 1;
        --mp[a[i - k]];
        num += mp[a[i]] == 0;
        ++mp[a[i]];
        if (num == k) {
          ok = 1;
          break;
        }
      }
    } else {
      ok = 1;
    }
  }

  if (ok) {
    cout << ans << endl;
    return 0;
  }

  fac[0] = 1;
  for (int i = 1; i < N; ++i) {
    fac[i] = mul(fac[i - 1], i);
  }
  inv[N - 1] = modexp(fac[N - 1], mod - 2);
  for (int i = N - 2; i >= 0; --i) {
    inv[i] = mul(inv[i + 1], i + 1);
  }

  dp[1][1] = k;

  for (int i = 2; i <= n; ++i) {
    int sum = 0;
    for (int j = k - 1; j > 0; --j) {
      add(sum, dp[i - 1][j]);
      add(dp[i][j], mul(k - (j - 1), dp[i - 1][j - 1]));
      add(dp[i][j], sum);
    }
  }
  for (int i = 1; i <= n; ++i) {
    for (int j = 1; j < k; ++j) {
      sum[i][j] = (sum[i][j - 1] + dp[i][j]) % mod;
    }
  }

  if (!dis) {
    int bndl = 0;
    int bndr = 0;

    mp.clear();
    for (int i = 0; i < m; ++i) {
      if (mp[a[i]]) {
        bndl = i;
        break;
      } else {
        mp[a[i]] = 1;
      }
    }

    mp.clear();
    for (int i = m - 1; i >= 0; --i) {
      if (mp[a[i]]) {
        bndr = m - i - 1;
        break;
      } else {
        mp[a[i]] = 1;
      }
    }

    int dtl = modexp(mul(fac[k], modexp(fac[k - bndl], mod - 2)), mod - 2);
    int dtr = modexp(mul(fac[k], modexp(fac[k - bndr], mod - 2)), mod - 2);
    for (int i = 0; i <= n - m; ++i) {
      int lenl = i + bndl;
      int lenr = n - m - i + bndr;
      sub(ans, mul(mul(getsum(lenl, bndl, min(k - 1, lenl)),
                       getsum(lenr, bndr, min(k - 1, lenr))),
                   mul(dtl, dtr)));
    }

    cout << ans << endl;

  } else {

    for (int i = 0; i <= n - m; ++i) {

      static int f[N];
      for (int l = 0; l <= k - m; ++l) {
        int a = mul(fac[k - m - l], inv[k]);
        if (l != 0) {
          f[l] = f[l - 1];
        } else {
          f[l] = 0;
        }
        add(f[l], mul(a, dp[i + l + m][l + m]));
      }

      for (int j = n - i - k + 1; j <= n - i - m; ++j) {
        if (n - i - j >= k) {
          break;
        }
        int a = mul(fac[k - m], inv[k]);
        sub(ans, mul(mul(a, dp[n - i][n - i - j]), f[n - i - m - j]));
      }
    }

    cout << ans << endl;
  }

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>

using namespace std;

const int N = 25000 + 5;
const int M = 400 + 5;
const int mod = (int)1e9 + 7;

inline int mul(int a, int b) { return (long long)a * b % mod; }
inline void sub(int &a, int b) {
  a -= b;
  if (a < 0) {
    a += mod;
  }
}
inline void add(int &a, int b) {
  a += b;
  if (a >= mod) {
    a -= mod;
  }
}
inline int modexp(int a, int x) {
  int res = 1;
  while (x) {
    if (x & 1) {
      res = mul(res, a);
    }
    a = mul(a, a);
    x >>= 1;
  }
  return res;
}

int n, m, k;
int a[N];
int fac[N];
int inv[N];

int dp[N][M];
int sum[N][M];

inline int getsum(int r, int ll, int rr) {
  int res = sum[r][rr] - sum[r][ll - 1];
  return res < 0 ? res + mod : res;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> n >> k >> m;
  int ans = mul(n - m + 1, modexp(k, n - m));

  map<int, int> mp;
  for (int i = 0; i < m; ++i) {
    cin >> a[i];
    ++mp[a[i]];
  }

  bool dis = 1;
  for (int i = 1; i <= k; ++i) {
    if (mp[i] > 1) {
      dis = 0;
    }
  }

  mp.clear();
  int num = 0;
  bool ok = 0;
  if (m >= k) {
    for (int i = 0; i < k; ++i) {
      num += mp[a[i]] == 0;
      ++mp[a[i]];
    }

    if (num != k) {
      for (int i = k; i < m; ++i) {
        num -= mp[a[i - k]] == 1;
        --mp[a[i - k]];
        num += mp[a[i]] == 0;
        ++mp[a[i]];
        if (num == k) {
          ok = 1;
          break;
        }
      }
    } else {
      ok = 1;
    }
  }

  if (ok) {
    cout << ans << endl;
    return 0;
  }

  fac[0] = 1;
  for (int i = 1; i < N; ++i) {
    fac[i] = mul(fac[i - 1], i);
  }
  inv[N - 1] = modexp(fac[N - 1], mod - 2);
  for (int i = N - 2; i >= 0; --i) {
    inv[i] = mul(inv[i + 1], i + 1);
  }

  dp[1][1] = k;

  for (int i = 2; i <= n; ++i) {
    int sum = 0;
    for (int j = k - 1; j > 0; --j) {
      add(sum, dp[i - 1][j]);
      add(dp[i][j], mul(k - (j - 1), dp[i - 1][j - 1]));
      add(dp[i][j], sum);
    }
  }
  for (int i = 1; i <= n; ++i) {
    for (int j = 1; j < k; ++j) {
      sum[i][j] = (sum[i][j - 1] + dp[i][j]) % mod;
    }
  }

  if (!dis) {
    int bndl = 0;
    int bndr = 0;

    mp.clear();
    for (int i = 0; i < m; ++i) {
      if (mp[a[i]]) {
        bndl = i;
        break;
      } else {
        mp[a[i]] = 1;
      }
    }

    mp.clear();
    for (int i = m - 1; i >= 0; --i) {
      if (mp[a[i]]) {
        bndr = m - i - 1;
        break;
      } else {
        mp[a[i]] = 1;
      }
    }

    int dtl = modexp(mul(fac[k], modexp(fac[k - bndl], mod - 2)), mod - 2);
    int dtr = modexp(mul(fac[k], modexp(fac[k - bndr], mod - 2)), mod - 2);
    for (int i = 0; i <= n - m; ++i) {
      int lenl = i + bndl;
      int lenr = n - m - i + bndr;
      sub(ans, mul(mul(getsum(lenl, bndl, min(k - 1, lenl)),
                       getsum(lenr, bndr, min(k - 1, lenr))),
                   mul(dtl, dtr)));
    }

    cout << ans << endl;

  } else {

    for (int i = 0; i <= n - m; ++i) {

      static int f[N];
      for (int l = 0; l <= k - m && l <= n - m - i; ++l) {
        int a = mul(fac[k - m - l], inv[k]);
        if (l != 0) {
          f[l] = f[l - 1];
        } else {
          f[l] = 0;
        }
        add(f[l], mul(a, dp[i + l + m][l + m]));
      }

      for (int j = n - i - k + 1; j <= n - i - m; ++j) {
        if (n - i - j >= k) {
          break;
        }
        int a = mul(fac[k - m], inv[k]);
        sub(ans, mul(mul(a, dp[n - i][n - i - j]), f[n - i - m - j]));
      }
    }

    cout << ans << endl;
  }

  return 0;
}