#include #include using namespace std; long countArray(int n, int k, int x, vector& cacheA, vector& cacheB) { // Return the number of ways to fill in the array. long modulo = pow(10, 9) + 7; if (n == 3) { if (x == 1) return (k-1); return k-2; } if (x != 1 && cacheA[n] != -1) return cacheA[n]; if (x == 1 && cacheB[n] != -1) return cacheB[n]; long total = 0; if (x != 1) total += countArray(n-1, k, 1, cacheA, cacheB); total += (x == 1 ? k-1 : k-2) * (countArray(n-1, k, 2, cacheA, cacheB) % modulo); total %= modulo; if (x == 1) cacheB[n] = total; if (x != 1) cacheA[n] = total; return total; } int main() { int n; int k; int x; cin >> n >> k >> x; vector cacheA(n+1, -1); vector cacheB(n+1, -1); long answer = countArray(n, k, x, cacheA, cacheB); cout << answer << endl; return 0; }