#include <bits/stdc++.h>
using namespace std;
long long a[100005], b[100005], aaa, nnn, p, n, m;
long long pw(long long di, long long mi) {
  long long res = 1, a = di;
  while (mi) {
    if (mi & 1) res = res * a % 1000000007;
    a = a * a % 1000000007;
    mi >>= 1;
  }
  return res;
}
int main() {
  scanf("%I64d%I64d", &n, &m);
  for (int i = 0; i < n; i++) scanf("%I64d", &a[i]);
  for (int i = 0; i < n; i++) scanf("%I64d", &b[i]);
  nnn = 1;
  p = pw(m, 1000000005);
  for (int i = 0; i < n; i++) {
    if (!a[i] && !b[i]) {
      nnn = nnn * p % 1000000007ll;
      aaa = (aaa + nnn * (m - 1) % 1000000007ll * 500000004LL % 1000000007ll) %
            1000000007ll;
    } else if (!a[i]) {
      nnn = nnn * p % 1000000007ll;
      aaa = (aaa + nnn * (m - b[i]) % 1000000007ll) % 1000000007ll;
    } else if (!b[i]) {
      nnn = nnn * p % 1000000007ll;
      aaa = (aaa + nnn * (a[i] - 1) % 1000000007ll) % 1000000007ll;
    } else if (a[i] != b[i]) {
      if (a[i] > b[i]) aaa = (aaa + nnn) % 1000000007ll;
      break;
    }
  }
  printf("%I64d\n", aaa);
}
