fork(1) download
  1. #include<iostream>
  2. using namespace std;
  3.  
  4. #define MOD 1000000007
  5.  
  6. long long a[1000000];
  7. long long c[1000000];
  8. long long dist[100000000];
  9. long long sum;
  10. int n, q;
  11.  
  12. long long power(int a, int b) {
  13. long long res = 1;
  14. for (int j = 1; j <= b; j++) {
  15. res *= a;
  16. res %= MOD;
  17. }
  18. return res;
  19. }
  20.  
  21. int main() {
  22. cin >> n >> q;
  23. for (int i = 1; i <= n; i++) {
  24. cin >> a[i];
  25. }
  26. c[0] = 1;
  27. c[q + 1] = 1;
  28. for (int i = 1; i <= q; i++) {
  29. cin >> c[i];
  30. }
  31. for (int i = 2; i <= n; i++) {
  32. dist[i] = power(a[i - 1], a[i]);
  33. }
  34. for (int i = 1; i <= q + 1; i++) {
  35. if (c[i - 1] < c[i]) {
  36. for (int j = c[i - 1] + 1; j <= c[i]; j++) {
  37. sum += dist[j];
  38. sum %= MOD;
  39. }
  40. }
  41. else {
  42. for (int j = c[i - 1]; j >= c[i] + 1; j--) {
  43. sum += dist[j];
  44. sum %= MOD;
  45. }
  46. }
  47. }
  48. cout << sum << endl;
  49. return 0;
  50. }
Success #stdin #stdout 0s 800256KB
stdin
3 2
23 12 9
2 3
stdout
876796540