#include <bits/stdc++.h>
using namespace std;
const int N = 300010;
const int mod = 1e9 + 7;
inline int Pow(int x, int y) {
  int res = 1;
  for (; y; y >>= 1, x = (long long)x * x % mod)
    if (y & 1) res = (long long)res * x % mod;
  return res;
}
int diff0, diff1, diff, n, pw[N];
int flag[N], mu[N], prime[N], pcnt, tmp[N], result;
inline void sieve() {
  mu[1] = pw[0] = 1;
  for (int i = 2; i <= n; i++) {
    if (!flag[i]) prime[++pcnt] = i, mu[i] = -1;
    for (int j = 1; j <= pcnt && i * prime[j] <= n; j++) {
      flag[i * prime[j]] = 1;
      if (i % prime[j] == 0) break;
      mu[i * prime[j]] = -mu[i];
    }
  }
  for (int i = 1; i <= n + 1; i++) pw[i] = pw[i - 1] * 2 % mod;
  for (int d = 1; d <= n; d++)
    for (int k = d, l = 1; k <= n; k += d, l++)
      tmp[k] = (tmp[k] + (long long)pw[d] * mu[l]) % mod;
  for (int T = 1; T <= n; T++)
    result = (result + (long long)(n / T) * (n / T) % mod * tmp[T]) % mod;
  result = (result + mod) % mod;
}
char S[N], T[N];
inline void Pre() {
  int a = strlen(S + 1), b = strlen(T + 1);
  for (int i = 1; i <= a; i++) {
    if (S[i] == 'A')
      diff0++;
    else if (S[i] == 'B')
      diff1++;
    else
      diff++;
  }
  for (int i = 1; i <= b; i++) {
    if (T[i] == 'A')
      diff0--;
    else if (T[i] == 'B')
      diff1--;
    else
      diff--;
  }
  sieve();
}
int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }
inline int F(int delta) {
  int ct0 = diff0 + delta, ct1 = diff1 + diff - delta;
  if (ct0 == 0 && ct1 == 0) return result;
  if ((long long)ct0 * ct1 >= 0) return 0;
  int A = abs(ct0), B = abs(ct1), g = gcd(A, B);
  A /= g, B /= g;
  return pw[n / max(A, B) + 1] - 2;
}
int c[N * 2], lim;
inline void GetC(int n) {
  c[0] = 1, lim = n;
  int res = 1;
  for (int i = 1; i <= n; i++) {
    res = (long long)res * Pow(i, mod - 2) % mod * (n - i + 1) % mod;
    c[i] = res;
  }
}
inline int C(int r) {
  if (r < 0 || r > lim) return 0;
  return c[r];
}
int main() {
  scanf("%s%s%d", S + 1, T + 1, &n), Pre();
  int x = 0, y = 0, A = strlen(S + 1), B = strlen(T + 1);
  for (int i = 1; i <= A; i++)
    if (S[i] == '?') x++;
  for (int i = 1; i <= B; i++)
    if (T[i] == '?') y++;
  GetC(x + y);
  int res = 0;
  for (int d = -y; d <= x; d++) res = (res + (long long)F(d) * C(y + d)) % mod;
  if (A == B) {
    int t = 1;
    for (int i = 1; i <= A; i++) {
      if (S[i] == '?' || T[i] == '?') {
        if (S[i] == '?' && T[i] == '?') t = t * 2 % mod;
      } else if (S[i] != T[i])
        t = 0;
    }
    res = (res - (long long)t * result % mod + mod) % mod;
    res = (res + (long long)t * (pw[n + 1] - 2) % mod * (pw[n + 1] - 2)) % mod;
  }
  printf("%d\n", res);
  return 0;
}
