#include <bits/stdc++.h>
int li, mo, ans, n, m, len, si, sum, ai, bi, x, y, jie[2000011], ni[2000011],
    f[1000011], mi[1000011];
char s[1000011], c[1000011];
int mim(int aa, int bb) {
  int ss = 1;
  while (bb != 0) {
    if ((bb % 2) == 1) ss = (1ll * ss * aa) % mo;
    bb = bb / 2;
    aa = (1ll * aa * aa) % mo;
  }
  return (ss);
}
int gcd(int aa, int bb) {
  if (aa == 0)
    return (bb);
  else
    return (gcd(bb % aa, aa));
}
int main() {
  scanf("%s", s + 1);
  scanf("%s", c + 1);
  len = strlen(s + 1);
  li = strlen(c + 1);
  mo = 1000000007;
  if (li != len)
    sum = 0;
  else {
    sum = 1;
    for (int i = 1; i <= len; i++) {
      if (s[i] == 'A') {
        if (c[i] == 'B') sum = 0;
      } else {
        if (s[i] == 'B') {
          if (c[i] == 'A') sum = 0;
        } else {
          if (c[i] == '?') sum = (sum * 2) % mo;
        }
      }
    }
  }
  ai = 0;
  bi = 0;
  scanf("%d", &n);
  x = 0;
  jie[0] = 1;
  for (int i = 1; i <= 2000001; i++) jie[i] = (1ll * jie[i - 1] * i) % mo;
  ni[2000001] = mim(jie[2000001], mo - 2);
  for (int i = 2000001; i >= 1; i--) ni[i - 1] = (1ll * ni[i] * i) % mo;
  mi[0] = 1;
  for (int i = 1; i <= 1000001; i++) mi[i] = (2 * mi[i - 1]) % mo;
  for (int i = 1; i <= len; i++) {
    if (s[i] == 'A') ai++;
    if (s[i] == 'B') bi++;
    if (s[i] == '?') x++;
  }
  for (int i = 1; i <= n; i++) {
    y = n / i;
    f[i] = (1ll * y * y) % mo;
  }
  for (int i = n - 1; i >= 1; i--) {
    int dbd = 2 * i;
    while (dbd <= n) {
      f[i] = (f[i] - f[dbd]) % mo;
      dbd = dbd + i;
    }
  }
  si = 0;
  for (int i = 1; i <= n; i++) {
    si = (si + 1ll * (f[i] - 1) * mi[i]) % mo;
    si = (si + mi[i]) % mo;
  }
  ans = 0;
  for (int i = 1; i <= li; i++) {
    if (c[i] == 'A') ai--;
    if (c[i] == 'B') bi--;
    if (c[i] == '?') {
      bi--;
      ai--;
      x++;
    }
  }
  for (int i = 0; i <= x; i++) {
    if (((ai + i) == 0) && ((bi + x - i) == 0)) {
      ans = (ans +
             1ll * (1ll * jie[x] * ni[i] % mo * ni[x - i] % mo - sum) * si) %
            mo;
      ans = (ans + 1ll * sum * (mi[n + 1] - 2) % mo * (mi[n + 1] - 2)) % mo;
    } else {
      if (((ai + i) > 0) && ((bi + x - i) < 0)) {
        int aa = ai + i, bb = i - x - bi;
        if (aa < bb) {
          int dbd = aa;
          aa = bb;
          bb = dbd;
        }
        bb = gcd(bb, aa);
        aa = aa / bb;
        ans = (ans + 1ll * jie[x] * ni[i] % mo * ni[x - i] % mo *
                         (mi[(n / aa) + 1] - 2)) %
              mo;
      }
      if (((ai + i) < 0) && ((bi + x - i) > 0)) {
        int aa = 0 - ai - i, bb = x + bi - i;
        if (aa < bb) {
          int dbd = aa;
          aa = bb;
          bb = dbd;
        }
        bb = gcd(bb, aa);
        aa = aa / bb;
        ans = (ans + 1ll * jie[x] * ni[i] % mo * ni[x - i] % mo *
                         (mi[(n / aa) + 1] - 2)) %
              mo;
      }
    }
  }
  ans = (ans + mo) % mo;
  printf("%d\n", ans);
}
