#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll mod = 1e9 + 7;
const int maxn = 2e5 + 10;
const int maxk = 20;
char s[maxn];
ll f[maxn][maxk];
int n, k;
inline int c(char ch) { return isdigit(ch) ? ch - '0' : ch - 'A' + 10;}
ll calc(int pos, int state, bool flag)
{
	if((!flag && state) || pos == n) return f[pos][__builtin_popcount(state)];
	ll ret = 0;
	int end = flag ? c(s[pos]) : 15;
	for(int i = 0; i <= end; ++i)
	{
		int nxtstate = (!state && !i) ? 0 : state | (1 << i);
		ret = (ret + calc(pos + 1, nxtstate, flag && (i == end))) % mod;
	}
	return ret;
}
int main()
{
	scanf("%s%d", s, &k);
	n = strlen(s);
	f[n][k] = 1;
	for(int i = n - 1; ~i; --i)
		for(int j = 0; j <= k; ++j)
			f[i][j] = (f[i + 1][j] * j % mod + f[i + 1][j + 1] * (16 - j) % mod) % mod;
	printf("%lld\n", calc(0, 0, 1));
	return 0;
}
#pragma GCC optimize(2)
#include<unordered_map>
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<string>
#include<vector>
#include<queue>
#include<stack>
#include<cmath>
#include<map>
#include<set>
#define Buff ios::sync_with_stdio(false)
#define rush() int Case = 0; int T; cin >> T;  while(T--)
#define rep(i, a, b) for(int i = a; i <= b; i ++)
#define per(i, a, b) for(int i = a; i >= b; i --)
#define reps(i, a, b) for(int i = a; b; i ++)
#define clc(a, b) memset(a, b, sizeof(a))
#define Buff ios::sync_with_stdio(false)
#define readl(a) scanf("%lld", &a)
#define readd(a) scanf("%lf", &a)
#define readc(a) scanf("%c", &a)
#define reads(a) scanf("%s", a)
#define read(a) scanf("%d", &a)
#define lowbit(n) (n&(-n))
#define pb push_back
#define lson rt<<1
#define rson rt<<1|1
#define ls lson, l, mid
#define rs rson, mid+1, r
#define y second
#define x first
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int>PII;
const double eps = 1e-6;
const int INF = 1e9 + 7;
const int N = 2007;
const int M = 1e6 + 7;
const ll mod = 1e9 + 7;
ll dp[N][N];
ll sum[4][N][N];
char a[N][N];
signed main()
{
    Buff;
    int n, m;
    cin >> n >> m;
    for(int i = 1; i <= n; i++)
        for(int j = 1; j <= m; j++)
            cin >> a[i][j];
    dp[1][1] = sum[1][1][1] = sum[2][1][1] = sum[3][1][1] = 1;
    for(int i = 1; i <= n; i++)
    {
        for(int j = 1; j <= m; j++)
        {
            if(a[i][j] == '#' || (i == j && j == 1))
                continue;
            else
            {
                dp[i][j] = (sum[1][i - 1][j] + sum[2][i][j - 1] + sum[3][i - 1][j - 1]) % mod;
                sum[1][i][j] = (sum[1][i - 1][j] + dp[i][j]) % mod;
                sum[2][i][j] = (sum[2][i][j - 1] + dp[i][j]) % mod;
                sum[3][i][j] = (sum[3][i - 1][j - 1] + dp[i][j]) % mod;
            }
        }
    }
    cout << dp[n][m] << endl;
    return 0;
}