#include <bits/stdc++.h>
using namespace std;
template <class TH>
void _dbg(const char *sdbg, TH h) {
  cerr << sdbg << "=" << h << "\n";
}
template <class TH, class... TA>
void _dbg(const char *sdbg, TH h, TA... a) {
  while (*sdbg != ',') cerr << *sdbg++;
  cerr << "=" << h << ",";
  _dbg(sdbg + 1, a...);
}
const long long N = 5 * 100 * 1000 + 7;
long long test;
long long n, m;
long long a[N], b[N];
long long fpow(long long a, long long b) {
  if (b == 0) return 1;
  if (b % 2 == 0) {
    long long pom = fpow(a, b / 2);
    return (pom * pom) % 1000000007;
  }
  if (b % 2 == 1) {
    return (a * fpow(a, b - 1)) % 1000000007;
  }
  return 0;
}
long long rev(long long a, long long b) {
  return (a * fpow(b, 1000000007 - 2)) % 1000000007;
}
long long pref[N];
long long res, a1;
long long gcd(long long a, long long b) {
  if (b == 0) {
    return a;
  }
  return gcd(b, a % b);
}
void solve() {
  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];
  }
  for (long long i = 1; i <= (n); ++i) {
    pref[i] = (a[i] == 0) + (b[i] == 0) + pref[i - 1];
  }
  a1 = 1;
  long long B = fpow(fpow(m, pref[n]), 1000000007 - 2) % 1000000007;
  for (long long i = 1; i <= (n); ++i) {
    if (a[i] != b[i] && a[i] != 0 && b[i] != 0) {
      if (a[i] > b[i]) {
        res +=
            ((a1 * fpow(m, pref[n] - pref[i])) % 1000000007) * B % 1000000007;
        res %= 1000000007;
      }
      break;
    }
    if (a[i] == b[i] && a[i] == 0) {
      res += ((a1 * ((((m * (m - 1)) / 2) * fpow(m, pref[n] - pref[i])) %
                     1000000007)) %
              1000000007) *
             B % 1000000007;
      a1 *= m;
      a1 %= 1000000007;
    }
    if (a[i] != b[i] && a[i] == 0) {
      res += ((a1 * (((m - b[i]) * fpow(m, pref[n] - pref[i])) % 1000000007)) %
              1000000007) *
             B % 1000000007;
      res %= 1000000007;
    }
    if (a[i] != b[i] && b[i] == 0) {
      res += ((a1 * (((a[i] - 1) * fpow(m, pref[n] - pref[i])) % 1000000007)) %
              1000000007) *
             B % 1000000007;
      res %= 1000000007;
    }
  }
  res += 1000000007;
  res %= 1000000007;
  cout << res % 1000000007;
  return;
}
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cout.precision(10);
  cout << fixed;
  test = 1;
  while (test--) {
    solve();
  }
  return 0;
}
