#include<bits/stdc++.h>
using namespace std;

const int N=2e5+5;

inline int read()
{
	int x=0,f=1;
	char c=getchar();
	while(c<'0' || c>'9')
	{
		if(c=='-') f=-1;
		c=getchar();
	}
	while(c>='0' && c<='9')
	{
		x=x*10+c-'0';
		c=getchar();
	}
	return x*f;
}

int n,a[N],b[N],c[N*2],re[N*2],len=0;
inline int Find(int x)
{
	return lower_bound(re+1,re+len+1,x)-re;
}
vector<int> re1[N*2],re2[N*2];
int tree[N],p[N];
inline int lowbit(int x)
{
	return x&(-x);
}
inline void add(int place)
{
	for(int i=place;i<=n;i+=lowbit(i))
		tree[i]++;
}
inline int ask(int place)
{
	int ans=0;
	for(int i=place;i;i-=lowbit(i))
		ans+=tree[i];
	return ans;
}
int main()
{
	n=read();
	for(int i=1;i<=n;i++) c[i]=a[i]=read()+i;
	for(int i=1;i<=n;i++) c[i+n]=b[i]=read()+i;
	sort(c+1,c+n*2+1);
	for(int i=1;i<=n*2;i++)
		if(c[i]!=c[i-1])
			re[++len]=c[i];
	for(int i=1;i<=n;i++) a[i]=Find(a[i]),b[i]=Find(b[i]);
	for(int i=1;i<=n;i++) re1[a[i]].push_back(i);
	for(int i=1;i<=n;i++) re2[b[i]].push_back(i);
	for(int i=1;i<=len;i++)
	{
		int siz1=re1[i].size(),siz2=re2[i].size();
		if(siz1!=siz2)
		{
			printf("-1\n");
			return 0;
		}
		for(int j=0;j<siz1;j++) p[re1[i][j]]=re2[i][j];
	}
	long long ans=0;
	for(int i=n;i>=1;i--) ans+=ask(p[i]), add(p[i]);
	cout<<ans<<endl;
	return 0;
}
#include<bits/stdc++.h>
using namespace std;
#define ALL(x) begin(x),end(x)
#define rep(i,n) for(int i=0;i<(n);i++)
#define debug(v) cout<<#v<<":";for(auto x:v){cout<<x<<' ';}cout<<endl;
#define mod 1000000007
using ll=long long;
const int INF=1000000000;
const ll LINF=1001002003004005006ll;
int dx[]={1,0,-1,0},dy[]={0,1,0,-1};
// ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
template<class T>bool chmax(T &a,const T &b){if(a<b){a=b;return true;}return false;}
template<class T>bool chmin(T &a,const T &b){if(b<a){a=b;return true;}return false;}

struct IOSetup{
    IOSetup(){
        cin.tie(0);
        ios::sync_with_stdio(0);
        cout<<fixed<<setprecision(12);
    }
} iosetup;
 
template<typename T>
ostream &operator<<(ostream &os,const vector<T>&v){
    for(int i=0;i<(int)v.size();i++) os<<v[i]<<(i+1==(int)v.size()?"":" ");
    return os;
}
template<typename T>
istream &operator>>(istream &is,vector<T>&v){
    for(T &x:v)is>>x;
    return is;
}

#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")

template<ll Mod>
struct ModInt{
    long long x;
    ModInt():x(0){}
    ModInt(long long y):x(y>=0?y%Mod:(Mod-(-y)%Mod)%Mod){}
    ModInt &operator+=(const ModInt &p){
        if((x+=p.x)>=Mod) x-=Mod;
        return *this;
    }
    ModInt &operator-=(const ModInt &p){
        if((x+=Mod-p.x)>=Mod)x-=Mod;
        return *this;
    }
    ModInt &operator*=(const ModInt &p){
        x=(int)(1ll*x*p.x%Mod);
        return *this;
    }
    ModInt &operator/=(const ModInt &p){
        (*this)*=p.inverse();
        return *this;
    }
    ModInt operator-()const{return ModInt(-x);}
    ModInt operator+(const ModInt &p)const{return ModInt(*this)+=p;}
    ModInt operator-(const ModInt &p)const{return ModInt(*this)-=p;}
    ModInt operator*(const ModInt &p)const{return ModInt(*this)*=p;}
    ModInt operator/(const ModInt &p)const{return ModInt(*this)/=p;}
    bool operator==(const ModInt &p)const{return x==p.x;}
    bool operator!=(const ModInt &p)const{return x!=p.x;}
    ModInt inverse()const{
        int a=x,b=Mod,u=1,v=0,t;
        while(b>0){
            t=a/b;
            swap(a-=t*b,b);swap(u-=t*v,v);
        }
        return ModInt(u);
    }
    ModInt pow(long long n)const{
        ModInt ret(1),mul(x);
        while(n>0){
            if(n&1) ret*=mul;
            mul*=mul;n>>=1;
        }
        return ret;
    }
    friend ostream &operator<<(ostream &os,const ModInt &p){return os<<p.x;}
    friend istream &operator>>(istream &is,ModInt &a){long long t;is>>t;a=ModInt<Mod>(t);return (is);}
    static int get_mod(){return Mod;}
};

using mint=ModInt<998244353>;

vector<vector<int>> f(5000,vector<int>(5000,-1));
vector<vector<mint>> dp(5001,vector<mint>(5001,0));

signed main(){
    int h,w,k;cin>>h>>w>>k;
    rep(_,k){
        int i,j;char c;cin>>i>>j>>c;i--,j--;
        if(c=='R') f[i][j]=0;
        if(c=='D') f[i][j]=1;
        if(c=='X') f[i][j]=2;
    }
    

    // naive
    {
        dp[0][0]=1;
        rep(i,h){
            rep(j,w){
                if(f[i][j]<0 and (i!=h-1 or j!=w-1)){
                    dp[i][j]*=2;
                    dp[i][j]/=3;
                }
                if(f[i][j]<0){
                    if(i+1<h) dp[i+1][j]+=dp[i][j];
                    if(j+1<w) dp[i][j+1]+=dp[i][j];
                }
                if(f[i][j]==0){
                    if(j+1<w) dp[i][j+1]+=dp[i][j];
                }
                if(f[i][j]==1){
                    if(i+1<h) dp[i+1][j]+=dp[i][j];
                }
                if(f[i][j]==2){
                    if(i+1<h) dp[i+1][j]+=dp[i][j];
                    if(j+1<w) dp[i][j+1]+=dp[i][j];
                }
            }
        }   
    }
    // rep(i,h){
    //     rep(j,w){
    //         cout<<dp[i][j]<<" ";
    //     }
    //     cout<<endl;
    // }
    cout<<dp[h-1][w-1]*mint(3).pow(h*w-k)<<endl;
    return 0;
}
