#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 7, p = 1e9 + 7;
char s[N], t[N];
int n, m, q, ans, vas, w[N], f[N], fac[N], inv[N], ifac[N];
inline long long pows(long long u, int v) {
  long long ans = 1;
  while (v > 0) {
    if (v & 1) ans = ans * u % p;
    u = u * u % p, v = v >> 1;
  }
  return ans;
}
inline void solve() {
  int cnt = 0;
  for (int i = 1; i <= n; i++) {
    if (s[i] == t[i] && s[i] == '?')
      cnt++;
    else if (s[i] != t[i] && s[i] != '?' && t[i] != '?')
      return;
  }
  vas = p - pows(2, cnt);
  ans = (ans + pows(2, cnt) * (pows(2, q + 1) - 2) % p * (pows(2, q + 1) - 2)) %
        p;
}
inline int binom(int a, int b) {
  if (a < b) return 0;
  return 1ll * fac[a] * ifac[b] % p * ifac[a - b] % p;
}
inline int gcd(int a, int b) {
  if (!b) return a;
  return gcd(b, a % b);
}
int main() {
  scanf("%s", s + 1), n = strlen(s + 1);
  scanf("%s", t + 1), m = strlen(t + 1);
  int x = 0, y = 0, a = 0, b = 0;
  cin >> q;
  if (n == m) solve();
  fac[0] = fac[1] = inv[0] = inv[1] = ifac[0] = ifac[1] = 1;
  for (int i = 2; i <= 1e6; i++)
    inv[i] = 1ll * (p - p / i) * inv[p % i] % p,
    fac[i] = 1ll * fac[i - 1] * i % p, ifac[i] = 1ll * ifac[i - 1] * inv[i] % p;
  for (int i = 1; i <= n; i++)
    if (s[i] == 'A')
      x++;
    else if (s[i] == 'B')
      y++;
    else
      a++;
  for (int i = 1; i <= m; i++)
    if (t[i] == 'A')
      x--;
    else if (t[i] == 'B')
      y--;
    else
      b++;
  f[0] = 1;
  for (int i = 1; i <= q; i++) f[i] = f[i - 1] * 2 % p;
  for (int i = 2; i <= q; i++) f[i] = (f[i] + f[i - 1]) % p;
  for (int i = 1; i <= q; i++) w[i] = 1ll * (q / i) * (q / i) % p;
  for (int i = q; i >= 1; i--)
    for (int j = i + i; j <= q; j += i) w[i] = (w[i] - w[j] + p) % p;
  int vac = 0;
  for (int i = 1; i <= q; i++) vac = (vac + 1ll * w[i] * pows(2, i)) % p;
  for (int z = -b; z <= a; z++) {
    int val = binom(a + b, b + z);
    if (val == 0) continue;
    int c = x + z, d = y - b + a - z;
    if (c == 0 && d == 0) {
      vas = (vas + val) % p;
      ans = (ans + 1ll * vas * vac) % p;
    }
    if (1ll * c * d >= 0) continue;
    c = abs(c), d = abs(d);
    if (q / (max(c, d) / gcd(c, d)) > 0)
      ans = (ans + 1ll * val * f[q / (max(c, d) / gcd(c, d))]) % p;
  }
  cout << ans << endl;
}
