#include<bits/stdc++.h>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define HUGE_NUM 99999999999999999
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;


#define LEN 505
#define NUM 30
#define SIZE 10

int N,M;
int height[LEN],boss[LEN];
ll calc_min[NUM],calc_max[NUM];
int input_char[SIZE],num_digit[SIZE];
int work[2*LEN];
char buf[LEN];
ll limit[SIZE];
ll minimum[2][NUM],maximum[2][NUM];


int get_boss(int id){
	if(boss[id] == id)return id;
	else{
		return boss[id] = get_boss(boss[id]);
	}
}

bool unite(int x,int y){
	int boss_x = get_boss(x);
	int boss_y = get_boss(y);

	if(boss_x == boss_y)return true;

	if(height[x] > height[y]){

		calc_min[boss_x] = max(calc_min[boss_x],calc_min[boss_y]);
		calc_max[boss_x] = min(calc_max[boss_x],calc_max[boss_y]);

		if(calc_min[boss_x] > calc_max[boss_x]){
			return false;
		}

		boss[boss_y] = boss_x;

	}else if(height[x] < height[y]){

		calc_min[boss_y] = max(calc_min[boss_y],calc_min[boss_x]);
		calc_max[boss_y] = min(calc_max[boss_y],calc_max[boss_x]);

		if(calc_min[boss_y] > calc_max[boss_y]){
			return false;
		}
		boss[boss_x] = boss_y;

	}else{ //height[x] == height[y]

		calc_min[boss_x] = max(calc_min[boss_x],calc_min[boss_y]);
				calc_max[boss_x] = min(calc_max[boss_x],calc_max[boss_y]);

		if(calc_min[boss_x] > calc_max[boss_x]){
			return false;
		}

		boss[boss_y] = boss_x;
		height[x]++;
	}

	return true;
}

void init(){

	for(int i = 0; i < NUM; i++){
		boss[i] = i;
		height[i] = 0;
	}
}

bool is_digit(char ch){
	return ch >= '0' && ch <= '9';
}

ll recursive(int index){

	if(index < M){

		ll ret = 0;

		int loc = input_char[index];

		if(limit[loc] < 10){

			minimum[0][loc] = 0;
			maximum[0][loc] = limit[loc];
			num_digit[loc] = 1;
			ret += recursive(index+1);

		}else{

			minimum[0][loc] = 0;
			maximum[0][loc] = 9;
			num_digit[loc] = 1;
			ret += recursive(index+1);

			if(limit[loc] >= 20){

				minimum[0][loc] = 0;
				maximum[0][loc] = 9;

				minimum[1][loc] = 1;
				maximum[1][loc] = limit[loc]/10-1;

				num_digit[loc] = 2;
				ret += recursive(index+1);
			}

			minimum[0][loc] = 0;
			maximum[0][loc] = limit[loc]%10;

			minimum[1][loc] = limit[loc]/10;
			maximum[1][loc] = limit[loc]/10;

			num_digit[loc] = 2;
			ret += recursive(index+1);
		}
		return ret%MOD;
	}

	int total_digit = 0;

	for(int i = 0; i < N; i++){
		if(is_digit(buf[i])){

			total_digit += 1;

		}else{

			total_digit += num_digit[buf[i]-'a'];
		}
	}

	init();

	for(int i = 0; i <= 9; i++){

		calc_min[i] = i;
		calc_max[i] = i;
	}
	for(int i = 10; i <= 19; i++){

		calc_min[i] = minimum[0][i%10];
		calc_max[i] = maximum[0][i%10];
	}
	for(int i = 20; i <= 29; i++){

		calc_min[i] = minimum[1][i%10];
		calc_max[i] = maximum[1][i%10];
	}

	int tmp_ch;
	int union_index = 0;

	for(int i = 0; i < N; i++){
		if(is_digit(buf[i])){

			work[union_index] = buf[i]-'0';
			union_index += 1;

		}else{

			tmp_ch = buf[i]-'a';

			if(num_digit[tmp_ch] == 1){

				work[union_index] = 10+tmp_ch;

			}else{

				work[union_index] = 20+tmp_ch;
				work[union_index+1] = 10+tmp_ch;

			}
			union_index += num_digit[tmp_ch];
		}
	}

	int left = 0,right = total_digit-1;

	while(left < right){

		if(!unite(work[left],work[right])){
			return 0;
		}

		left++;
		right--;
	}

	ll ret = 1;
	int tmp_boss;
	bool check[NUM];

	for(int i = 0; i < NUM; i++){
		check[i] = false;
	}

	for(int i = 0; i < total_digit; i++){

		tmp_boss = get_boss(work[i]);
		if(check[tmp_boss])continue;

		check[tmp_boss] = true;
		ret *= (calc_max[tmp_boss]-calc_min[tmp_boss]+1);
		ret %= MOD;
	}

	return ret%MOD;
}

int main(){

	scanf("%d %d %s",&N,&M,buf);

	char ch[2];
	ll tmp;

	for(int loop = 0; loop < M; loop++){

		scanf("%s %lld",ch,&tmp);
		limit[ch[0]-'a'] = tmp;
		input_char[loop] = ch[0]-'a';
	}

	printf("%lld\n",recursive(0));

	return 0;
}



Measured memory_bytes: 3252