#include <bits/stdc++.h>
using namespace std;
long long int mod = 1e9 + 7;
long long int myPow(long long int x, long long int y) {
  long long int ret;
  if (y == 1) return x;
  ret = myPow(x, y / 2);
  ret *= ret;
  ret %= mod;
  if (y % 2) {
    ret *= x;
    ret %= mod;
  }
  return ret;
}
long long int inv(long long int x) { return myPow(x, mod - 2); }
long long int len, alphaSize;
vector<int> w1, w2;
long long int solve(int pos) {
  if (pos == len) return 0LL;
  if (w1[pos] != 0 && w2[pos] != 0) {
    if (w1[pos] > w2[pos]) return 1LL;
    if (w1[pos] < w2[pos]) return 0LL;
    return solve(pos + 1);
  }
  if (w1[pos] == 0 && w2[pos] == 0) {
    return (((inv(alphaSize) * solve(pos + 1)) % mod) +
            ((((((alphaSize * (alphaSize - 1LL)) % mod) * inv(2)) % mod) *
              inv((alphaSize * alphaSize) % mod)) %
             mod)) %
           mod;
  }
  if (w1[pos] == 0) {
    return (((inv(alphaSize) * (alphaSize - w2[pos])) % mod) +
            ((inv(alphaSize) * solve(pos + 1)) % mod)) %
           mod;
  }
  if (w2[pos] == 0) {
    return (((inv(alphaSize) * (w1[pos] - 1)) % mod) +
            ((inv(alphaSize) * solve(pos + 1)) % mod)) %
           mod;
  }
}
int main(void) {
  cin >> len >> alphaSize;
  w1.resize(len);
  w2.resize(len);
  for (int i = 0; i < len; i++) cin >> w1[i];
  for (int i = 0; i < len; i++) cin >> w2[i];
  cout << solve(0) << endl;
  return 0;
}
