1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <map>
#include <set>
#include <chrono>
#include <functional>
#include <cstdlib>
#include <cassert>
using namespace std;

long long P = 1000000007;

struct Counter
{
  long long numberLeft = 0;
  long long numberBottom = 0;
};

class DryblingCounterN5
{
public:
  long long getNumberOfDrybling(std::string word)
  {
    std::vector<std::vector<Counter>> counters(word.size()+1);
    for (int i=0; i<=word.size(); ++i)
      counters[i] = std::vector<Counter>(word.size()+1, Counter());
    counters[0][0].numberLeft = 1;
    int maxRight = 0;
    int maxUp = 0;
    for (int i = 0; i < word.size(); ++i)
    {
      bool right = (word[i] == 'P');
      if (right)
      {
        for (int x = maxRight + 1; x >= 1; --x)
        {
          for (int y = 0; y <= maxUp; ++y)
          {
            if (y>=x)
            {
              counters[x][y].numberLeft = counters[x-1][y].numberLeft + counters[x-1][y].numberBottom;
              if (counters[x][y].numberLeft > P)
                counters[x][y].numberLeft -=P;
              if (counters[x][y].numberLeft > 0)
                maxRight = std::max(maxRight, x);
            }
          }
        }
      }
      else
      {
        for (int y = maxUp + 1; y >= 1; --y)
        {
          for (int x = 0; x <= maxRight; ++x)
          {
              counters[x][y].numberBottom = counters[x][y-1].numberLeft + counters[x][y-1].numberBottom;
              if (counters[x][y].numberBottom > P)
                counters[x][y].numberBottom-=P;
              if (counters[x][y].numberBottom > 0)
                maxUp = std::max(maxUp, y);
          }
        }
      }
    }
    long long result = 0;
    for (int i=1; i<=std::min(maxRight, maxUp); ++i)
    {
      long long currentNumber = counters[i][i].numberBottom + counters[i][i].numberLeft;
      result += currentNumber;
    }
    result = result % P;
    return result;
  }
  
  std::vector<std::vector<long long>> getNumberOfDrybling(std::vector<std::string> words)
  {
    std::vector<std::vector<long long>> results(words.size());
    for (int i=0; i <words.size(); ++i)
    {
      for (int j=0; j<words.size();  ++j)
      {
        std::string mergedWord = words[i] + words[j];
        results[i].push_back(getNumberOfDrybling(mergedWord));
      }
    }
    return results;
  }
};

class DryblingCounter
{
public:
  long long getNumberOfDrybling(std::string word)
  {
    std::vector<Counter> counters(word.size() + 1, Counter());
    counters[0].numberBottom = 1;
    int maxNonZero = 0;
    for (int i = 0; i < word.size(); ++i)
    {
      bool right = (word[i] == 'P');
      if (right)
      {
        for (int x = 0; x <= maxNonZero; ++x)
        {
          counters[x].numberLeft = counters[x + 1].numberLeft + counters[x + 1].numberBottom;
          if (counters[x].numberLeft >= P)
            counters[x].numberLeft -= P;
        }
      }
      else
      {
        for (int x = maxNonZero+1; x >= 1; --x)
        {
          counters[x].numberBottom = counters[x - 1].numberLeft + counters[x - 1].numberBottom;
          if (counters[x].numberBottom >= P)
            counters[x].numberBottom -= P;
        }
        ++maxNonZero;
      }
    }
    long long result = counters[0].numberBottom + counters[0].numberLeft - 1;
    result = result % P;
    return result;
  }

  std::vector<std::vector<long long>> getNumberOfDrybling(std::vector<std::string> words)
  {
    std::vector<std::vector<long long>> results(words.size());
    for (int i = 0; i < words.size(); ++i)
    {
      for (int j = 0; j < words.size(); ++j)
      {
        std::string mergedWord = words[i] + words[j];
        results[i].push_back(getNumberOfDrybling(mergedWord));
      }
    }
    return results;
  }
};

int main()
{
  const clock_t begin_time = clock();
  int wordNumber;
  std::cin >> wordNumber;
  std::vector<std::string> words;
  for (int i = 0; i < wordNumber; ++i)
  {
    std::string word;
    std::cin >> word;
    words.push_back(word);
  }
  DryblingCounterN5 counterN5;
  DryblingCounter counter;
  std::string w;
  for (int i = 0; i < 100; ++i)
  {
    w.push_back('L');
    w.push_back('P');
  }
  //std::cout << counterN5.getNumberOfDrybling(w) << "\n";
  //std::cout << counter.getNumberOfDrybling(w) << "\n";
  //return 0;
  std::vector<std::vector<long long>> result = counter.getNumberOfDrybling(words);
  for (int i = 0; i < words.size(); ++i)
  {
    for (int j = 0; j < words.size(); ++j)
    {
      std::cout << result[i][j] << " ";
    }
    std::cout << "\n";
  }
  int a;
  std::cin >> a;
  return 0;
  //std::cout << float(clock() - begin_time) / CLOCKS_PER_SEC << "\n";
};