#include <bits/stdc++.h>
using namespace std;
const long long MOD = 1e9 + 7;
const int SIZE = 300000;
long long realmod(long long a, long long M) {
  a %= M;
  if (a < 0) a += M;
  return a;
}
long long quickpow(long long a, long long n, long long M) {
  a = realmod(a, M);
  long long ans = 1;
  while (n) {
    if (n & 1) ans = ans * a % M;
    a = a * a % M;
    n >>= 1;
  }
  return ans;
}
long long fact[SIZE * 2 + 10] = {0};
long long invfact[SIZE * 2 + 10] = {0};
long long phi[SIZE + 10] = {0};
long long phi_sum[SIZE + 10] = {0};
void phi_table(long long n) {
  long long i, j;
  phi[1] = 1;
  for (i = 2; i <= n; i++) {
    if (!phi[i]) {
      for (j = i; j <= n; j += i) {
        if (!phi[j]) phi[j] = j;
        phi[j] = phi[j] / i * (i - 1);
      }
    }
  }
  phi_sum[0] = 0;
  for (int i = 1; i <= n; i++) phi_sum[i] = (phi_sum[i - 1] + phi[i]) % MOD;
}
long long inverse(long long a, long long p) { return quickpow(a, p - 2, p); }
void prepare_fact(long long n) {
  fact[0] = invfact[0] = 1;
  for (long long i = 1; i <= n; i++) {
    fact[i] = realmod(fact[i - 1] * i, MOD);
    invfact[i] = inverse(fact[i], MOD);
  }
}
long long Comb(long long n, long long m) {
  assert(n >= m);
  long long ans = fact[n];
  ans = realmod(ans * invfact[m], MOD);
  ans = realmod(ans * invfact[n - m], MOD);
  return ans;
}
long long N;
char C[SIZE + 10], D[SIZE + 10];
long long gcd(long long a, long long b) { return !b ? a : gcd(b, a % b); }
long long calc_basic(long long x, long long y) {
  if (x == 0 || y == 0) return 0;
  if (x < 0) x *= -1, y *= -1;
  if (x < 0 || y < 0) return 0;
  long long g = gcd(x, y);
  long long k = N / max(x / g, y / g);
  return realmod(2 * (quickpow(2, k, MOD) - 1), MOD);
}
long long calc_free(void) {
  long long sn = realmod(2 * (quickpow(2, N, MOD) - 1), MOD);
  long long ans = sn * sn % MOD;
  return ans;
}
long long free_cnt(void) {
  int n = strlen(C), m = strlen(D);
  if (n != m) return 0;
  long long ans = 1;
  for (int i = 0; i < n; i++) {
    char a = C[i], b = D[i];
    if (a == '?' && b == '?') {
      ans = realmod(ans * 2, MOD);
    } else if (a == '?' || b == '?') {
    } else if (a != b)
      return 0;
  }
  return ans;
}
long long calc_sync(void) {
  long long ans = 0;
  for (int g = 1; g <= N; g++) {
    long long cnt = phi_sum[N / g] * 2 - 1;
    long long now = quickpow(2, g, MOD);
    ans += cnt * now % MOD;
    ans %= MOD;
  }
  return ans;
}
long long solve(long long a, long long d1, long long b, long long d2) {
  long long sync_flx = calc_sync();
  long long free_flx = calc_free();
  long long cnt = free_cnt();
  long long ans = 0;
  for (long long aa = a - d2; aa <= a + d1; aa++) {
    long long bb = aa - (a + d1 - b - d2);
    long long t = Comb(d1 + d2, aa - a + d2);
    if (aa == 0 && bb == 0) {
      long long now =
          realmod((t - cnt) * sync_flx % MOD + cnt * free_flx % MOD, MOD);
      ans = realmod(ans + now, MOD);
    } else {
      ans = realmod(ans + t * calc_basic(aa, bb) % MOD, MOD);
    }
  }
  return ans;
}
void work(void) {
  long long a = 0, d1 = 0, b = 0, d2 = 0;
  int n = strlen(C), m = strlen(D);
  for (int i = 0; i < n; i++) {
    if (C[i] == 'A')
      a++;
    else if (C[i] == 'B')
      b--;
    else if (C[i] == '?')
      d1++;
  }
  for (int i = 0; i < m; i++) {
    if (D[i] == 'A')
      a--;
    else if (D[i] == 'B')
      b++;
    else if (D[i] == '?')
      d2++;
  }
  long long ans = solve(a, d1, b, d2);
  cout << ans << endl;
}
void read(void) {
  scanf("%s", C);
  scanf("%s", D);
  scanf("%I64d", &N);
}
int main() {
  phi_table(SIZE);
  prepare_fact(SIZE * 2);
  read();
  work();
  return 0;
}
