#include <bits/stdc++.h>
using namespace std;
const int P = 1e9 + 7;
const int N = 2e6 + 5;
long long fsp(long long x, int y) {
  long long ans = 1;
  while (y) {
    if (y & 1) ans = ans * x % P;
    x = x * x % P, y >>= 1;
  }
  return ans;
}
char c[N], d[N];
long long sum, res, ans, fac[N], inv[N];
int n, lc, ld, ca, cb, da, db, qc, qd, now, a[N], b[N], vi[N];
bool check() {
  if (lc != ld) return false;
  for (int i = 0; i < lc; i++)
    if (c[i] != d[i] && c[i] != '?' && d[i] != '?')
      return false;
    else if (c[i] == d[i] && c[i] == '?')
      ++res;
  return true;
}
int gcd(int a, int b) {
  while (b) {
    int t = b;
    b = a % b, a = t;
  }
  return a;
}
long long calc(int a, int b) {
  if (!a && !b) return sum;
  if (1LL * a * b >= 0)
    return 0;
  else
    a = abs(a), b = abs(b);
  return (P + fsp(2, n / (max(a, b) / gcd(a, b)) + 1) - 2) % P;
}
long long C(int n, int m) {
  if (m > n) return 0;
  return fac[n] * inv[m] % P * inv[n - m] % P;
}
long long get(int x) { return 1LL * x * x % P; }
int main() {
  scanf("%s%s%d", c, d, &n);
  lc = strlen(c), ld = strlen(d);
  for (int i = 0; i < lc; i++)
    if (c[i] == '?')
      ++qc;
    else if (c[i] == 'A')
      ++ca;
    else
      ++cb;
  for (int i = 0; i < ld; i++)
    if (d[i] == '?')
      ++qd;
    else if (d[i] == 'A')
      ++da;
    else
      ++db;
  fac[0] = inv[0] = 1;
  for (int i = 1; i <= qc + qd; i++) {
    fac[i] = fac[i - 1] * i % P;
    inv[i] = fsp(fac[i], P - 2);
  }
  a[1] = 1;
  for (int i = 2; i <= n; i++) {
    if (!vi[i]) a[i] = -1, b[++now] = i;
    for (int j = 1; j <= now && i * b[j] <= n; j++) {
      vi[i * b[j]] = 1;
      if (i % b[j] == 0)
        break;
      else
        a[i * b[j]] = -a[i];
    }
  }
  for (int i = 1; i <= n; i++) {
    long long ans = 0, k = n / i;
    for (int j = 1; j <= k; j++) ans = (ans + P + a[j] * get(k / j) % P) % P;
    sum = (sum + fsp(2, i) * ans) % P;
  }
  if (check()) {
    ans = get((P + fsp(2, n + 1) - 2) % P) * fsp(2, res) % P;
    ans = (ans - fsp(2, res) * sum % P + P) % P;
  }
  for (int k = -qd; k <= qc; k++)
    ans =
        (ans + calc(ca - da + k, cb - db + qc - qd - k) * C(qc + qd, qd + k)) %
        P;
  printf("%lld\n", ans);
  return 0;
}
