#include <bits/stdc++.h>
using namespace std;
const int maxn = 300010;
const int mod = 1e9 + 7;
char buf1[maxn], buf2[maxn];
int c_a, c_b, c_w;
int d_a, d_b, d_w;
int len = 0, n;
int jc[maxn << 1], inv[maxn << 1], ijc[maxn << 1];
int phi[(maxn << 2) + maxn], isnp[(maxn << 2) + maxn], p[(maxn << 2) + maxn],
    sum[(maxn << 2) + maxn];
int tot = 0, s = 0;
int gcd(int x, int y) {
  if (!y) return x;
  return gcd(y, x % y);
}
int qpow(int x, int y) {
  int ret = 1;
  while (y) {
    if (y & 1) ret = 1LL * ret * x % mod;
    y >>= 1;
    x = 1LL * x * x % mod;
  }
  return ret;
}
int tmp = 0;
int cal(int t) {
  int ca = c_a - d_a, cb = d_b - c_b;
  int A = ca + t, B = cb + d_w - c_w + t;
  if (A == 0 && B == 0) {
    return tmp;
  } else if (1LL * A * B < 0) {
    return 0;
  } else {
    if (A == 0 && B != 0) return 0;
    if (B == 0 && A != 0) return 0;
    if (A < 0 || B < 0) {
      A = -A;
      B = -B;
    }
    int d = gcd(A, B);
    int L = n / (max(A, B) / d);
    return (qpow(2, L + 1) - 2 + mod) % mod;
  }
}
int main() {
  scanf("%s", buf1);
  len = int(strlen(buf1));
  for (int i = 0; i < len; i++) {
    if (buf1[i] == 'A')
      c_a++;
    else if (buf1[i] == 'B')
      c_b++;
    else
      c_w++;
  }
  s += len;
  int len1 = len;
  scanf("%s", buf2);
  len = int(strlen(buf2));
  scanf("%d", &n);
  for (int i = 0; i < len; i++) {
    if (buf2[i] == 'A')
      d_a++;
    else if (buf2[i] == 'B')
      d_b++;
    else
      d_w++;
  }
  s += len;
  inv[1] = 1;
  jc[0] = ijc[0] = 1;
  for (int i = 1; i <= c_w + d_w; i++) {
    jc[i] = 1LL * jc[i - 1] * i % mod;
    if (i == 1) {
      ijc[i] = inv[i] = 1;
    } else {
      inv[i] = mod - 1LL * (mod / i) * inv[mod % i] % mod;
      ijc[i] = 1LL * ijc[i - 1] * inv[i] % mod;
    }
  }
  phi[1] = 1;
  s += n;
  for (int i = 2; i <= s; i++) {
    if (!isnp[i]) {
      p[++tot] = i;
      phi[i] = i - 1;
    }
    for (int j = 1; j <= tot && p[j] * i <= s; j++) {
      isnp[p[j] * i] = 1;
      if (i % p[j] == 0) {
        phi[i * p[j]] = phi[i] * p[j];
        break;
      } else
        phi[i * p[j]] = phi[i] * phi[p[j]];
    }
  }
  for (int i = 1; i <= s; i++) {
    sum[i] = (sum[i - 1] + phi[i]) % mod;
  }
  {
    int t = 1;
    for (int d = 1; d <= n; d++) {
      t = t * 2 % mod;
      tmp = (tmp + 1LL * t * (sum[n / d] * 2 % mod - 1 + mod) % mod) % mod;
    }
  }
  int ans = 0;
  if (len1 == len) {
    int c = 1;
    for (int i = 0; i < len; i++) {
      if (buf1[i] != buf2[i]) {
        if (buf1[i] != '?' && buf2[i] != '?') {
          c = 0;
        }
      } else if (buf1[i] == '?')
        c = 1LL * c * 2 % mod;
    }
    ans = 1LL * c *
          ((1LL * qpow(2, n + 1) - 2) % mod * (qpow(2, n + 1) - 2) % mod - tmp +
           mod) %
          mod;
  }
  for (int t = -d_w; t <= c_w; t++) {
    int cnt = 1LL * jc[c_w + d_w] * ijc[d_w + t] % mod * ijc[c_w - t] % mod;
    (ans += 1LL * cal(t) * cnt % mod) %= mod;
  }
  printf("%d\n", ans);
  return 0;
}
