#include #include #include #include // For potential exceptions, though not strictly needed by the final simple solution // Define the modulus const int MOD = 998244353; // Function to compute (base^exp) % mod using binary exponentiation long long power(long long base, long long exp) { long long res = 1; base %= MOD; while (exp > 0) { if (exp % 2 == 1) res = (res * base) % MOD; base = (base * base) % MOD; exp /= 2; } return res; } // Function to compute modular inverse using Fermat's Little Theorem // Assumes mod is prime and n is not a multiple of mod long long modInverse(long long n) { // Check for division by zero modulo MOD long long n_mod = n % MOD; if (n_mod < 0) n_mod += MOD; // Ensure n_mod is in [0, MOD-1] if (n_mod == 0) { // This case should ideally not happen in this problem logic unless K=1 and element=0? // Or if trying inverse logic that hits non-invertible cases. // The recompute logic avoids this issue. throw std::runtime_error("Division by zero modulo p"); } // Compute inverse using Fermat's Little Theorem: a^(p-2) = a^(-1) (mod p) for prime p return power(n_mod, MOD - 2); } int main() { // Use faster I/O operations std::ios_base::sync_with_stdio(false); std::cin.tie(NULL); int N; // Length of the sequence A int M; // Length of the subarrays (window size) int K; // Modulo value for the sum std::cin >> N >> M >> K; std::vector A(N); // Sequence A for (int i = 0; i < N; ++i) { std::cin >> A[i]; // Constraint check: 0 <= A[i] < K. This implies A[i] % K = A[i]. } // dp state vector: dp[j] stores the number of subsequences whose sum is congruent to j modulo K. // The size of the vector is K. std::vector dp(K); // Iterate through all possible starting positions k of the sliding window. // The window spans indices [k, k+M-1]. // k runs from 0 to N-M (0-based indexing). for (int k = 0; k <= N - M; ++k) { // Reset the dp state for each new window. // Initialize dp array with 0s. dp.assign(K, 0); // Base case: The empty subsequence has a sum of 0. Its count is 1. dp[0] = 1; // Process each element within the current window A[k...k+M-1]. for (int i = k; i < k + M; ++i) { // Store the current dp state because we need to read from it while updating. std::vector current_dp = dp; // Get the value of the current element A[i]. Since 0 <= A[i] < K, A[i] % K = A[i]. int x = A[i]; // Create a temporary vector to store the counts of subsequences formed by adding A[i]. // shifted_dp[j] will represent the count of subsequences from the previous state (using A[k..i-1]) // whose sum was (j - x) mod K. Adding A[i] to these results in sum j mod K. std::vector shifted_dp(K); for(int j=0; j