#include <bits/stdc++.h>
using namespace std;
template <class U, class T>
void Max(U &first, T second) {
  if (first < second) first = second;
}
template <class U, class T>
void Min(U &first, T second) {
  if (first > second) first = second;
}
template <class T>
void add(int &a, T b) {
  a = (a + b) % 998244353;
}
int pow(int a, long long b) {
  int ans = 1;
  while (b) {
    if (b & 1) ans = 1LL * ans * a % 998244353;
    a = 1LL * a * a % 998244353;
    b >>= 1;
  }
  return ans;
}
int pow(int a, int b, int c) {
  int ans = 1;
  while (b) {
    if (b & 1) ans = 1LL * ans * a % c;
    a = 1LL * a * a % c;
    b >>= 1;
  }
  return ans;
}
int q[1000010], f[1000010], inv[1000010];
int C(int a, int b) {
  return 1LL * f[a] * inv[b] % 998244353 * inv[a - b] % 998244353;
}
int main() {
  srand(time(NULL));
  int T, ca = 0, m = 0, k, i, j, K, n;
  scanf("%d", &n);
  if (n == 1) {
    printf("3\n");
    return 0;
  }
  q[0] = f[0] = 1;
  for (int i = 1; i < n + 1; i++)
    q[i] = q[i - 1] * 3LL % 998244353,
    f[i] = f[i - 1] * (long long)i % 998244353;
  inv[n] = pow(f[n], 998244353 - 2LL);
  for (int i = n - 1; i >= 0; i--)
    inv[i] = inv[i + 1] * (long long)(i + 1) % 998244353;
  long long ans = pow(q[n] - 3, 1LL * n);
  for (int i = 1; i < n + 1; i++) {
    int w = 1LL * pow(q[n - i], 1LL * n) * (q[i] - 3) % 998244353;
    w += 3LL * pow(q[n - i] - 1, 1LL * n) % 998244353;
    w %= 998244353;
    w = 1LL * C(n, i) * w % 998244353;
    if (i & 1)
      ans -= w;
    else
      ans += w;
  }
  ans = pow(3, 1LL * n * n) - ans;
  ans %= 998244353;
  if (ans < 0) ans += 998244353;
  printf("%lld\n", ans);
}
