Language: C++

#include<bits/stdc++.h>
using namespace std;
const int P=1e9+7;
const int N=1e3+5;
struct Mar{
	int v[2][2];
}x,ans;
Mar operator * (Mar x,Mar y){
	Mar ans;
	for(int i=0;i<2;i++)
		for(int j=0;j<2;j++){
			ans.v[i][j]=0;
			for(int k=0;k<2;k++)
				ans.v[i][j]=(ans.v[i][j]+1LL*x.v[i][k]*y.v[k][j])%P;
		}
	return ans;
}
long long fsp(long long x,long long y){
	long long ans=1;
	while(y){
		if(y&1)	ans=ans*x%P;
		x=x*x%P,y>>=1;
	}
	return ans;
}
long long n;
char s[N][N];
int h,w,s1,s2,rl,ud,cnt;
int main(){
	scanf("%d%d%lld",&h,&w,&n);
	for(int i=1;i<=h;i++)
		scanf("%s",s[i]+1);
	for(int i=1;i<=h;i++)
		for(int j=1;j<=w;j++)
			if(s[i][j]=='#'){
				s1+=s[i][j-1]=='#';
				s2+=s[i-1][j]=='#';
				++cnt;
			}
	for(int i=1;i<=h;i++)
		if(s[i][1]=='#'&&s[i][w]=='#')	++rl;
	for(int i=1;i<=w;i++)
		if(s[1][i]=='#'&&s[h][i]=='#')	++ud;
	if((rl&&ud)||n<2){
		puts("1");
		return 0;
	}
	if(!rl&&!ud){
		printf("%lld\n",fsp(cnt,n-1));
		return 0;
	}
	if(ud)	swap(s1,s2),swap(rl,ud);

	x.v[0][0]=cnt,x.v[0][1]=s1;
	x.v[1][1]=rl,ans.v[0][0]=ans.v[1][1]=1,--n;
	while(n){
		if(n&1)	ans=x*ans;
		x=x*x,n>>=1;
	}
	printf("%d\n",(ans.v[0][0]-ans.v[0][1]+P)%P);
	return 0;
}
#include<bits/stdc++.h>
using namespace std;
const int P=1e9+7;
const int N=1e3+5;
struct Mar{
	int v[2][2];
}x,ans;
Mar operator * (Mar x,Mar y){
	Mar ans;
	for(int i=0;i<2;i++)
		for(int j=0;j<2;j++){
			ans.v[i][j]=0;
			for(int k=0;k<2;k++)
				ans.v[i][j]=(ans.v[i][j]+1LL*x.v[i][k]*y.v[k][j])%P;
		}
	return ans;
}
long long fsp(long long x,long long y){
	long long ans=1;
	while(y){
		if(y&1)	ans=ans*x%P;
		x=x*x%P,y>>=1;
	}
	return ans;
}
long long n;
char s[N][N];
int h,w,s1,s2,rl,ud,cnt;
int main(){
	scanf("%d%d%lld",&h,&w,&n);
	for(int i=1;i<=h;i++)
		scanf("%s",s[i]+1);
	for(int i=1;i<=h;i++)
		for(int j=1;j<=w;j++)
			if(s[i][j]=='#'){
				s1+=s[i][j-1]=='#';
				s2+=s[i-1][j]=='#';
				++cnt;
			}
	for(int i=1;i<=h;i++)
		if(s[i][1]=='#'&&s[i][w]=='#')	++rl;
	for(int i=1;i<=w;i++)
		if(s[1][i]=='#'&&s[h][i]=='#')	++ud;
	if((rl&&ud)||n<2){
		puts("1");
		return 0;
	}
	if(!rl&&!ud){
		printf("%lld\n",fsp(cnt,n-1));
		return 0;
	}
	if(ud)	swap(s1,s2),swap(rl,ud);

	x.v[0][0]=cnt,x.v[0][1]=s1;
	x.v[1][1]=rl,ans.v[0][0]=ans.v[1][1]=1,--n;
	while(n){
		if(n&1)	ans=x*ans;
		x=x*x,n>>=1;
	}
	printf("%d\n",(ans.v[0][0]-ans.v[0][1]+P)%P);
	return 0;
}


Compiler output:
a.cc:65:11: error: redefinition of 'const int P'
   65 | const int P=1e9+7;
      |           ^
a.cc:3:11: note: 'const int P' previously defined here
    3 | const int P=1e9+7;
      |           ^
a.cc:66:11: error: redefinition of 'const int N'
   66 | const int N=1e3+5;
      |           ^
a.cc:4:11: note: 'const int N' previously defined here
    4 | const int N=1e3+5;
      |           ^
a.cc:67:8: error: redefinition of 'struct Mar'
   67 | struct Mar{
      |        ^~~
a.cc:5:8: note: previous definition of 'struct Mar'
    5 | struct Mar{
      |        ^~~
a.cc:69:2: error: conflicting declaration 'int x'
   69 | }x,ans;
      |  ^
a.cc:7:2: note: previous declaration as 'Mar x'
    7 | }x,ans;
      |  ^
a.cc:69:4: error: conflicting declaration 'int ans'
   69 | }x,ans;
      |    ^~~
a.cc:7:4: note: previous declaration as 'Mar ans'
    7 | }x,ans;
      |    ^~~
a.cc:70:5: error: redefinition of 'Mar operator*(Mar, Mar)'
   70 | Mar operator * (Mar x,Mar y){
      |     ^~~~~~~~
a.cc:8:5: note: 'Mar operator*(Mar, Mar)' previously defined here
    8 | Mar operator * (Mar x,Mar y){
      |     ^~~~~~~~
a.cc:80:11: error: redefinition of 'long long int fsp(long long int, long long int)'
   80 | long long fsp(long long x,long long y){
      |           ^~~
a.cc:18:11: note: 'long long int fsp(long long int, long long int)' previously defined here
   18 | long long fsp(long long x,long long y){
      |           ^~~
a.cc:88:11: error: redefinition of 'long long int n'
   88 | long long n;
      |           ^
a.cc:26:11: note: 'long long int n' previously declared here
   26 | long long n;
      |           ^
a.cc:89:6: error: redefinition of 'char s [1005][1005]'
   89 | char s[N][N];
      |      ^
a.cc:27:6: note: 'char s [1005][1005]' previously declared here
   27 | char s[N][N];
      |      ^
a.cc:90:5: error: redefinition of 'int h'
   90 | int h,w,s1,s2,rl,ud,cnt;
      |     ^
a.cc:28:5: note: 'int h' previously declared here
   28 | int h,w,s1,s2,rl,ud,cnt;
      |     ^
a.cc:90:7: error: redefinition of 'int w'
   90 | int h,w,s1,s2,rl,ud,cnt;
      |       ^
a.cc:28:7: note: 'int w' previously declared here
   28 | int h,w,s1,s2,rl,ud,cnt;
      |       ^
a.cc:90:9: error: redefinition of 'int s1'
   90 | int h,w,s1,s2,rl,ud,cnt;
      |         ^~
a.cc:28:9: note: 'int s1' previously declared here
   28 | int h,w,s1,s2,rl,ud,cnt;
      |         ^~
a.cc:90:12: error: redefinition of 'int s2'
   90 | int h,w,s1,s2,rl,ud,cnt;
      |            ^~
a.cc:28:12: note: 'int s2' previously declared here
   28 | int h,w,s1,s2,rl,ud,cnt;
      |            ^~
a.cc:90:15: error: redefinition of 'int rl'
   90 | int h,w,s1,s2,rl,ud,cnt;
      |               ^~
a.cc:28:15: note: 'int rl' previously declared here
   28 | int h,w,s1,s2,rl,ud,cnt;
      |               ^~
a.cc:90:18: error: redefinition of 'int ud'
   90 | int h,w,s1,s2,rl,ud,cnt;
      |                  ^~
a.cc:28:18: note: 'int ud' previously declared here
   28 | int h,w,s1,s2,rl,ud,cnt;
      |                  ^~
a.cc:90:21: error: redefinition of 'int cnt'
   90 | int h,w,s1,s2,rl,ud,cnt;
      |                     ^~~
a.cc:28:21: note: 'int cnt' previously declared here
   28 | int h,w,s1,s2,rl,ud,cnt;
      |                     ^~~
a.cc:91:5: error: redefinition of 'int main()'
   91 | int main(){
      |     ^~~~
a.cc:29:5: note: 'int main()' previously defined here
   29 | int main(){
      |     ^~~~