#include <bits/stdc++.h>
using namespace std;
const int N = 6e5 + 5, MOD = 1e9 + 7;
inline int qpow(int x, int y) {
  int ret = 1;
  while (y) {
    if (y & 1) ret = 1ll * ret * x % MOD;
    x = 1ll * x * x % MOD;
    y >>= 1;
  }
  return ret;
}
int gcd(int x, int y) {
  if (x == 0) return y;
  return gcd(y % x, x);
}
int n, len1, len2, fac[N], finv[N], p2[N], mu[N], isp[N], pri[N];
void init() {
  int i, j, lim = max(n, len1 + len2) + 1;
  mu[1] = 1;
  for (i = 2; i <= lim; i++) {
    if (!isp[i]) pri[++pri[0]] = i, mu[i] = MOD - 1;
    for (j = 1; j <= pri[0] && i * pri[j] <= lim; j++) {
      isp[i * pri[j]] = 1;
      if (i % pri[j])
        mu[i * pri[j]] = (MOD - mu[i]) % MOD;
      else
        break;
    }
  }
  fac[0] = finv[0] = 1;
  for (i = 1; i <= lim; i++)
    fac[i] = 1ll * fac[i - 1] * i % MOD, finv[i] = qpow(fac[i], MOD - 2);
  p2[0] = 1;
  for (i = 1; i <= lim; i++) p2[i] = (p2[i - 1] << 1) % MOD;
}
char s1[N], s2[N];
int main() {
  scanf("%s%s%d", s1 + 1, s2 + 1, &n);
  len1 = strlen(s1 + 1), len2 = strlen(s2 + 1);
  int i, j, k, c1 = 0, c2 = 0, kx = 0, ky = 0, al = 0, is = (len1 != len2),
               ans = 0;
  for (i = 1; i <= len1; i++) {
    if (s1[i] == 'A') {
      if (s2[i] == 'B') is = 1;
      kx++;
    } else {
      ky--;
      if (s1[i] == '?') {
        c1++;
        if (s2[i] == '?') al++;
      } else if (s2[i] == 'A')
        is = 1;
    }
  }
  for (i = 1; i <= len2; i++) {
    if (s2[i] == 'A')
      kx--;
    else {
      ky++;
      if (s2[i] == '?') c2++;
    }
  }
  init();
  for (i = -c2; i <= c1; i++) {
    int tx = kx + i, ty = ky + i;
    int tc = 1ll * fac[c1 + c2] * finv[c1 - i] % MOD * finv[c2 + i] % MOD;
    if (tx == 0 && ty == 0) {
      if (!is) {
        int ctb1 = 1ll * (p2[n + 1] - 2) * (p2[n + 1] - 2) % MOD;
        ans = (ans + 1ll * ctb1 * p2[al]) % MOD;
      }
      int ctb2 = 0;
      for (j = 1; j <= n; j++) {
        int tmp = 0;
        for (k = 1; j * k <= n; k++) {
          int tp = n / (j * k);
          tp = 1ll * tp * tp % MOD;
          tmp = (tmp + 1ll * mu[k] * tp) % MOD;
        }
        ctb2 = (ctb2 + 1ll * tmp * p2[j]) % MOD;
      }
      if (!is)
        ans = (ans + 1ll * ctb2 * (tc - p2[al] + MOD)) % MOD;
      else
        ans = (ans + 1ll * ctb2 * tc) % MOD;
    } else {
      if (tx < 0 && ty < 0) tx = -tx, ty = -ty;
      if (tx <= 0 || ty <= 0) continue;
      int div = gcd(tx, ty), lim = n / (max(tx, ty) / div);
      ans = (ans + 1ll * tc * (p2[lim + 1] - 2 + MOD)) % MOD;
    }
  }
  printf("%d", ans);
  return 0;
}
