#include <bits/stdc++.h>
using namespace std;
int N, M, n, A, B, cntc, cntd, VAL;
char s[3000010], t[3000010];
int ci[3000010];
int quick_pow(int x, int p) {
  int an = 1, po = x;
  while (p) {
    if (p & 1) an = 1ll * an * po % 1000000007;
    po = 1ll * po * po % 1000000007;
    p >>= 1;
  }
  return an;
}
int fac[3000010], inv[3000010];
bool isprime[3000010];
int prime[3000010], primesize, phi[3000010];
void pre() {
  ci[0] = 1;
  for (int i = 1; i <= 3000000; i++) ci[i] = 1ll * ci[i - 1] * 2 % 1000000007;
  fac[0] = 1;
  for (int i = 1; i <= 3000000; i++) fac[i] = 1ll * fac[i - 1] * i % 1000000007;
  inv[3000000] = quick_pow(fac[3000000], 1000000007 - 2);
  for (int i = 2999999; i >= 0; i--)
    inv[i] = 1ll * inv[i + 1] * (i + 1) % 1000000007;
  memset(isprime, true, sizeof(isprime));
  isprime[1] = 0;
  phi[1] = 1;
  for (int i = 2; i <= 3000000; i++) {
    if (isprime[i]) {
      prime[++primesize] = i;
      phi[i] = i - 1;
    }
    for (int j = 1; j <= primesize && i * prime[j] <= 3000000; j++) {
      isprime[i * prime[j]] = false;
      if (i % prime[j] == 0) {
        phi[i * prime[j]] = phi[i] * prime[j];
        break;
      }
      phi[i * prime[j]] = phi[i] * (prime[j] - 1);
    }
  }
  for (int i = 1; i <= n; i++) phi[i] = (phi[i - 1] + phi[i]) % 1000000007;
  for (int i = 1; i <= n; i++)
    VAL = (VAL +
           1ll * ci[i] * (1ll * 2 * phi[n / i] - 1 + 1000000007) % 1000000007) %
          1000000007;
  ci[0] = 0;
  for (int i = 1; i <= n; i++) ci[i] = (ci[i] + ci[i - 1]) % 1000000007;
}
int C(int x, int y) {
  if (x < y || x < 0 || y < 0) return 0;
  return 1ll * fac[x] * inv[y] % 1000000007 * inv[x - y] % 1000000007;
}
int gcd(int x, int y) {
  if (!y) return x;
  return gcd(y, x % y);
}
int F(int a, int b) {
  if (a == 0 && b == 0) return VAL;
  if (1ll * a * b >= 0) return 0;
  a = abs(a);
  b = abs(b);
  int x = max(a, b) / gcd(a, b);
  return ci[n / x];
}
int ans;
int main() {
  scanf("%s", s + 1);
  N = strlen(s + 1);
  scanf("%s", t + 1);
  M = strlen(t + 1);
  cin >> n;
  pre();
  for (int i = 1; i <= N; i++) {
    if (s[i] == 'A')
      A++;
    else if (s[i] == 'B')
      B++;
    else
      cntc++;
  }
  for (int i = 1; i <= M; i++) {
    if (t[i] == 'A')
      A--;
    else if (t[i] == 'B')
      B--;
    else
      cntd++;
  }
  for (int i = -cntd; i <= cntc; i++) {
    ans = (ans + 1ll * F(A + i, B + cntc - cntd - i) *
                     C(cntc + cntd, cntd + i) % 1000000007) %
          1000000007;
  }
  if (N == M) {
    int x = 1;
    for (int i = 1; i <= N; i++) {
      if (s[i] == '?' || t[i] == '?') {
        if (s[i] == t[i]) x = 1ll * x * 2 % 1000000007;
      } else if (s[i] != t[i]) {
        x = 0;
        break;
      }
    }
    ans = (ans - 1ll * x * VAL % 1000000007 + 1000000007) % 1000000007;
    ans =
        (ans + 1ll * x * ci[n] % 1000000007 * ci[n] % 1000000007) % 1000000007;
  }
  cout << ans << endl;
  return 0;
}
