#include <bits/stdc++.h>
using namespace std;
const long long N = 6 * 1e5 + 100;
long long n, ca, cb, da, db, tc, td, fac[N], inv[N], z[N], sum[N];
long long mu[N], v[N], p[N], w;
char c[N], d[N];
bool bl;
long long gcd(long long a, long long b) { return (b == 0) ? a : gcd(b, a % b); }
inline void add(long long &a, long long b) {
  a = ((a + b >= 1000000007) ? a + b - 1000000007 : a + b);
}
inline void del(long long &a, long long b) {
  a = ((a - b < 0) ? a - b + 1000000007 : a - b);
}
inline void mul(long long &a, long long b) { a = a * b % 1000000007; }
inline long long C(long long n, long long m) {
  return fac[n] * inv[m] % 1000000007 * inv[n - m] % 1000000007;
}
inline long long m_pow(long long a, long long b) {
  long long ans = 1;
  while (b) {
    if (b & 1) mul(ans, a);
    b >>= 1;
    mul(a, a);
  }
  return ans;
}
inline long long read() {
  long long f = 1, x = 0;
  char s = getchar();
  while (s < '0' || s > '9') {
    if (s == '-') f = -1;
    s = getchar();
  }
  while (s >= '0' && s <= '9') {
    x = x * 10 + s - '0';
    s = getchar();
  }
  return x * f;
}
inline void init(char s[], long long &a, long long &b, long long &t) {
  long long n = strlen(s + 1);
  for (long long i = 1; i <= n; i++) {
    if (s[i] == 'A') a++;
    if (s[i] == 'B') b++;
    if (s[i] == '?') t++;
  }
}
inline long long cal(long long p, long long x) {
  return C(tc, x) * C(td, x - p) % 1000000007;
}
inline long long f(long long n) {
  long long ans = 0;
  for (long long l = 1, r; l <= n; l = r + 1) {
    r = n / (n / l);
    long long tmp = mu[r];
    del(tmp, mu[l - 1]);
    mul(tmp, n / l);
    mul(tmp, n / l);
    add(ans, tmp);
  }
  return ans;
}
signed main() {
  scanf("%s%s", c + 1, d + 1);
  n = read();
  init(c, ca, cb, tc);
  init(d, da, db, td);
  long long m = 600010;
  fac[0] = 1;
  for (long long i = 1; i <= m; i++) fac[i] = fac[i - 1] * i % 1000000007;
  inv[m] = m_pow(fac[m], 1000000007 - 2);
  for (long long i = m - 1; i >= 0; i--)
    inv[i] = inv[i + 1] * (i + 1) % 1000000007;
  z[0] = 1;
  for (long long i = 1; i <= m; i++) z[i] = z[i - 1] * 2 % 1000000007;
  mu[1] = 1;
  for (long long i = 2; i <= n; i++) {
    if (!v[i]) v[i] = p[++w] = i, mu[i] = 1000000007 - 1;
    for (long long j = 1; j <= w; j++) {
      if (p[j] > v[i] || p[j] > n / i) break;
      v[p[j] * i] = p[j];
      if (i % p[j])
        mu[p[j] * i] = 1000000007 - mu[i];
      else
        mu[p[j] * i] = 0;
    }
  }
  for (long long i = 1; i <= n; i++) add(mu[i], mu[i - 1]);
  for (long long i = 1; i <= n; i++) sum[i] = z[i];
  for (long long i = 1; i <= n; i++) add(sum[i], sum[i - 1]);
  long long ans = 0, now = 0;
  for (long long l = 1, r; l <= n; l = r + 1) {
    r = n / (n / l);
    long long tmp = z[r + 1];
    del(tmp, z[l]);
    mul(tmp, f(n / l));
    add(now, tmp);
  }
  for (long long p = -td; p <= tc; p++) {
    long long f = C(tc + td, td + p);
    long long a = db - cb + td - tc + p, b = ca - da + p;
    if (a == 0 && b == 0) {
      add(ans, now * f % 1000000007);
      continue;
    }
    if (a * b <= 0) continue;
    a = abs(a);
    b = abs(b);
    long long g = gcd(a, b);
    a /= g;
    b /= g;
    if (a > b) swap(a, b);
    add(ans, sum[n / b] * f % 1000000007);
  }
  bool bl = (strlen(c + 1) == strlen(d + 1));
  m = strlen(c + 1);
  long long cnt = 0;
  for (long long i = 1; i <= m; i++) {
    if (c[i] == '?' && d[i] == '?') cnt++;
    if (c[i] == '?' || d[i] == '?') continue;
    if (c[i] != d[i]) bl = 0;
  }
  if (bl) {
    cnt = z[cnt];
    del(ans, cnt * now % 1000000007);
    add(ans, cnt * sum[n] % 1000000007 * sum[n] % 1000000007);
  }
  printf("%lld\n", ans);
}
