#include <bits/stdc++.h>
using namespace std;
using namespace std;
const int mod = 1000000007;
const int maxn = 700010;
char s[maxn], t[maxn];
int n, ca, cb, cc, all;
inline int che(int a) { return a >= mod ? a - mod : a; }
long long ksm(long long a, long long b = mod - 2) {
  long long r = 1ll;
  for (; b; b >>= 1ll, a = a * a % mod)
    if (b & 1ll) r = r * a % mod;
  return r;
}
long long po2[maxn], fac[maxn], inv[maxn];
int C(int a, int b) {
  return a < b ? 0 : fac[a] * inv[b] % mod * inv[a - b] % mod;
}
int slv(int x) { return che(po2[x + 1] - 2 + mod); }
int gcd(int x, int y) { return y == 0 ? x : gcd(y, x % y); }
int f(int a, int b) {
  if (a < 0) a = -a, b = -b;
  if (b > 0) return 0;
  b = -b;
  if (a == 0 && b == 0) return all;
  if (a == 0 || b == 0) return 0;
  int g = gcd(a, b);
  a /= g, b /= g;
  return slv(n / max(a, b));
}
int mu[maxn], pri[maxn], tot, g[maxn];
int vis[maxn];
void init() {
  po2[0] = fac[0] = 1;
  for (int i = 1; i <= 700003; i++)
    po2[i] = po2[i - 1] * 2ll % mod, fac[i] = 1ll * fac[i - 1] * i % mod;
  inv[700003] = ksm(fac[700003]);
  for (int i = 700002; i >= 0; i--) inv[i] = 1ll * inv[i + 1] * (i + 1) % mod;
  mu[1] = 1;
  for (int i = 2; i <= 700003; i++) {
    if (!vis[i]) pri[++tot] = i, mu[i] = -1;
    for (int j = 1; j <= tot && i * pri[j] <= 700003; j++) {
      vis[i * pri[j]] = true;
      if (i % pri[j] == 0) {
        mu[i * pri[j]] = 0;
        break;
      } else
        mu[i * pri[j]] = -mu[i];
    }
  }
  for (int i = 1; i <= 700003; i++)
    for (int j = 1; i * j <= 700003; j++)
      g[i * j] = (g[i * j] + 1ll * mu[j] * po2[i] + mod) % mod;
}
int main() {
  scanf("%s%s%d", s + 1, t + 1, &n);
  init();
  int n1 = strlen(s + 1), n2 = strlen(t + 1), ans = 0;
  for (int i = 1; i <= n; i++)
    all = (all + 1ll * g[i] * (n / i) % mod * (n / i)) % mod;
  if (n1 == n2) {
    int c = 1;
    for (int i = 1; i <= n1; i++)
      if (s[i] == '?' && t[i] == '?')
        c = c * 2ll % mod;
      else if (s[i] != '?' && t[i] != '?' && s[i] != t[i])
        c = 0;
    ans = (ans + 1ll * c * slv(n) % mod * slv(n) % mod - 1ll * c * all % mod +
           mod) %
          mod;
  }
  for (int i = 1; i <= n1; i++)
    if (s[i] == '?')
      cc++, ca++;
    else if (s[i] == 'A')
      ca++;
    else
      cb++;
  for (int i = 1; i <= n2; i++)
    if (t[i] == '?')
      cc++, cb--;
    else if (t[i] == 'A')
      ca--;
    else
      cb--;
  for (int i = 0; i <= cc; i++)
    ans = (ans + 1ll * f(ca - i, cb + i) * C(cc, i) % mod) % mod;
  printf("%d\n", ans);
  return 0;
}
