//g++ -std=c++14 test.cpp -o test.out

#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <cassert>
#include <algorithm>
#include <functional>
#include <iostream>
#include <iomanip>
#include <vector>
#include <queue>
#include <stack>
#include <map> 
#include <unordered_map> 
#include <utility>
#include <limits.h>
#include <bitset>
#include <set>
using namespace std;
 
#define LL long long int
const LL INF = (1LL<<60);
const int INF_INT = 2147483647-1e6-1;
const LL mod = 1000000007ll;
const int mod_int = 1000000007;

//https://github.com/atcoder-live/library/blob/master/mint.cpp
struct mint {
  LL x; 
  mint(LL x=0):x((x%mod+mod)%mod){}
  mint& operator+=(const mint a) {
    if ((x += a.x) >= mod) x -= mod;
    return *this;
  }
  mint& operator-=(const mint a) {
    if ((x += mod-a.x) >= mod) x -= mod;
    return *this;
  }
  mint& operator*=(const mint a) {
    (x *= a.x) %= mod;
    return *this;
  }
  mint operator+(const mint a) const {
    mint res(*this);
    return res+=a;
  }
  mint operator-(const mint a) const {
    mint res(*this);
    return res-=a;
  }
  mint operator*(const mint a) const {
    mint res(*this);
    return res*=a;
  }
  mint pow(LL t) const {
    if (!t) return 1;
    mint a = pow(t>>1);
    a *= a;
    if (t&1) a *= *this;
    return a;
  }
 
  // for prime mod
  mint inv() const {
    return pow(mod-2);
  }
  mint& operator/=(const mint a) {
    return (*this) *= a.inv();
  }
  mint operator/(const mint a) const {
    mint res(*this);
    return res/=a;
  }
};
 
struct combination {
  vector<mint> fact, ifact;//factに階乗を計算、ifactはその各々に対する逆元
  combination(LL n):fact(n+1),ifact(n+1) {
    assert(n < mod);
    fact[0] = 1;
    for (LL i = 1; i <= n; ++i) fact[i] = fact[i-1]*i;
    ifact[n] = fact[n].inv();
    for (LL i = n; i >= 1; --i) ifact[i-1] = ifact[i]*i;
  }
  mint operator()(LL n,LL k) {
    if (k < 0 || k > n) return 0;
    return fact[n]*ifact[k]*ifact[n-k];
  }
};
 
LL N;
char CAA,CAB,CBA,CBB;

mint ans = 1;

mint dp(){
    //dp[i][c]:
    //i文字目までカウント、最後に使った文字がc
    vector<map<char,mint>> dp((N-3)+1);
    dp[0]['A'] = 1;
    dp[0]['B'] = 1;//Bは2文字以上連続しない
    for(int i=0;i<N-3;i++){
        dp[i+1]['A'] = (dp[i]['A'] + dp[i]['B']);
        dp[i+1]['B'] = dp[i]['A'];
    }
    return dp[N-3]['A'];
}
void solve(){
    if(N<=3) return;
    if(CAB=='A'){
        if(CAA=='A') return;
        if(CAA=='B'){
            if(CBA=='A'){
                ans = dp();
                return;
            }
            if(CBA=='B'){
                ans = mint(2).pow(N-3);
                return;
            }
        }
    }
    if(CAB=='B'){
        if(CBB=='B') return;
        if(CBB=='A'){
            if(CBA=='B'){
                ans = dp();
                return;
            }
            if(CBA=='A'){
                ans = mint(2).pow(N-3);
                return;
            }
        }
    }
}
 
int main(){
    cin >> N;
    cin >> CAA >> CAB >> CBA >> CBB;
 
    solve();
 
    cout << ans.x << endl;
    return 0;
}
#include <bits/stdc++.h>
using namespace std;

const int64_t MOD = 1e9 + 7;
const int64_t MOD2 = 998244353;
const int INF = 2e9;
const long long inf = 1LL<<62;

template<int MOD> struct ModInt {
    static const int Mod = MOD; unsigned x; ModInt() : x(0) { }
    ModInt(signed sig) { x = sig < 0 ? sig % MOD + MOD : sig % MOD; }
    ModInt(signed long long sig) { x = sig < 0 ? sig % MOD + MOD : sig % MOD; }
    int get() const { return (int)x; }
    ModInt &operator+=(ModInt that) { if ((x += that.x) >= MOD) x -= MOD; return *this; }
    ModInt &operator-=(ModInt that) { if ((x += MOD - that.x) >= MOD) x -= MOD; return *this; }
    ModInt &operator*=(ModInt that) { x = (unsigned long long)x * that.x % MOD; return *this; }
    ModInt &operator/=(ModInt that) { return *this *= that.inverse(); }
    ModInt operator+(ModInt that) const { return ModInt(*this) += that; }
    ModInt operator-(ModInt that) const { return ModInt(*this) -= that; }
    ModInt operator*(ModInt that) const { return ModInt(*this) *= that; }
    ModInt operator/(ModInt that) const { return ModInt(*this) /= that; }
    ModInt inverse() const { long long a = x, b = MOD, u = 1, v = 0;
        while (b) { long long t = a / b; a -= t * b; std::swap(a, b); u -= t * v; std::swap(u, v); }
        return ModInt(u); }
    bool operator==(ModInt that) const { return x == that.x; }
    bool operator!=(ModInt that) const { return x != that.x; }
    ModInt operator-() const { ModInt t; t.x = x == 0 ? 0 : Mod - x; return t; }
};
template<int MOD> ostream& operator<<(ostream& st, const ModInt<MOD> a) { st << a.get(); return st; };
template<int MOD> ModInt<MOD> operator^(ModInt<MOD> a, unsigned long long k) {
    ModInt<MOD> r = 1; while (k) { if (k & 1) r *= a; a *= a; k >>= 1; } return r; }
typedef ModInt<998244353> mint;

int64_t po(int64_t a, int64_t b) {
  int64_t ans = 1;
  while (b > 0) {
    if (b%2) {
      ans = (ans*a)%MOD2;
    }
    a = a*a%MOD2;
    b /= 2;
  }
  return ans;
}

int main() {
  int n, m;
  cin >> n >> m;
  mint ans = (mint(m)^n)*(mint(n));
  for (int l=2; l<=n; l++) {
    for (int x = 1; x<=m; x++) {
      ans -= (mint(m-x)^(l-2))*(mint(m)^(n-l))*mint(n-l+1);
    }
  }
  cout << ans << endl;
}
