#include <bits/stdc++.h>
using namespace std;
long long a[100005], b[100005], suff1[100005], suff2[100005];
long long n, m;
long long pow1(long long a, long long b) {
  if (a == 0) return 0LL;
  long long x = 1, y = a;
  while (b > 0) {
    if (b % 2 == 1) {
      x = (x * y);
      if (x > 1000000007) x %= 1000000007;
    }
    y = (y * y);
    if (y > 1000000007) y %= 1000000007;
    b /= 2;
  }
  return x % 1000000007;
}
long long solve(long long i) {
  if (i >= n + 1) return 0LL;
  if (a[i] == 0 && b[i] == 0) {
    long long temp1 = ((((m - 1) * m) / 2LL) % 1000000007);
    long long temp2 =
        ((pow1(m, suff1[i] - 1) * pow1(m, suff2[i] - 1)) % 1000000007) %
        1000000007;
    return ((temp1 * temp2) % 1000000007 + (m * solve(i + 1)) % 1000000007) %
           1000000007;
  } else if (a[i] == 0 && b[i] != 0) {
    long long temp1 =
        ((pow1(m, suff1[i] - 1) * pow1(m, suff2[i])) % 1000000007) % 1000000007;
    long long temp2 = (m - b[i]) % 1000000007;
    return (solve(i + 1) % 1000000007 + (temp2 * temp1) % 1000000007) %
           1000000007;
  } else if (b[i] == 0 && a[i] != 0) {
    long long temp1 =
        ((pow1(m, suff1[i]) * pow1(m, suff2[i] - 1)) % 1000000007) % 1000000007;
    return (solve(i + 1) % 1000000007 + ((a[i] - 1) * temp1) % 1000000007) %
           1000000007;
  } else {
    if (a[i] < b[i])
      return 0LL;
    else if (a[i] > b[i])
      return (pow1(m, suff1[i]) * pow1(m, suff2[i])) % 1000000007;
    else
      solve(i + 1) % 1000000007;
  }
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> n >> m;
  for (long long i = 1; i <= n; i++) cin >> a[i];
  for (long long i = 1; i <= n; i++) cin >> b[i];
  if (a[n] == 0)
    suff1[n] = 1;
  else
    suff1[n] = 0;
  if (b[n] == 0)
    suff2[n] = 1;
  else
    suff2[n] = 0;
  for (long long i = n - 1; i >= 1; i--) {
    suff1[i] = suff1[i + 1];
    suff2[i] = suff2[i + 1];
    if (a[i] == 0) suff1[i]++;
    if (b[i] == 0) suff2[i]++;
  }
  long long num = solve(1);
  long long den = pow1(m, suff1[1] + suff2[1]);
  cout << (num * pow1(den, 1000000007 - 2)) % 1000000007 << endl;
}
