#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define sz(v) int((v).size())
#define all(x) (x).begin(), (x).end()
#define forn(i, n) for (int i = 1; i <= int(n); ++i)
//출처: https://codeforces.com/blog/entry/68809
void __print(int x) {cerr << x;}
void __print(long x) {cerr << x;}
void __print(long long x) {cerr << x;}
void __print(unsigned x) {cerr << x;}
void __print(unsigned long x) {cerr << x;}
void __print(unsigned long long x) {cerr << x;}
void __print(float x) {cerr << x;}
void __print(double x) {cerr << x;}
void __print(long double x) {cerr << x;}
void __print(char x) {cerr << '\'' << x << '\'';}
void __print(const char *x) {cerr << '"' << x << '"';}
void __print(const string &x) {cerr << '"' << x << '"';}
void __print(bool x) {cerr << (x ? "true" : "false");}
template<typename T, typename V>
void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}';}
template<typename T>
void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? "," : ""), __print(i); cerr << "}";}
void _print() {cerr << "]\n";}
template <typename T, typename... V>
void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);}
#ifndef ONLINE_JUDGE
#define debug(x...) cerr << "[" << (#x) << "] = [", _print(x)
#else
#define debug(x...)
#endif
#define int ll


const int MOD = 998244353;
int power(int a, int n) {
    int res = 1;
    for (;n; n >>= 1, a = (a * a) % MOD)
        if (n & 1) res = (res * a) % MOD;
    return res;
}
void add(int& a, int b){a = (a%MOD + b%MOD)%MOD;}
int h, w, k;
char grid[5050][5050];
int dp[5050][5050][4];
int f(char c)
{
    return c == 'R'? 2 : (c== 'D');
}
void solve()
{
    cin>>h>>w>>k;
    forn(_, k)
    {
        int a, b; char c; cin>>a>>b>>c;
        grid[a][b] = c;
    }
    int r = h*w - k;

    if (grid[1][1]) dp[1][1][f(grid[1][1])] = power(3, r);
    else dp[1][1][3] = power(3, r);

    int p3 = power(3, MOD - 2);
    forn(i, h) forn(j, w)
    {
        if (grid[i+1][j])
        {
            add(dp[i + 1][j][f(grid[i+1][j])], dp[i][j][f('D')]);
            add(dp[i + 1][j][f(grid[i+1][j])], dp[i][j][f('X')]);
            add(dp[i + 1][j][f(grid[i+1][j])], dp[i][j][3] * p3);
            add(dp[i + 1][j][f(grid[i+1][j])], dp[i][j][3] * p3);
        }
        else
        {
            add(dp[i + 1][j][3], dp[i][j][f('D')]);
            add(dp[i + 1][j][3], dp[i][j][f('X')]);
            add(dp[i + 1][j][3], dp[i][j][3] * p3);
            add(dp[i + 1][j][3], dp[i][j][3] * p3);
        }
        if (grid[i][j+1])
        {
            add(dp[i][j + 1][f(grid[i][j+1])], dp[i][j][f('R')]);
            add(dp[i][j + 1][f(grid[i][j+1])], dp[i][j][f('X')]);
            add(dp[i][j + 1][f(grid[i][j+1])], dp[i][j][3] * p3);
            add(dp[i][j + 1][f(grid[i][j+1])], dp[i][j][3] * p3);
        }
        else
        {
            add(dp[i][j + 1][3], dp[i][j][f('R')]);
            add(dp[i][j + 1][3], dp[i][j][f('X')]);
            add(dp[i][j + 1][3], dp[i][j][3] * p3);
            add(dp[i][j + 1][3], dp[i][j][3] * p3);
        }
        // for (int k = 0; k < 4; ++k) debug(i, j, k, dp[i][j][k]);
    }

    if (grid[h][w]) cout << dp[h][w][f(grid[h][w])] << '\n';
    else cout << dp[h][w][3] << '\n';
}
signed main(void)
{
    ios_base::sync_with_stdio(0); cin.tie(0);
    int T = 1; //cin >> T;
    while (T--) solve();
    return 0;
}
#include <bits/stdc++.h>
using namespace std;
typedef pair<int,int> ii;
typedef vector<int> vi;
typedef vector<ii> vii;
typedef vector<vi> vvi;
typedef long long int ll;
typedef vector<ll> vll;

#define _ << " " <<

string s1, s2, s3;
map<char, int> let;
vector<pair<char, int> > le;
vi tak, fir;

void getUniqLet(string &s) {
    int sz = int(s.size());
    for (int i = 0; i < sz; i++)
    {
        if (i == 0) let[s[i]] = 1;
        else let[s[i]];
    }
}

ll getNum(string &s) {
    int sz = int(s.size());
    ll n = 0, c = 1;
    for (int i = sz-1; i >= 0; i--)
    {
        n += (c * let[s[i]]);
        c *= 10;
    }
    return n;
}

bool check() {
    for (auto l : le) {
        let[l.first] = l.second;
    }
    ll a = getNum(s1);
    ll b = getNum(s2);
    ll c = getNum(s3);
    if (a + b == c) {
        cout << a << "\n";
        cout << b << "\n";
        cout << c << "\n";
        return true;
    }
    else return false;
}

bool perm(int i) {
    if (i == int(le.size())) {
        if (check()) return true;
        else return false;
    }
    int beg = 0;
    if (fir[i]) beg = 1;
    for (int d = beg; d < 10; d++)
    {
        if (!tak[d]) {
            tak[d] = 1;
            le[i].second = d;
            if (perm(i+1)) return true;
            tak[d] = 0;
        }
    }
    return false;
}

int main() {
    ios_base::sync_with_stdio(false); cin.tie(NULL);
    
    cin>>s1>>s2>>s3;
    getUniqLet(s1);
    getUniqLet(s2);
    getUniqLet(s3);

    if (let.size() > 10) {
        cout << "UNSOLVABLE\n";
        return 0;
    }

    for (auto l : let) {
        le.push_back({l.first, 0});
        fir.push_back(l.second);
    }
    tak.resize(10, 0);
    if (perm(0)) return 0;

    cout << "UNSOLVABLE\n";
    
    return 0;
}