#include <algorithm>
#include <iostream>
#include <numeric>
#include <string>
#include <tuple>
#include <utility>
#include <vector>

#define rep(i, a, b) for (int i = int(a); i < int(b); i++)
using namespace std;
using ll = long long int;
using P = pair<ll, ll>;

// clang-format off
#ifdef _DEBUG_
#define dump(...) do{ cerr << __LINE__ << ":\t" << #__VA_ARGS__ << " = "; debug_print(__VA_ARGS__); } while(false)
template<typename T, typename... Ts> void debug_print(const T &t, const Ts &...ts) { cerr << t; ((cerr << ", " << ts), ...); cerr << endl; }
#else
#define dump(...) do{ } while(false)
#endif
template<typename T> vector<T> make_v(size_t a, T b) { return vector<T>(a, b); }
template<typename... Ts> auto make_v(size_t a, Ts... ts) { return vector<decltype(make_v(ts...))>(a, make_v(ts...)); }
template<typename T> bool chmin(T &a, const T& b) { if (a > b) {a = b; return true; } return false; }
template<typename T> bool chmax(T &a, const T& b) { if (a < b) {a = b; return true; } return false; }
template<typename T, typename... Ts> void print(const T& t, const Ts&... ts) { cout << t; ((cout << ' ' << ts), ...); cout << '\n'; }
template<typename... Ts> void input(Ts&... ts) { (cin >> ... >> ts); }
template<typename T> istream &operator,(istream &in, T &t) { return in >> t; }
// clang-format on

template<ll MOD = 1000000007>
class ModInt {
    ll n;
    ModInt constexpr inverse() const {
        return ModInt::pow(*this, MOD - 2);
    }

public:
    ModInt() : n(0) {}
    ModInt(ll _n) : n(((_n % MOD) + MOD) % MOD) {}
    ModInt operator+=(const ModInt &m) {
        n += m.n;
        if (n >= MOD) n -= MOD;
        return *this;
    }
    ModInt operator-=(const ModInt &m) {
        n -= m.n;
        if (n < 0) n += MOD;
        return *this;
    }
    ModInt operator*=(const ModInt &m) {
        n *= m.n;
        if (n >= MOD) n %= MOD;
        return *this;
    }
    ModInt operator/=(const ModInt &m) {
        (*this) *= m.inverse();
        return *this;
    }
    friend ModInt operator+(ModInt t, const ModInt &m) {
        return t += m;
    }
    friend ModInt operator-(ModInt t, const ModInt &m) {
        return t -= m;
    }
    friend ModInt operator*(ModInt t, const ModInt &m) {
        return t *= m;
    }
    friend ModInt operator/(ModInt t, const ModInt &m) {
        return t /= m;
    }
    ModInt operator=(const ll l) {
        n = l % MOD;
        if (n < 0) n += MOD;
        return *this;
    }
    friend ostream &operator<<(ostream &out, const ModInt &m) {
        out << m.n;
        return out;
    }
    friend istream &operator>>(istream &in, ModInt &m) {
        ll l;
        in >> l;
        m = l;
        return in;
    }
    static constexpr ModInt pow(const ModInt x, ll p) {
        ModInt<MOD> ans = 1;
        for (ModInt<MOD> m = x; p > 0; p /= 2, m *= m) {
            if (p % 2) ans *= m;
        }
        return ans;
    }
};
using mint = ModInt<998244353>;
mint operator"" _m(unsigned long long m) {
    return mint(m);
}

int main() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    int n, m;
    input(n, m);
    mint ans = mint::pow(m, n) * n;
    vector<mint> memo(n, 0);
    vector<bool> used(n, false);
    rep(i, 0, n) {
        rep(j, 0, i) {
            mint t = 0;
            if (used[i - j - 1]) {
                t = memo[i - j - 1];
            } else {
                rep(k, 0, m) {
                    t += mint::pow(m - k - 1, i - j - 1);
                }
                used[i - j - 1] = true;
                memo[i - j - 1] = t;
            }
            ans -= t * mint::pow(m, n - (i - j) - 1);
        }
    }
    print(ans);

    return 0;
}

#pragma GCC optimize("Ofast") 
#include <bits/stdc++.h>
using namespace std;
using ll = long long int ;
using P = pair<ll,ll>;
using Graph= vector<vector<ll>>;
struct edge{ll to ; ll cost ;} ;
using graph =vector<vector<edge>> ;
#define rep(i,n) for (ll i=0; i < (n); ++i)
#define rep2(i,n,m) for(ll i=n;i<=m;i++)
#define rep3(i,n,m) for(ll i=n;i>=m;i--)
#define pb push_back 
#define eb emplace_back 
#define ppb pop_back 
#define mpa make_pair 
#define fi first  
#define se second  
#define set20 cout<<fixed<<setprecision(20)  ;
const ll INF=1e18 ;   
inline void chmax(ll& a,ll b){a=max(a,b);}  
inline void chmin(ll& a,ll b){a=min(a,b);} 
long double pi=acos(-1) ;  
ll gcd(ll a, ll b) { return b?gcd(b,a%b):a;}  
ll lcm(ll a, ll b) { return a/gcd(a,b)*b;} 
ll dx[4] {1,0,-1,0} ;
ll dy[4] {0,1,0,-1} ;


int main(){
  ios::sync_with_stdio(false) ;
  cin.tie(nullptr) ;
  vector<string> A(3) ;
  rep(i,3) cin>>A[i] ;
  map<char,vector<P>> mp ;
  rep(i,3){
    string x= A[i] ;
    ll k= x.size() ;
    rep(j,k){
      mp[A[i][j]].pb(mpa(i,j)) ;
    }

  }

  ll cnt=mp.size() ;
  vector<vector<P>> B(cnt) ;
  ll i=0 ;
 for(auto u :mp){
   for(auto v:u.se){
    B[i].pb(v) ;
   }
   i++ ;
 }
  if(cnt>10){
    cout<<"UNSOLVABLE"<<endl ;
    return 0;
  }
  vector<ll> now(10) ;
  rep(i,10) now[i]=i ;
  do{
    ll d[3][15] ;
    rep(a,3)rep(b,15) d[a][b]=-1 ;
    rep(i,cnt){
      for(auto x:B[i]){
        d[x.fi][x.se]=now[i] ;
      }
    }
    if(d[0][0]==0 || d[1][0]==0 ||d[2][0]==0) continue ;
    vector<ll> e(3) ;
    rep(ww,3){
      ll num=0 ;
      rep(pp,15){
        if(d[ww][pp]==-1) break ;
        num*=10ll ;
        num+=d[ww][pp] ;
      }
      e[ww]=num ;
    }
    if(e[0]+e[1]==e[2]){
      rep(unti,3){
        cout<<e[unti]<<endl ;
      }
      return 0;
    }
  }while(next_permutation(now.begin(),now.end())) ;
  cout<<"UNSOLVABLE"<<endl ;
  return 0 ;
}
