#include <bits/stdc++.h>
using namespace std;
const long long Maxn = 300010;
const long long Mod = 1e9 + 7;
long long gcd(long long A, long long B) { return A == 0 ? B : gcd(B % A, A); }
long long _max(long long x, long long y) { return x > y ? x : y; }
long long n, m, N;
char s1[Maxn], s2[Maxn];
long long X, A, Y;
long long jc[Maxn << 1], inv[Maxn << 1], two[Maxn];
long long phi[Maxn], pr[Maxn], prl;
void pre() {
  phi[1] = 1;
  for (long long i = 2; i <= N; i++) {
    if (!phi[i]) phi[i] = i - 1, pr[++prl] = i;
    for (long long j = 1; j <= prl && i * pr[j] <= N; j++) {
      if (i % pr[j] == 0) {
        phi[i * pr[j]] = phi[i] * pr[j];
        break;
      }
      phi[i * pr[j]] = phi[i] * (pr[j] - 1);
    }
  }
  for (long long i = 2; i <= N; i++) phi[i] = (phi[i - 1] + phi[i]) % Mod;
}
long long solve(long long x) {
  long long y = n - m - x;
  if (x == 0 || y == 0) {
    if (x != 0 || y != 0) return 0;
    long long ret = 0;
    for (long long i = 1; i <= N; i++)
      ret = (ret + two[i] * (2 * phi[N / i] - 1)) % Mod;
    return ret;
  } else {
    y = -y;
    if (x < 0) x = -x, y = -y;
    if (y < 0) return 0;
    long long g = gcd(x, y);
    x /= g;
    y /= g;
    long long p = _max(x, y);
    return two[N / p + 1] - 2 + Mod;
  }
}
long long C(long long x, long long y) {
  return jc[x] * inv[y] % Mod * inv[x - y] % Mod;
}
int main() {
  long long i, j, k;
  scanf("%s", s1 + 1);
  n = strlen(s1 + 1);
  for (i = 1; i <= n; i++) {
    if (s1[i] == 'A') A++;
    if (s1[i] == '?') X++;
  }
  scanf("%s", s2 + 1);
  m = strlen(s2 + 1);
  for (i = 1; i <= m; i++) {
    if (s2[i] == 'A') A--;
    if (s2[i] == '?') Y++;
  }
  scanf("%I64d", &N);
  jc[0] = 1;
  for (i = 1; i <= X + Y; i++) jc[i] = jc[i - 1] * i % Mod;
  inv[0] = inv[1] = 1;
  for (i = 2; i <= X + Y; i++) inv[i] = inv[Mod % i] * (Mod - Mod / i) % Mod;
  for (i = 2; i <= X + Y; i++) inv[i] = inv[i - 1] * inv[i] % Mod;
  two[0] = 1;
  for (i = 1; i <= N + 1; i++) two[i] = two[i - 1] * 2 % Mod;
  pre();
  long long ans = 0, sum;
  if (n == m) {
    sum = 1;
    for (i = 1; i <= n; i++) {
      if (s1[i] == s2[i] || s1[i] == '?' || s2[i] == '?') {
        if (s1[i] == '?' && s2[i] == '?') sum = sum * 2 % Mod;
      } else {
        sum = 0;
        break;
      }
    }
    ans = (two[N + 1] - 2 + Mod) % Mod * (two[N + 1] - 2) % Mod * sum % Mod;
  }
  for (i = -Y; i <= X; i++) {
    if (n == m && A + i == 0)
      ans = (ans + solve(A + i) * (C(X + Y, i + Y) - sum + Mod)) % Mod;
    else
      ans = (ans + solve(A + i) * C(X + Y, i + Y)) % Mod;
  }
  printf("%I64d\n", ans);
  return 0;
}
