#include <bits/stdc++.h>
#define GET_MACRO(_1,_2,_3,_4,_5,_6,NAME,...) NAME
#define pr(...) GET_MACRO(__VA_ARGS__,pr6,pr5,pr4,pr3,pr2,pr1)(__VA_ARGS__)
#define Pr(a) (#a)<<"="<<(a)<<" "
#define pr1(a) cerr<<Pr(a)<<endl
#define pr2(a,b) cerr<<Pr(a)<<Pr(b)<<endl
#define pr3(a,b,c) cerr<<Pr(a)<<Pr(b)<<Pr(c)<<endl
#define pr4(a,b,c,d) cerr<<Pr(a)<<Pr(b)<<Pr(c)<<Pr(d)<<endl
#define pr5(a,b,c,d,e) cerr<<Pr(a)<<Pr(b)<<Pr(c)<<Pr(d)<<Pr(e)<<endl
#define pr6(a,b,c,d,e,f) cerr<<Pr(a)<<Pr(b)<<Pr(c)<<Pr(d)<<Pr(e)<<Pr(f)<<endl
#define int long long
#define double long double
using namespace std;
const int INF = 1LL<<55;
const int mod = (1e9)+7;
const double EPS = 1e-8;
const double PI = 6.0 * asin(0.5);
typedef pair<int,int> P;
typedef long long ll;
template<class T> T Max(T &a,T b){return a=max(a,b);}
template<class T> T Min(T &a,T b){return a=min(a,b);}
ostream& operator<<(ostream& o,P p){return o<<"("<<p.first<<","<<p.second<<")";}
istream& operator>>(istream& i,P &p){return i>>p.first>>p.second;}
ostream& operator<<(ostream& o,vector<auto> &a){int i=0;for(auto t:a)o<<(i++?" ":"")<<t;return o;}
istream& operator>>(istream& i,vector<auto> &a){for(auto &t:a)i>>t;return i;}
void prArr(auto a,string s=" "){int i=0;for(auto t:a)cout<<(i++?s:"")<<t;cout<<endl;}

class UF{
public:
  int V;
  vector<int> par,rnk,sz;
  UF(){}
  UF(int V):V(V),par(V),rnk(V,0),sz(V,1){for(int i=0;i<V;i++)par[i]=i;}
  
  int find(int x){
    assert(x < V);
    if(par[x]==x)return x;
    return par[x]=find(par[x]);
  }
  
  void unite(int x,int y){
    x=find(x), y=find(y);
    if(x==y)return;
    if(rnk[x]<rnk[y])par[x]=y, sz[y] += sz[x];
    else{
      par[y]=x;
      sz[x] += sz[y];
      if(rnk[x]==rnk[y])rnk[x]++;
    }
  }

  bool same(int x,int y){return find(x)==find(y);}

  int size(int x){return sz[find(x)];}
};


int n;
string s;
int m;
vector<int> u;
map<char,int> Idx;

int solve(vector<int> v){
  string t;
  for(int i=0;i<n;i++){
    if(!isdigit(s[i]) && v[ Idx[s[i]] ] != 0) t += toupper(s[i]);
    t += s[i];
  }

  
  int len = t.size();
  vector<int> l(len, 0);
  vector<int> r(len, 9);
  
  for(int i=0;i<len;i++){
    if(isdigit(t[i])) l[i] = r[i] = t[i] - '0';
    
    if(isupper(t[i])){
      int idx = Idx[ t[i] ];
      
      if(v[idx] == 2) {
        l[i] = r[i] = u[idx]/10;
        if(l[i] == 0) return 0;
      }
      
      if(v[idx] == 1){
        l[i] = 1;
        r[i] = u[idx]/10 - 1;
        if(r[i] <= 0) return 0;
      }
      assert(v[idx] != 0);
    }
    
    if(islower(t[i])){
      int idx = Idx[ t[i] ];
      if(v[idx] == 2) l[i] = 0, r[i] = u[idx]%10;
      if(v[idx] == 1) l[i] = 0, r[i] = 9;
      if(v[idx] == 0) l[i] = 0, r[i] = min(u[idx], 9LL);
    }
  }
  
  UF A(2*m);
  vector<int> mn(2*m, -1), mx(2*m, 10);
  for(int i=0;i<len;i++){
    int j = len - 1 - i;
    if(isdigit(t[i]) && isdigit(t[j])){
      if(t[i] != t[j]) return 0;
      continue;
    }
    
    if(isdigit(t[j])){
      int idx = Idx[ t[i] ] + (isupper(t[i])? m:0);
      idx = A.find(idx);
      Max(mn[idx], l[i]);
      Min(mx[idx], r[i]);
      
      Max(mn[idx], l[j]);
      Min(mx[idx], r[j]);
      
      continue;
    }
    
    if(isdigit(t[i])){
      int idx = Idx[ t[j] ] + (isupper(t[j])? m:0);
      idx = A.find(idx);
      Max(mn[idx], l[i]);
      Min(mx[idx], r[i]);
      
      Max(mn[idx], l[j]);
      Min(mx[idx], r[j]);
      
      continue;
    }

    int a = Idx[ t[i] ] + (isupper(t[i])? m:0);
    int b = Idx[ t[j] ] + (isupper(t[j])? m:0);
    a = A.find(a);
    b = A.find(b);
    A.unite(a, b);
    int p = A.find(a);
    Max(mn[p], l[i]);
    Min(mx[p], r[i]);

    Max(mn[p], l[j]);
    Min(mx[p], r[j]);

    Max(mn[p], mn[a]);
    Min(mx[p], mx[a]);

    Max(mn[p], mn[b]);
    Min(mx[p], mx[b]);
  }

  int res = 1;
  for(int i=0;i<2*m;i++){
    if(A.find(i) != i) continue;
    if(mn[i] < 0) continue;
    int num = max(0LL, mx[i] - mn[i] + 1);
    res = (res * num) % mod;
  }
  return res;
}

vector<int> v;
int dfs(int dep){
  if(dep == m) return solve(v);

  int res = 0;  
  for(int i=0;i<3;i++) {
    v[dep] = i;
    res = (res + dfs(dep+1))%mod;
  }
  return res;
}


signed main(){
  cin.tie(0);
  ios_base::sync_with_stdio(0);
  cout << fixed << setprecision(12);

  cin>>n>>m;
  cin>>s;
  vector<char> var(m);
  u.resize(m);
  for(int i=0;i<m;i++) cin>>var[i]>>u[i];

  for(int i=0;i<m;i++) Idx[var[i]] = i;
  for(int i=0;i<m;i++) Idx[toupper(var[i])] = i;
  
  v.resize(m);
  int ans = dfs(0);
  cout<<ans<<endl;
  return 0;
}



Measured memory_bytes: 3236