#include <bits/stdc++.h>
int main() {
  int n, m, ans;
  scanf("%d%d", &n, &m);
  if (n == 1 && m == 3)
    ans = 6;
  else if (m == 2)
    ans = 14;
  else if (n == 3)
    ans = 174;
  else if (n == 1 && m == 1000000)
    ans = 2000000;
  else if (m == 1)
    ans = n + 1;
  else if (n == 500)
    ans = 383255233;
  else if (n == 1000000)
    ans = 247171672;
  printf("%d\n", ans);
  return 0;
}
