#include <bits/stdc++.h>
using namespace std;
long long int inv[1000005], fac[1000005], finv[1000005];
long long int rt[1000005];
char A[1000005], B[1000005];
void init() {
  inv[1] = 1;
  fac[0] = fac[1] = finv[0] = finv[1] = 1;
  for (int i = 2; i < 1000005; i++) {
    inv[i] = 1000000007 - inv[1000000007 % i] * (1000000007 / i) % 1000000007;
    fac[i] = fac[i - 1] * (long long int)i % 1000000007;
    finv[i] = finv[i - 1] * inv[i] % 1000000007;
  }
  rt[0] = 1;
  for (int i = 1; i < 1000005; i++) rt[i] = rt[i - 1] * 2LL % 1000000007;
}
long long int C(int a, int b) {
  if (a < b) return 0;
  return fac[a] * (finv[b] * finv[a - b] % 1000000007) % 1000000007;
}
int gcd(int x, int y) {
  if (y == 0) return x;
  return gcd(y, x % y);
}
int euler(int n) {
  int ret = n;
  int now = n;
  for (int i = 2; i * i <= now; i++) {
    if (now % i == 0) {
      ret -= ret / i;
      while (now % i == 0) now /= i;
    }
  }
  if (now >= 2) ret -= ret / now;
  return ret;
}
int main() {
  init();
  scanf("%s", &A);
  scanf("%s", &B);
  int n;
  scanf("%d", &n);
  long long int ret = 0;
  int cnt = 0, nm = 0;
  int sa = strlen(A), sb = strlen(B);
  for (int i = 0; i < sa; i++) {
    if (A[i] == 'A')
      cnt++;
    else if (A[i] == '?')
      nm++;
  }
  for (int i = 0; i < sb; i++) {
    if (B[i] == 'B')
      cnt++;
    else if (B[i] == '?')
      nm++;
  }
  for (int i = 0; i <= nm; i++) {
    int da = cnt + i - sb, db = cnt + i - sa;
    if (da < 0) da *= -1, db *= -1;
    if (da == 0 || db <= 0) continue;
    int g = gcd(da, db);
    da /= g, db /= g;
    int to = min(n / da, n / db);
    long long int ad =
        C(nm, i) * ((rt[to + 1] + 1000000007 - 2) % 1000000007) % 1000000007;
    ret += ad;
    if (ret >= 1000000007) ret -= 1000000007;
  }
  if (sa == sb) {
    long long int ct = 0;
    int na = 0, nb = 0;
    int aa = 0, ba = 0;
    for (int i = 0; i < sa; i++) {
      if (A[i] == 'A')
        aa++;
      else if (A[i] == '?')
        na++;
    }
    for (int i = 0; i < sb; i++) {
      if (B[i] == 'A')
        ba++;
      else if (B[i] == '?')
        nb++;
    }
    for (int i = 0; i <= sa; i++) {
      if (i >= aa && i >= ba && i <= aa + na && i <= ba + nb) {
        ct += C(na, i - aa) * C(nb, i - ba) % 1000000007;
        if (ct >= 1000000007) ct -= 1000000007;
      }
    }
    int nt = 0;
    for (int i = 0; i < sa; i++) {
      if (A[i] == '?' && B[i] == '?' && nt != -1)
        nt++;
      else if (A[i] != '?' && B[i] != '?' && A[i] != B[i])
        nt = -1;
    }
    if (nt >= 0) {
      ct -= rt[nt];
      if (ct < 0) ct += 1000000007;
    }
    for (int i = 1; i <= n; i++) {
      int way = 2 * euler(i);
      if (i == 1) way--;
      ret += ct * (long long int)way % 1000000007 *
             ((rt[n / i + 1] - 2 + 1000000007) % 1000000007) % 1000000007;
      if (ret >= 1000000007) ret -= 1000000007;
    }
    if (nt >= 0) {
      ret += rt[nt] * ((rt[n + 1] - 2 + 1000000007) % 1000000007) % 1000000007 *
             ((rt[n + 1] - 2 + 1000000007) % 1000000007) % 1000000007;
      if (ret >= 1000000007) ret -= 1000000007;
    }
  }
  printf("%lld\n", ret);
  return 0;
}
