#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> l_l;
typedef pair<int, int> i_i;
template<class T>
inline bool chmax(T &a, T b) {
    if(a < b) {
        a = b;
        return true;
    }
    return false;
}

template<class T>
inline bool chmin(T &a, T b) {
    if(a > b) {
        a = b;
        return true;
    }
    return false;
}

struct UnionFind {
    vector<int> par;
    vector<int> rank;
    vector<ll> state;
    UnionFind(int n = 1) {
        init(n);
    }

    void init(int n = 1) {
        par.resize(n + 1); rank.resize(n + 1); state.resize(n + 1);
        for (int i = 0; i <= n; ++i) par[i] = i, rank[i] = 0, state[i] = 0;
    }

    int root(int x) {
        if (par[x] == x) {
            return x;
        }
        else {
            int r = root(par[x]);
            return par[x] = r;
        }
    }

    bool issame(int x, int y) {
        return root(x) == root(y);
    }

    bool merge(int x, int y) {
        x = root(x); y = root(y);
        if (x == y) return false;
        if (rank[x] < rank[y]) swap(x, y);
        if (rank[x] == rank[y]) ++rank[x];
        par[y] = x;
        state[x] &= state[y];
        return true;
    }
};

const long double EPS = 1e-10;
const long long INF = 1e18;
const long double PI = acos(-1.0L);
const ll mod = 1000000007;
ll N, M;
string S;
char var[10];
int u[10];
map<char, int> mp;
vector<int> v;
ll ans = 0;

void f() {
    UnionFind uni(30);
    for(int i = 0; i < 10; i++) {
        uni.state[i] = (1 << i);
    }
    for(int i = 0; i < M; i++) {
        if(v[i] == 0) {
            for(int j = 0; j <= min(9, u[i]); j++) {
                uni.state[10+i] |= (1 << j);
            }
            uni.state[20+i] = 1;
        } else if(v[i] == 1) {
            uni.state[10+i] |= (1 << (u[i]/10));
            for(int j = 0; j <= (u[i] % 10); j++) {
                uni.state[20+i] |= (1 << j);
            }
        } else {
            for(int j = 1; j < (u[i] / 10); j++) {
                uni.state[10+i] |= (1 << j);
            }
            for(int j = 0; j <= 9; j++) {
                uni.state[20+i] |= (1 << j);
            }
        }
    }
    vector<int> A;
    //cerr << S << endl;
    for(auto c : S) {
        if('0' <= c and c <= '9') {
            A.push_back((int)(c - '0'));
        } else {
            int idx = mp[c];
            if(v[idx] == 0) {
                A.push_back(10 + idx);
            } else {
                A.push_back(10+idx);
                A.push_back(20+idx);
            }
        }
    }
    auto B = A;
    reverse(B.begin(), B.end());
    for(int i = 0; i < A.size(); i++) {
        //cerr << A[i] << " " << B[i] << endl;
        uni.merge(A[i], B[i]);
    }
    //cerr << endl;
    ll tmp = 1;
    for(int i = 0; i < 10; i++) {
        if(uni.root(i) != i) continue;
        tmp *= __builtin_popcount(uni.state[i]);
        tmp %= mod;
    }
    for(int a = 0; a < M; a++) {
        for(int j = 1; j <= 2; j++) {
            int i = 10 * j + a;
            if(uni.root(i) != i) continue;
            tmp *= __builtin_popcount(uni.state[i]);
            tmp %= mod;
        }
    }
    ans += tmp;
    ans %= mod;
}

void dfs() {
    if(v.size() == M) {
        f();
        return;
    }
    int idx = v.size();
    v.push_back(0);
    dfs();
    v.pop_back();
    v.push_back(1);
    if(u[idx] >= 10) dfs();
    v.pop_back();
    v.push_back(2);
    if(u[idx] >= 20) dfs();
    v.pop_back();
}

int main() {
    cin >> N >> M;
    cin >> S;
    for(int i = 0; i < M; i++) {
        cin >> var[i] >> u[i];
        mp[var[i]] = i;
    }
    dfs();
    cout << ans << endl;
    return 0;
}


Measured memory_bytes: 3232