#include <bits/stdc++.h>
using namespace std;
long long mC2, inv, sq;
long long m, n, a[100010], b[100010];
long long power(long long num, long long p) {
  if (p == 0) return 1;
  long long ret = power(num, p / 2);
  ret = (ret * ret) % 1000000007;
  if (p & 1) ret = (ret * num) % 1000000007;
  return ret;
}
long long mod_inverse(long long num) { return power(num, 1000000007 - 2); }
long long solve() {
  long long curr = 1;
  long long ans = 0;
  for (int i = 0; i < n; i++) {
    if (a[i] && b[i]) {
      if (a[i] > b[i]) {
        ans = (ans + curr) % 1000000007;
        break;
      } else if (a[i] < b[i])
        break;
    } else if (!a[i] && b[i]) {
      ans = (ans + ((curr * ((((m - b[i] + 1000000007) % 1000000007) * inv) %
                             1000000007)) %
                    1000000007)) %
            1000000007;
      curr = (curr * inv) % 1000000007;
    } else if (a[i] && !b[i]) {
      ans = (ans + ((curr * ((((a[i] - 1 + 1000000007) % 1000000007) * inv) %
                             1000000007)) %
                    1000000007)) %
            1000000007;
      curr = (curr * inv) % 1000000007;
    } else {
      ans = (ans + ((curr * ((mC2 * sq) % 1000000007)) % 1000000007)) %
            1000000007;
      curr = (curr * inv) % 1000000007;
    }
  }
  return ans;
}
void init() {
  inv = mod_inverse(m);
  sq = mod_inverse((m * m) % 1000000007);
  mC2 = (m * (m - 1)) / 2;
  mC2 %= 1000000007;
}
int main() {
  long long i, j, k, ans;
  scanf("%lld %lld", &n, &m);
  init();
  for (i = 0; i < n; i++) scanf("%lld", &a[i]);
  for (i = 0; i < n; i++) scanf("%lld", &b[i]);
  if (m == 1)
    printf("0\n");
  else {
    ans = solve();
    printf("%lld\n", ans);
  }
  return 0;
}
