#include <bits/stdc++.h>
using namespace std;
int cnt, n, ans1;
char s[3000006], t[3000006];
int prime[3000006], po[3000006], miu[3000006], sum[3000006], fac[3000006],
    ifac[3000006];
bool vis[3000006];
void init(int);
int quick_pow(int, int);
int f(int, int);
int gcd(int, int);
int C(int, int);
int main() {
  scanf("%s%s", s + 1, t + 1);
  int a = 0, b = 0, lens = strlen(s + 1), lent = strlen(t + 1), p = 0, q = 0;
  cin >> n;
  init(lens);
  for (int i = 1; i <= lens; ++i)
    if (s[i] == 'A')
      ++a;
    else if (s[i] == 'B')
      ++b;
    else
      ++p;
  for (int i = 1; i <= lent; ++i)
    if (t[i] == 'A')
      --a;
    else if (t[i] == 'B')
      --b;
    else
      ++q;
  int ans = 0;
  for (int i = -max(p, q); i <= max(p, q); ++i) {
    int tmp = 1ll * f(a + i, b + p - q - i) * C(p - i, p + q) % 1000000007;
    (ans += tmp) %= 1000000007;
  }
  if (lens == lent) {
    bool flag = true;
    int cnt = 0;
    for (int i = 1; i <= lens; ++i)
      if (s[i] != t[i] && s[i] != '?' && t[i] != '?')
        flag = false;
      else if (s[i] == '?' && t[i] == '?')
        ++cnt;
    if (flag) {
      (ans += (1000000007 - 1ll * po[cnt] * ans1 % 1000000007) % 1000000007) %=
          1000000007;
      (ans += 1ll * po[cnt] * (po[n + 1] - 2) % 1000000007 * (po[n + 1] - 2) %
              1000000007) %= 1000000007;
    }
  }
  cout << ans << endl;
  return 0;
}
void init(int m) {
  miu[1] = 1;
  po[0] = ifac[0] = fac[0] = 1;
  for (int i = 2; i <= n; ++i) {
    if (!vis[i]) prime[++cnt] = i, miu[i] = -1;
    for (int j = 1; j <= cnt && i * prime[j] <= n; ++j) {
      vis[i * prime[j]] = true;
      miu[i * prime[j]] = -miu[i];
      if (i % prime[j] == 0) {
        miu[i * prime[j]] = 0;
        break;
      }
    }
  }
  for (int i = 1; i <= max(n, m) * 2; ++i)
    po[i] = po[i - 1] * 2 % 1000000007,
    fac[i] = 1ll * fac[i - 1] * i % 1000000007,
    ifac[i] = quick_pow(fac[i], 1000000007 - 2);
  for (int i = 1; i <= n; ++i)
    for (int j = i; j <= n; j += i)
      (sum[j] += (1ll * po[i] * miu[j / i] % 1000000007 + 1000000007) %
                 1000000007) %= 1000000007;
  for (int i = 1; i <= n; ++i) {
    int tmp = 1ll * (n / i) * (n / i) % 1000000007;
    sum[i] = 1ll * sum[i] * tmp % 1000000007;
    (ans1 += sum[i]) %= 1000000007;
  }
  return;
}
int f(int a, int b) {
  if (!a && !b) return ans1;
  if (1ll * a * b >= 0) return 0;
  a = abs(a), b = abs(b);
  int g = gcd(a, b);
  a /= g;
  b /= g;
  return po[n / max(a, b) + 1] - 2;
}
int quick_pow(int x, int y) {
  int sum = 1;
  for (; y; y >>= 1, x = 1ll * x * x % 1000000007)
    if (y & 1) sum = 1ll * sum * x % 1000000007;
  return sum;
}
int gcd(int x, int y) {
  if (!y) return x;
  return gcd(y, x % y);
}
int C(int x, int y) {
  if (x > y || x < 0) return 0;
  return 1ll * fac[y] * ifac[x] % 1000000007 * ifac[y - x] % 1000000007;
}
