#include using namespace std; #include #include using namespace __gnu_pbds; template using ordered_set = tree, rb_tree_tag, tree_order_statistics_node_update>; typedef long long ll; typedef long double ld; typedef pair pl; typedef vector vl; #define sl(x) scanf("%d",&x) #define pl(x) printf("%d\n",x) #define sz(x) ((int)x.size()) #define s(x) sort(x.begin(),x.end()) #define all(v) v.begin(),v.end() #define debug(x) cerr << #x << " " << x << "\n"; #define debug2(x, n) cerr << #x << "\n"; f(i, n) cerr << i << " " << x[i] << "\n"; #define r(v) {reverse(all(v));} #define pb push_back #define F first #define S second #define f(i,n) for(int i=0;i T power2(T a, ll b) { if(b == 0) return 1; if(b == 1) return a; T x = power2(a, b / 2); x = x * x; if(b % 2) x = x * a; return x;} ll gcd(ll a, ll b) {if(a < b) swap(a, b); while(b) {ll t = b; b = a % b; a = t;} return a;} ll mul(ll a, ll b, ll m = mod) { return (ll)(a * b) % m;} ll add(ll a, ll b, ll m = mod) { a += b; if(a >= m) a -= m; if(a < 0) a += m; return a;} ll power(ll a, ll b, ll m = mod) { if(b == 0) return 1; if(b == 1) return (a % m); ll x = power(a, b / 2, m); x = mul(x, x, m); if(b % 2) x = mul(x, a, m); return x;} ll n, k, x; ll l = 0; void solve(ll i, vector v) { if(i == n - 2) { if(v[0] != 1 && v.back() != x) l++; return; } for(ll j = 1; j <= k; j++) { if(sz(v) && j == v.back()) continue; v.pb(j); solve(i + 1, v); v.pop_back(); } } void brute() { vector v; l = 0; solve(0, v); cout << l << "\n"; } ll a[N]; void st() { cin >> n >> k >> x; if(k == 2) { if(x == 1) { if(n % 2) cout << "1\n"; else cout << "0\n"; } else { if(n % 2) cout << "0\n"; else cout << "1\n"; } //brute(); return ; } /*if(n == 3) { if(x == 1) cout << k - 1 << "\n"; else cout << k - 2 << "\n"; return 0; }*/ a[3] = k - 2; a[2] = 1; for(ll j = 4; j <= n; j++) { a[j] = add(mul(k - 2, a[j - 1]), mul(k - 1, a[j - 2])); } ll ans = a[n]; if(x == 1) { if(n & 1) ans = add(ans, 1); else ans = add(ans, mod - 1); } cout << ans << "\n"; //brute(); } int main() { ios_base::sync_with_stdio(0); ll t = 1; while(t--) st(); return 0; }